diff --git a/res/examples/day16_1.txt b/res/examples/day16_1.txt new file mode 100644 index 0000000..6a5bb85 --- /dev/null +++ b/res/examples/day16_1.txt @@ -0,0 +1,15 @@ +############### +#.......#....E# +#.#.###.#.###.# +#.....#.#...#.# +#.###.#####.#.# +#.#.#.......#.# +#.#.#####.###.# +#...........#.# +###.#.#####.#.# +#...#.....#.#.# +#.#.#.###.#.#.# +#.....#...#.#.# +#.###.#.#.#.#.# +#S..#.....#...# +############### \ No newline at end of file diff --git a/res/examples/day16_2.txt b/res/examples/day16_2.txt new file mode 100644 index 0000000..54f4cd7 --- /dev/null +++ b/res/examples/day16_2.txt @@ -0,0 +1,17 @@ +################# +#...#...#...#..E# +#.#.#.#.#.#.#.#.# +#.#.#.#...#...#.# +#.#.#.#.###.#.#.# +#...#.#.#.....#.# +#.#.#.#.#.#####.# +#.#...#.#.#.....# +#.#.#####.#.###.# +#.#.#.......#...# +#.#.###.#####.### +#.#.#...#.....#.# +#.#.#.#####.###.# +#.#.#.........#.# +#.#.#.#########.# +#S#.............# +################# \ No newline at end of file diff --git a/src/day16/puzzle1.typ b/src/day16/puzzle1.typ new file mode 100644 index 0000000..dd47e2c --- /dev/null +++ b/src/day16/puzzle1.typ @@ -0,0 +1,67 @@ +#import "/src/utils.typ": * + +#let START = "S" +#let END = "E" +#let WALL = "#" +#let EMPTY = "." + +#let offsets = ( + (1, 0), + (0, 1), + (-1, 0), + (0, -1) +) + +#let solve(input) = { + let grid = input.split("\n").map(l => l.clusters()) + let w = grid.first().len() + let h = grid.len() + + let (sx, sy) = (0, 0) + let (ex, ey) = (0, 0) + for y in range(h) { + for x in range(w) { + let c = grid.at(y).at(x) + if c == START { + (sx, sy) = (x, y) + } else if c == END { + (ex, ey) = (x, y) + } + } + } + + let choices = ((sx, sy, 0, 0),) + let (x, y, dir, score) = (0, 0, 0, 0) + while choices.len() != 0 { + let min-score = calc.min(..choices.map(c => c.last())) + let i = choices.position(c => c.last() == min-score) + (x, y, dir, score) = choices.remove(i) + for (d, (dx, dy)) in offsets.enumerate() { + // Ignore backflips + if calc.abs(d - dir) == 2 { + continue + } + let (x2, y2) = (x + dx, y + dy) + let c = grid.at(y2).at(x2) + if c == WALL { + continue + } + let score2 = score + 1 + if d != dir {1000} else {0} + if c == END { + return score2 + } + choices.push((x2, y2, d, score2)) + } + break + } + return 0 +} + +#show-puzzle( + 16, 1, + solve, + example: ( + "1": 7036, + "2": 11048 + ) +) \ No newline at end of file diff --git a/src/day16/puzzle2.typ b/src/day16/puzzle2.typ new file mode 100644 index 0000000..e69de29 diff --git a/src/main.pdf b/src/main.pdf index 558952d..62d3fc1 100644 Binary files a/src/main.pdf and b/src/main.pdf differ