diff --git a/progress.png b/progress.png index 218ee03..77276cf 100644 Binary files a/progress.png and b/progress.png differ diff --git a/progress.yaml b/progress.yaml index bbf170a..0f03e03 100644 --- a/progress.yaml +++ b/progress.yaml @@ -1,2 +1,2 @@ 1: - stars: 1 \ No newline at end of file + stars: 2 \ No newline at end of file diff --git a/src/day1/puzzle2.typ b/src/day1/puzzle2.typ index e69de29..fcde641 100644 --- a/src/day1/puzzle2.typ +++ b/src/day1/puzzle2.typ @@ -0,0 +1,36 @@ +#import "/src/utils.typ": * + +#let solve(input) = { + let lines = input.split("\n") + let (l1, l2) = ((), ()) + let reg = regex("^(\d+)\s+(\d+)$") + for line in lines { + let digits = line.match(reg) + l1.push(digits.captures.first()) + l2.push(digits.captures.last()) + } + + let nums = (:) + + for n in l1 { + if n not in nums.keys() { + nums.insert(n, (0, 0)) + } + nums.at(n).first() += 1 + } + + for n in l2 { + if n in nums.keys() { + nums.at(n).last() += 1 + } + } + + let total = nums.pairs().map(((num, (a, b))) => int(num) * a * b).sum() + return total +} + +#show-puzzle( + 1, + solve, + example: 31 +) \ No newline at end of file diff --git a/src/main.pdf b/src/main.pdf index 3c3915d..2f9ca51 100644 Binary files a/src/main.pdf and b/src/main.pdf differ diff --git a/src/utils.typ b/src/utils.typ index 91fc9fb..86d36dd 100644 --- a/src/utils.typ +++ b/src/utils.typ @@ -15,25 +15,36 @@ #let check-example(day, func, target-result, suffix: none) = { let result = (func)(read(get-example-path(day, suffix: suffix))) - /*assert( - result == target-result, - message: "Expected '" + repr(target-result) + "' got '" + repr(result) + "'" - )*/ let passes = (result == target-result) let name = if suffix == none [Example] else [Example '#suffix'] - box( + let badge = box( inset: (x: 1.2em, y: 0.6em), radius: 1.2em, + baseline: 35%, fill: if passes {green.lighten(20%)} else {red.lighten(20%)}, if passes [#name passes] else [#name fails] ) - h(0.6em) + if not passes { + badge = box( + baseline: 35%, + grid( + columns: 2, + align: horizon, + )[ + #badge + Expected '#repr(target-result)' got '#repr(result)' + ] + ) + } + + [#badge #h(0.6em)] } #let show-result(result) = { box( inset: (x: 1.2em, y: 0.6em), radius: 1.2em, + baseline: 35%, fill: blue.lighten(20%), text(fill: white)[Result: #result] )