diff --git a/progress.png b/progress.png index 7dfab21..e01e994 100644 Binary files a/progress.png and b/progress.png differ diff --git a/progress.yaml b/progress.yaml index 5f75258..79f8d02 100644 --- a/progress.yaml +++ b/progress.yaml @@ -1,4 +1,4 @@ 1: stars: 2 2: - stars: 1 \ No newline at end of file + stars: 2 \ No newline at end of file diff --git a/src/day2/puzzle2.typ b/src/day2/puzzle2.typ index e69de29..22eeec5 100644 --- a/src/day2/puzzle2.typ +++ b/src/day2/puzzle2.typ @@ -0,0 +1,98 @@ +#import "/src/utils.typ": * + +#let is-safe(levels) = { + let increasing + let safe = true + for i in range(levels.len() - 1) { + let d = levels.at(i + 1) - levels.at(i) + let abs-d = calc.abs(d) + if abs-d < 1 or abs-d > 3 { + safe = false + break + } + if i == 0 { + increasing = d > 0 + } else if (d > 0) != increasing { + safe = false + break + } + } + return safe +} + +#let solve-bruteforce(input) = { + let safe-cnt = 0 + for line in input.split("\n") { + let nums = line.split(" ").map(n => int(n)) + if is-safe(nums) { + safe-cnt += 1 + } else { + for i in range(nums.len()) { + if is-safe(nums.slice(0, i) + nums.slice(i+1)) { + safe-cnt += 1 + break + } + } + } + } + return safe-cnt +} + +#let visualize(input) = { + let safe-cnt = 0 + for line in input.split("\n") { + let nums = line.split(" ").map(n => int(n)) + + let remove-i = none + if not is-safe(nums) { + for i in range(nums.len()) { + if is-safe(nums.slice(0, i) + nums.slice(i+1)) { + remove-i = i + break + } + } + } + + let cells = () + cells += nums.enumerate().map(((i, n)) => grid.cell( + colspan: 3, + fill: if i == remove-i {gray.transparentize(40%)}, + str(n) + )) + cells += (none,none,) + cells += range(nums.len() - 1).map(i => nums.at(i + 1) - nums.at(i)) + .map(n => { + let col = if calc.abs(n) in (1,2,3) {green} else {red} + grid.cell( + colspan: 2, + fill: gradient.linear( + angle: if n >= 0 {0deg} else {180deg}, + (white, 0%), + (white, 50%), + (col, 50%), + (col, 100%) + ), + str(n) + ) + }) + .intersperse(none) + + grid( + columns: (1fr,) * 3 * nums.len(), + inset: (x: 0.2em, y: 0.4em), + stroke: (x, y) => { + if y == 0 or (calc.rem(x, 3) == 2 and x != nums.len() * 3 - 1) { + black + .5pt + } + }, + ..cells + ) + } +} + +#show-puzzle( + 2, 2, + solve-bruteforce, + example: 4, + visualize: visualize +) \ No newline at end of file diff --git a/src/main.pdf b/src/main.pdf index 2f67d86..48d48dd 100644 Binary files a/src/main.pdf and b/src/main.pdf differ