diff --git a/progress.png b/progress.png index e01e994..5bb2a85 100644 Binary files a/progress.png and b/progress.png differ diff --git a/progress.yaml b/progress.yaml index c55a64b..80bd6a7 100644 --- a/progress.yaml +++ b/progress.yaml @@ -3,4 +3,4 @@ 2: stars: 2 3: - stars: 1 \ No newline at end of file + stars: 2 \ No newline at end of file diff --git a/res/examples/day3.txt b/res/examples/day3_1.txt similarity index 100% rename from res/examples/day3.txt rename to res/examples/day3_1.txt diff --git a/res/examples/day3_2.txt b/res/examples/day3_2.txt new file mode 100644 index 0000000..b774ec9 --- /dev/null +++ b/res/examples/day3_2.txt @@ -0,0 +1 @@ +xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5)) \ No newline at end of file diff --git a/src/day3/puzzle1.typ b/src/day3/puzzle1.typ index b46419a..d4a520d 100644 --- a/src/day3/puzzle1.typ +++ b/src/day3/puzzle1.typ @@ -12,5 +12,5 @@ #show-puzzle( 3, 1, solve, - example: 161 + example: ("1": 161) ) \ No newline at end of file diff --git a/src/day3/puzzle2.typ b/src/day3/puzzle2.typ index e69de29..20b0b3f 100644 --- a/src/day3/puzzle2.typ +++ b/src/day3/puzzle2.typ @@ -0,0 +1,31 @@ +#import "/src/utils.typ": * + +#let solve(input) = { + let dos = input.matches(regex("do\(\)")) + let donts = input.matches(regex("don't\(\)")) + let toggles = dos.map(m => (m.end, true)) + toggles += donts.map(m => (m.end, false)) + toggles = toggles.sorted(key: t => t.first()) + + let is-enabled(i) = { + let t = toggles.rev().find(t => t.first() <= i) + return t == none or t.last() + } + + let matches = input.matches(regex("mul\((\d{1,3}),(\d{1,3})\)")) + let total = matches.map(m => { + if is-enabled(m.start) { + m.captures.map(int) + .product() + } else { + 0 + } + }).sum() + return total +} + +#show-puzzle( + 3, 2, + solve, + example: ("2": 48) +) \ No newline at end of file diff --git a/src/main.pdf b/src/main.pdf index 964c603..d93f21f 100644 Binary files a/src/main.pdf and b/src/main.pdf differ diff --git a/src/utils.typ b/src/utils.typ index cc435f0..88ae385 100644 --- a/src/utils.typ +++ b/src/utils.typ @@ -67,7 +67,14 @@ ) } -#let show-puzzle(day, puzzle, func, example: none, visualize: none) = { +#let show-puzzle( + day, + puzzle, + func, + example: none, + visualize: none, + only-example: false +) = { let check-example = check-example.with(visualize: visualize) if example != none { if type(example) == dictionary { @@ -80,9 +87,11 @@ linebreak() } - let input = get-input(day) - let result = (func)(input) - show-result(result) + if not only-example { + let input = get-input(day) + let result = (func)(input) + show-result(result) + } } #let day-template(day, puzzle1, puzzle2, stars: 0) = {