diff --git a/progress.png b/progress.png index 48e5913..233e9c1 100644 Binary files a/progress.png and b/progress.png differ diff --git a/progress.yaml b/progress.yaml index 6c28326..49ee180 100644 --- a/progress.yaml +++ b/progress.yaml @@ -17,4 +17,4 @@ 9: stars: 2 10: - stars: 1 \ No newline at end of file + stars: 2 \ No newline at end of file diff --git a/src/day10/puzzle2.typ b/src/day10/puzzle2.typ index 837b232..8465f1b 100644 --- a/src/day10/puzzle2.typ +++ b/src/day10/puzzle2.typ @@ -1,11 +1,74 @@ #import "/src/utils.typ": * +#let offsets = ( + (-1, 0), + (0, -1), + (1, 0), + (0, 1) +) + +#let in-grid(w, h, x, y) = { + return 0 <= x and x < w and 0 <= y and y < h +} + +#let count-paths(grid, w, h, ox, oy) = { + let in-grid = in-grid.with(w, h) + let rating = 0 + + let to-visit = ((ox, oy),) + while to-visit.len() != 0 { + let (x, y) = to-visit.remove(0) + let v = grid.at(y).at(x) + let branches = 0 + for (dx, dy) in offsets { + let (x2, y2) = (x + dx, y + dy) + if not in-grid(x2, y2) { + continue + } + let v2 = grid.at(y2).at(x2) + if v2 == v + 1 { + let pos2 = (x2, y2) + branches += 1 + if v2 != 9 { + to-visit.push(pos2) + } + } + } + rating += if v == 0 { + branches + } else { + branches - 1 // If no branch -> -1 + } + } + + return rating +} #let solve(input) = { + let grid = input.split("\n").map(l => l.clusters().map(int)) + let w = grid.first().len() + let h = grid.len() + + let count-paths = count-paths.with(grid, w, h) + + let ratings = () + let total = 0 + for y in range(h) { + for x in range(w) { + if grid.at(y).at(x) == 0 { + let rating = count-paths(x, y) + total += rating + ratings.push(rating) + } + } + } + let a = ratings + + return total } #show-puzzle( 10, 2, solve, - example: 0 + example: 81 ) \ No newline at end of file diff --git a/src/main.pdf b/src/main.pdf index 4559a90..063ee1f 100644 Binary files a/src/main.pdf and b/src/main.pdf differ