diff --git a/README.md b/README.md index 6e0b386..4b21c59 100644 --- a/README.md +++ b/README.md @@ -8,10 +8,10 @@ This project can also be run using the amazing [CraftOS-PC emulator](https://git ## Progress -#### Stars: 2/24 +#### Stars: 3/24 |Mon|Tue|Wed|Thu|Fri|Sat|Sun| |:-:|:-:|:-:|:-:|:-:|:-:|:-:| -|1
:star::star:|2
|3
|4
|5
|6
|7
| +|1
:star::star:|2
:star:|3
|4
|5
|6
|7
| |8
|9
|10
|11
|12
||| diff --git a/res/examples/day02.txt b/res/examples/day02.txt new file mode 100644 index 0000000..b3dc7c4 --- /dev/null +++ b/res/examples/day02.txt @@ -0,0 +1,3 @@ +11-22,95-115,998-1012,1188511880-1188511890,222220-222224, +1698522-1698528,446443-446449,38593856-38593862,565653-565659, +824824821-824824827,2121212118-2121212124 \ No newline at end of file diff --git a/res/stats.json b/res/stats.json index 08df9c6..f981806 100644 --- a/res/stats.json +++ b/res/stats.json @@ -4,7 +4,7 @@ "puzzle2": true }, "day02": { - "puzzle1": false, + "puzzle1": true, "puzzle2": false }, "day03": { diff --git a/src/day02/puzzle1.lua b/src/day02/puzzle1.lua new file mode 100644 index 0000000..073f4f8 --- /dev/null +++ b/src/day02/puzzle1.lua @@ -0,0 +1,38 @@ +local utils = require "utils" +local puzzle1 = {} + +function puzzle1.splitRange(range) + local parts = utils.split(range, "-") + return tonumber(parts[1]), tonumber(parts[2]) +end + +function puzzle1.isValid(id) + local str = tostring(id) + if #str % 2 == 1 then + return true + end + local mid = #str / 2 + return str:sub(1, mid) ~= str:sub(mid + 1) +end + +function puzzle1.countInvalids(range) + local min, max = puzzle1.splitRange(range) + local total = 0 + for i=min, max do + if not puzzle1.isValid(i) then + total = total + i + end + end + return total +end + +function puzzle1.solve(input) + local ranges = utils.split(input, ",") + local total = 0 + for _, range in ipairs(ranges) do + total = total + puzzle1.countInvalids(range) + end + return total +end + +return puzzle1 diff --git a/src/day02/puzzle2.lua b/src/day02/puzzle2.lua new file mode 100644 index 0000000..2f4bc9a --- /dev/null +++ b/src/day02/puzzle2.lua @@ -0,0 +1,7 @@ +local puzzle2 = {} + +function puzzle2.solve(input) + return 0 +end + +return puzzle2 diff --git a/src/lib/utils.lua b/src/lib/utils.lua index 3ae6ea4..38bd219 100644 --- a/src/lib/utils.lua +++ b/src/lib/utils.lua @@ -78,14 +78,18 @@ function utils.waitForKey(targetKey) end end -function utils.splitLines(data) +function utils.split(data, sep) local t = {} - for str in string.gmatch(data, "([^\n]+)") do + for str in string.gmatch(data, "([^" .. sep .. "]+)") do table.insert(t, str) end return t end +function utils.splitLines(data) + return utils.split(data, "\n") +end + function utils.round(x) return x >= 0 and math.floor(x + 0.5) or math.ceil(x - 0.5) end