diff --git a/progress.png b/progress.png index 22cb021..458d07a 100644 Binary files a/progress.png and b/progress.png differ diff --git a/progress.yaml b/progress.yaml index 25b5bfc..c105224 100644 --- a/progress.yaml +++ b/progress.yaml @@ -13,4 +13,4 @@ 7: stars: 2 8: - stars: 1 \ No newline at end of file + stars: 2 \ No newline at end of file diff --git a/src/day8/puzzle2.typ b/src/day8/puzzle2.typ index e69de29..66bfcd1 100644 --- a/src/day8/puzzle2.typ +++ b/src/day8/puzzle2.typ @@ -0,0 +1,65 @@ +#import "/src/utils.typ": * + +#let in-grid(w, h, x, y) = { + return 0 <= x and x < w and 0 <= y and y < h +} + +#let get-antinodes(in-grid, x1, y1, x2, y2) = { + let (dx, dy) = (x2 - x1, y2 - y1) + let f = calc.gcd(dx, dy) + dx /= f + dy /= f + + let walk(ox, oy, dx, dy) = { + let x = ox + let y = oy + let pos = () + while in-grid(x, y) { + pos.push((x, y)) + x += dx + y += dy + } + return pos + } + + let antinodes = walk(x1, y1, dx, dy) + walk(x1, y1, -dx, -dy) + + return antinodes +} + +#let solve(input) = { + let by-freq = (:) + let antinodes = () + + let grid = input.split("\n").map(l => l.clusters()) + let w = grid.first().len() + let h = grid.len() + + let in-grid = in-grid.with(w, h) + let get-antinodes = get-antinodes.with(in-grid) + + for y in range(h) { + for x in range(w) { + let c = grid.at(y).at(x) + if c == "." { + continue + } + if c not in by-freq { + by-freq.insert(c, ()) + } + + for (x2, y2) in by-freq.at(c) { + antinodes += get-antinodes(x, y, x2, y2) + } + by-freq.at(c).push((x, y)) + } + } + + return antinodes.dedup().len() +} + +#show-puzzle( + 8, 2, + solve, + example: 34 +) \ No newline at end of file diff --git a/src/main.pdf b/src/main.pdf index 16ab2a2..a80607f 100644 Binary files a/src/main.pdf and b/src/main.pdf differ