diff --git a/README.md b/README.md index 4b21c59..a82fa50 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: 3/24 +#### Stars: 4/24 |Mon|Tue|Wed|Thu|Fri|Sat|Sun| |:-:|:-:|:-:|:-:|:-:|:-:|:-:| -|1
:star::star:|2
:star:|3
|4
|5
|6
|7
| +|1
:star::star:|2
:star::star:|3
|4
|5
|6
|7
| |8
|9
|10
|11
|12
||| diff --git a/res/stats.json b/res/stats.json index f981806..6de54de 100644 --- a/res/stats.json +++ b/res/stats.json @@ -5,7 +5,7 @@ }, "day02": { "puzzle1": true, - "puzzle2": false + "puzzle2": true }, "day03": { "puzzle1": false, diff --git a/src/day02/puzzle2.lua b/src/day02/puzzle2.lua index 2f4bc9a..254c68f 100644 --- a/src/day02/puzzle2.lua +++ b/src/day02/puzzle2.lua @@ -1,7 +1,46 @@ +local utils = require "utils" +local puzzle1 = require(SRC_PATH .. "/day02/puzzle1") local puzzle2 = {} +function puzzle2.isValid(id) + local str = tostring(id) + local totLen = #str + for len=1, totLen - 1 do + if totLen % len == 0 then + local ref = str:sub(1, len) + local allSame = true + for i=1, totLen / len - 1 do + if str:sub(len * i + 1, len * (i + 1)) ~= ref then + allSame = false + break + end + end + if allSame then + return false + end + end + end + return true +end + +function puzzle2.countInvalids(range) + local min, max = puzzle1.splitRange(range) + local total = 0 + for i=min, max do + if not puzzle2.isValid(i) then + total = total + i + end + end + return total +end + function puzzle2.solve(input) - return 0 + local ranges = utils.split(input, ",") + local total = 0 + for _, range in ipairs(ranges) do + total = total + puzzle2.countInvalids(range) + end + return total end return puzzle2