From ad2fd882e4cab59b529acfcaa6234389d976a288 Mon Sep 17 00:00:00 2001 From: Florian Cassayre Date: Mon, 16 Dec 2024 20:23:53 +0100 Subject: [PATCH] Solved day 16 --- README.md | 2 +- input/16.txt | 141 ++++++++++++++++++ output/16-1.txt | 1 + output/16-2.txt | 1 + .../scala/adventofcode/solutions/Day16.scala | 78 ++++++++++ 5 files changed, 222 insertions(+), 1 deletion(-) create mode 100644 input/16.txt create mode 100644 output/16-1.txt create mode 100644 output/16-2.txt create mode 100644 src/main/scala/adventofcode/solutions/Day16.scala diff --git a/README.md b/README.md index b7b1a04..e9cb062 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,7 @@ _My solutions to the 2024 edition of [Advent of Code](https://adventofcode.com/2 | **[13](https://adventofcode.com/2024/day/13)** | [solution](src/main/scala/adventofcode/solutions/Day13.scala) | | **[14](https://adventofcode.com/2024/day/14)** | [solution](src/main/scala/adventofcode/solutions/Day14.scala) | | **[15](https://adventofcode.com/2024/day/15)** | [solution](src/main/scala/adventofcode/solutions/Day15.scala) | -| **[16](https://adventofcode.com/2024/day/16)** | [](src/main/scala/adventofcode/solutions/Day16.scala) | +| **[16](https://adventofcode.com/2024/day/16)** | [solution](src/main/scala/adventofcode/solutions/Day16.scala) | | **[17](https://adventofcode.com/2024/day/17)** | [](src/main/scala/adventofcode/solutions/Day17.scala) | | **[18](https://adventofcode.com/2024/day/18)** | [](src/main/scala/adventofcode/solutions/Day18.scala) | | **[19](https://adventofcode.com/2024/day/19)** | [](src/main/scala/adventofcode/solutions/Day19.scala) | diff --git a/input/16.txt b/input/16.txt new file mode 100644 index 0000000..67000f9 --- /dev/null +++ b/input/16.txt @@ -0,0 +1,141 @@ +############################################################################################################################################# +#.....#.....#...#.................#...#.......#.........#...#.......#...#.......#.......#...#...#.....#...#.......................#...#....E# +#.###.#.#.#.###.#.###.###.#######.#.#.#.#.###.#.###.###.#.#.#.###.#.###.#.#.#####.###.#.###.#.#.#.#.#.#.#.#.#####.#.###.#.#######.#.#.#.###.# +#...#.#.#.#...#.....#...#.#.....#...#...#.#...#.#...#...#.#.#...#.#...#.#.#...#...#...#.....#.#.#.#.#...#...#.....#.#.#.....#.....#.#.#...#.# +#.#.###.#.###.#.#####.#.#.###.###########.#.#####.###.###.###.###.###.#.#.#.#.#.###.#######.#.#.#.#.#########.#####.#.#.#####.#####.#.###.#.# +#.#.....#.#.#.#...#...#.#...#.....#.....#.#.....#.#.#.#.........................#.#.......#.#.#...#.....#...#.#...#.....#...#.#...#.#.....#.# +#.#######.#.#.#.###.#.#.###.#.#.#.#.#.###.#####.#.#.#.#.###.###.###.#.###.#.#.#.#.#######.###.#.#####.#.#.#.#.###.###.#.#.#.#.#.#.#.#######.# +#...#...#.#...#.#...#.#...#.#.#.#...#.#.......#...#...#...#...#.#.....#...#.#...#.......#...#...#.....#...#.#.....#...#...#...#.#.........#.# +###.#.###.#.#####.###.###.#.###.#.#####.###.#.#####.###.#.###.#.###.###.###.#####.#########.#.#.###.#.###.#.#####.#.###.#######.###########.# +#...#...#.#.#.....#...#...#.#...#.#...#.#...#.#...#...#.#.....#...#...#.#...........#.........#.#...#.#...#.........#.#.#.......#...........# +#.#####.#.#.#.#######.#.###.#.###.#.#.#.#.###.#.#####.#########.#.#.###.#.#####.###.#.#####.#.#.#.###.###.###.#####.#.#.#.#######.#########.# +#.#.....#.#...#.......#...#.#.#.....#.#...#.#.#...............................#...#.#...#...#...#.#.#.....#.#.....#...#.#.#.....#.......#...# +#.#.###.#.#####.#.###.###.#.#.#.#.###.#.###.#.###############.#.#.#.#.#####.#.#####.###.#####.#.#.#.#####.#.#####.###.#.#.#.###.#######.###.# +#.#...#.#.....#.#.....#...#...#.#...#.#.....#.#.....#.................................#...#...#...#...#...................#...#...#...#...#.# +#.###.#.#####.###.#.#.#.#####.#.#.#.#.#######.#.###.#.#######.###.#.#.#.#.###.#.#########.#.#.#.#.#.###.#.#####.#.###.#.#.###.#.#.###.###.#.# +#.#...............#.#...#...#.#...#.........#.....#.#.#.......#...#.#.#.#.#...#.#.....#...#.#.....#.....#.....#.#.....#.#.#...#.#...#...#...# +#.#.#####.#.#####.#.#####.#.#.###.#########.#.#####.#.#.#######.###.#.###.#.###.#.###.#.#.#.#.#########.#.###.#########.#.#####.###.###.###.# +#.#.#.....#.....#.#.#.....#.....#.....#...#.#.#.....#.#.#.......#.#.#.....#.#...#.#...#.#.#.#.........#...#.#.........#.#.........#...#...#.# +#.#.#.#####.#####.#.###.#######.#####.###.#.###.#####.#.#####.###.#.#######.#.#.#.#.#.#.###.#.#######.#.###.###.#.###.#.#########.###.#.#.#.# +#.#.......#...#...#...#...#...#.....#...#.#.#...#.#.....#.....#.....#...#...#.#...#.#.#.....#.#.......#.#.......#...#.#.....#.......#.#.....# +#.#######.###.#.#####.#.###.#.#########.#.#.#.###.#.#####.###.#######.#.#.#########.#.###.###.#.#######.#.#####.###.#.#####.#.###.###.#.#.#.# +#.....#...#...#...#.#.#.#...#.#.........#.#...#...#...#...#.#.#.......#...#.......#.#.#.......#.........#.........#.#...#.#.#...#.....#.#.#.# +#####.###.#.#.###.#.#.###.###.#.#########.#####.#####.#.#.#.#.#.#############.###.#.#.###########.#######.#######.#.#.#.#.#.###.#######.#.#.# +#...#...#.#.#.#...#.......#.#.#.#.......#.......#.....#.#.#...#.............#.#...#.#...#.......#.........#...#...#...#...#...#...#.....#.#.# +#.#.###.#.#.#.#.###########.#.#.###.###.#.#.#####.#####.#.#####.#.#########.#.#.###.###.#.###.#.###########.###.#####.###.#.#####.#.#####.#.# +#.#.#.#.#.#.#.#.....#.....#...#...#.#.....#.#.....#.#...#.....#.#.#.......#.#.#...#.#.....#...#.#.....#.....#...#.......#.#.................# +#.#.#.#.###.#.#####.#.###.#.#####.#.#######.#.#####.#.#####.#.#.#.#####.###.#.###.#.#########.#.#.###.#.###.#.###.#####.#.###.###.###.#.###.# +#.....#.#...#.....#...#...#...#...#.#.....#.#...#.#...#.....#...#.#.....#...#.#...#...#.......#...#...#...#.#.#...#.....#.#.#.........#...#.# +#.#####.#.#######.#####.#####.#.###.#.###.#####.#.#.#####.#.#####.#.#####.###.#.#####.#.###.#######.#######.#.###.#.#####.#.###############.# +#.#.....#...#...#...#...#.....#.#...#.#.#.......#.#.......#.....#.#.....#.#...#.......#.#...#.......#.....#.#.....#.....#.......#.........#.# +#.#.#######.#.#.###.#.#######.#.#.###.#.#########.#############.#.#.###.#.#.###.#########.#.#.#########.#.#.#####.#####.#####.###.#######.#.# +#...#.....#...#...#.#.......#...#...#...#.........#.......#...#...#...#.#.#...#.#...#...#.#.#.#.....#...#.#.....#.....#.....#.#...#.....#...# +#.###.#.#.#.#####.#########.###.###.###.###.###.#.#.###.#.#.#.#########.#.#.#.###.#.#.#.#.###.#.###.#.###.#.#.#######.#####.#.#.###.#.#####.# +#.#.....#.#.#...#...#.......#.............#.#.#.#.#.#...#...#.#.........#.#.#.....#.#.#.#.#.....#.....#.#.#.#...........#...#.#...#.#.#...#.# +#.#.#.###.###.#.###.#.#########.#####.###.#.#.#.#.#.#.#########.#.#####.#.#########.#.#.#.#.#######.###.#.###########.###.###.###.#.###.#.#.# +#...#...#.....#.....#.#.........#.....#...#.....#.#.#.#.....#...#.#...#.#.#.......#...#.#...#.#...#...#...#.............#.#...#...#.....#.#.# +#####.#.#######.#####.#.#.#############.#####.#.###.#.#.###.#.###.#.#.###.#.#####.#####.#.###.#.#.#.#.#.###.###########.#.###.#.###.#####.#.# +#.#...#.......#.#.....#.#.#...#.............#.#.#...#.....#.....#...#...#.#...#.#...#...#.#...#.#.#.#.#...#.#...#.....#.#.#.....#...#...#...# +#.#.#.###.#####.#.#####.###.#.#.###########.###.#.#############.#######.#.###.#.###.#.###.#.###.#.#.#.#.#.#.#.#.#.#####.#.#.#.###.###.#.##### +#...#...#...#...#.#.#.......#.#.#.......#.....#.....#.........#.........#...#.#.....#...#...#...#.#.#.#.....#.#.#.........#.#.#...#...#.#...# +#.###.#.###.#.###.#.#.#######.#.#.#####.#####.#.#####.#######.#.#####.#.###.#.#.#######.###.#.###.#.###.###.#.#.#.#########.#.#.#####.#.#.#.# +#.......#...#...#.#.........#...#...#.#.....#.#...#...#.#.....#.#.....#...#...#...#...#.....#...#.#.....#.....#.#.#.........#.#.......#.#.#.# +#.###.#.#.###.#.#.###.#########.###.#.#####.#.#####.###.#.#######.#######.#######.#.#.#.###.###.#.#.#####.#####.#.#.#########.#########.#.### +#.#.....#.....#.#...#.#.......#.#...#.....#.#.......#.....#.......#...#.....#...#...#.#.#...#...#.#.#.....#...#.#.#.#.........#.....#...#...# +#.#.#.#######.#.#.#.###.#.###.###.###.#####.#.#.###.#.#########.#.#.#.#.#####.#.###.#.###.#.#.###.###.#####.#.#.###.#.#####.###.###.#.###.#.# +#.#.#.....#...#.#.#.#...#...#.#...#.....#...#.........#.........#.#.#.#.#.....#.....#.#...#...#.#...........#.#...#.#.#...#.......#.#...#.#.# +#.#.#.###.#.#.#.#.#.#.#####.#.#.#######.#.###.#########.#.###.#####.#.###.#####.#####.#.###.###.#######.#########.#.###.#.#####.#.#.###.###.# +#.#.#.....#.#.#.#.#.#.......#.#.#...#...#.#.#...#.#.....#.#...#.........#...#...#...#.#.#...#...#.....#.#.....#...#.....#.#.....#.#.........# +#.#.#.###.#.#.#.#.#.#.#.#.#.#.#.#.#.#.###.#.###.#.#.#####.#####.###.###.###.#.###.#.#.#.#.###.#.#.###.###.###.#.#.#####.#.#.###.#.#########.# +#.#.......#.#.#...#.#.#.#.#.#.#...#.#.#...#.#...#...#...#.#...#...#...#...#.#.....#.#.#.#.#...#.#.#...#...#.#.#.#.........#...#.#.....#...#.# +#.#######.#.#.###.#.#.#.#.###.#####.#.#.###.#.#####.#.#.#.#.#.###.###.###.#.#######.#.#.#.#.#####.#.###.###.#.#.#####.#.#####.#.#######.#.#.# +#.#.......#.#...#.#.#...#...#.#.......#.#...#.....#.#.#...#.#...#.....#.#.#.......#...#.....#.....#...#.#.....#.....#.#.#.....#.........#.#.# +#.#.#####.#.###.#.#.#.#.###.#.#.#######.#.#.#####.#.#.###.#.###.#.###.#.#.#######.###########.#######.#.#.#########.###.#.###############.#.# +#.#...#...#.#...#.#...#.#...#...#.#.....#.#.#.....#.#.#...#...#.........#...................#.#.....#.#.#.........#...#...#.......#.......#.# +#.###.#.#.#.#####.###.###.#.#####.#.#####.#.#.#####.#.#.#####.#########.###################.#.#.#####.#.###.###.#.###.#####.#######.#######.# +#...#...#...#.....#.......#.....#.#.#.....#...#.........#...#...#.....#...#...........#.#.....#.......#...#.....#.......#...#.....#.#.....#.# +###.###.#.###.#####.###########.#.#.#.###########.#.#####.#.###.#.###.###.#.#########.#.#.#######.#####.#.###########.#.#.#.#.###.#.###.###.# +#...#...#...........#.#.....#.....#.#.#.......#...#.#.....#.....#.#.#.#...#.#.#.....#.#.....#...#.....#.#.................#.#.#.#.#...#.....# +#.#.#.#####.###.#####.#.###.#.###.#.#.###.###.#.#####.#######.###.#.#.#####.#.#.#.###.#.#####.#.#####.#####.#########.#####.#.#.#.#.#.###.### +#.#.#.#...#.#.......#.....#.....#.#.#.#...#.#.#.......#.....#...#.#.#...#.......#.....#...#...#.#...#.....#.........#...#...#.#...#.#...#...# +#.###.#.#.###.###.#.###########.#.#.#.#.###.#.#.#.#####.###.###.#.#.###.#.#########.#######.###.###.#####.#########.###.#.###.#.#######.##### +#.#...#.#.....#...#.#.........#...#.#...#...#.#.....#...#...#...#.#.#...#.........#.........#.#.#...#...#...#.....#...#.#...#.#...#...#.....# +#.#.###.#.#####.#.#.#.#######.#.###.#####.###.#.###.#.###.#######.#.#.###.#######.###########.#.#.###.#.###.#.#####.###.#####.###.#.#.#.###.# +#...#...#.....#.#...#.#.....#.#.#...#.....#...#.....#...#.........#.#.#.........#.......#...#...#.....#...#.#...........#...#...#...#.......# +#####.###.#.###.#####.#.#.###.###.###.#####.#######.###.###.#######.#.#########.#.#.###.###.#.#########.###.#########.###.#.###.#######.###.# +#.....#...#.....#.....#.#.......................#...#...#.........#.#.......#...#.#...#...#.#.......#...#...#.........#...#...#.......#.....# +#.#####.#.#######.#################.#########.#.#.###.###.#####.#.#.#######.#####.###.###.#.#.#.###.#.#.#.###.###.#####.#####.#######.#.##### +#.#.#...#...#.................#.#...#.........#.......#.#.#.#...#...#...#...#...#.......#.#.#.#...#...#.#.....#...#...#...#.........#.......# +#.#.#.#.#.###.###.###########.#.#.###.#################.#.#.#.#####.#.#.#.###.#.#########.#.#.###.#####.#######.###.#.###.#########.###.###.# +#.#.#...#.........#.....#.....#.#.#.....#...#.............#.#.#.....#.#.......#.........#.#.#...#.........#.......#.#...#...#.....#.#.....#.# +#.#.###.#.#######.#####.#.#####.#.#.###.#.#.###.###.###.#.#.#.#######.#################.#.#.###.#.#.#####.#.#######.###.###.#.###.#.#.###.#.# +#.#...#.#.....#.........#.#.....#.#.#...#.#...#...#...#.#.....#.......#.........#.......#.#...#.#.#.....#.#.#...#...#...#...#...#.#...#.#.#.# +#.#.###.#.###.###########.#.###.#.###.###.###.#######.###.#.#.#.###############.#.#######.#.#.#.###.###.#.#.#.#.#.###.#.#.#####.#.#####.#.#.# +#.#.....#.#...............#.#...#...#...#...#.#.....#.......#...........#.....#.#.#.......#.#.#.....#.#.#.#...#.#.#.......#.....#.......#...# +#.#.###.#.#.#################.###.#.#.#.###.#.#.###.#######.###########.#.###.#.#.#.###.#.###.###.#.#.#.###.#####.#.###########.#.#####.###.# +#.#...#...#.#...#...#.........#...#.#.#...#.#...#.#.#.....#.#.....#...#.#...#...#...#.#...#...#...#...#.....#.....#.....#.......#.....#...#.# +#.###.#.#####.#.#.#.#####.#.###.#.#.###.#.#.#####.#.#.###.###.###.#.#.#.###.#########.#####.#.#.#####.###.#.#.#######.###.#####.#####.###.#.# +#.#...#.......#.#.#...#...#.#...#...#.....#...#...#...#.#.....#.#...#.#...#.#.#.......#.......#.....#.......#.#.#.....#...#...#.#.....#...#.# +#.#.###.###.###.#.###.#.###.#.#######.#.#####.#.#.#####.#######.#####.###.#.#.#.###.#.#.#####.#####.#########.#.#.#####.###.#.#.###.###.#.#.# +#.#.#.....#.....#.#.....#.#.#.........#.......#.#.........#.............#.#.#.....#.#.#.#.....#.....#.......#.#...#...#.#...#.#...#...#.#.#.# +#.#.#.#.#.#.#####.#######.#.#####.#####.#.#########.###.#.#.#####.#####.#.#.#######.#.###.###.#.#.###.#####.#.###.#.#.#.###.#.#.#.#.#.#.#.#.# +#.#...#.#...#...............#...#.#...#.#.#.......#...#.#.#...#.......#.#.#.#.......#.#...#.#.#.#.......#...#.......#.......#...#.#.#...#.#.# +#.#####.#.#.###.#######.#####.#.#.#.#.###.#.#####.#####.#.#.#.#.#.###.#.#.#.#.#######.#.###.#.#.#######.#.###.###########.#####.#.#.#######.# +#.....#...#...#.#...#...#.....#...#.#.#...#.#...........#.#...#.#...#.#.#.#.#...#...#...#.#...#.........#.#.....#...#.............#.#.......# +#.#.#.#.###.#.###.#.#.###.#.#######.#.#.###.#.#.#########.#.#.#.###.#.#.#.#.#.#.#.#.#####.#.#########.###.#.#####.#.#.#########.###.#.####### +#.#.#.......#.#...#.#.....#.......#.#...#...#.#.#.......#...#.#.#...#...#.#...#...#.#.........#.......#...#...#...#.#.#...#.....#...#...#...# +#.#.###.#####.#.###.#.#########.###.#######.#.###.#####.###.###.#.###.#######.#####.#.#######.###.#.###.#####.#.###.#.#.###.###.#.#.###.###.# +#...#...........#.#.#...........#...#.......#.........#.....#...#...#.#.....#.....#.......#...#...#.#...#.....#.#.....#...#...#.#.#...#.....# +#.###.#.###.#.#.#.#.###########.#.###.#.#################.###.#####.#.#.###.#####.#.#####.#.###.###.#.#####.###.#######.#.###.#.#.###.#####.# +#.....#.....#.....#.......#.....#...#.#...#.............#...#.....#.#.#...#.....#.#.....#.#.....#...#.#...#...#.....#...#.....#.#.#.#.....#.# +#.#######.###########.###.#.#######.#.###.#.###.#.#####.#########.#.#.###.###.#.#.###.###.#######.###.#.#.#.#######.#.#########.#.#.###.#.#.# +#.#.....#.......#...#...#.#...#.........#...#.#...#...#...........#.....#...#.....#...#...#.........#...#.#.#.......#.#.#.....#...#...#.#.#.# +#.#.###.#########.#.###.#.###.#.#######.#####.#.###.#########.#.#.#########.#######.###.###.#######.#####.###.#######.#.#.###.#.###.#.#.###.# +#.#.#.#...........#.#...#.#...#...#...#...........#.....#...#.#.#...#.....#.....#.....#.#.....#...#.#...#.....#.....#.#.....#.......#.#.#...# +#.#.#.#############.#####.#.#####.#.###.#.#########.###.#.#.#.#####.#.#.#####.#.#######.###.###.#.#.#.#.#######.###.#.#############.###.#.### +#.#.#...#...#.......#.....#.#.#...#.#...#.......#.....#.#.#.#.....#...#.....#...#.....#.....#...#.#...#...#.....#...#.............#...#...#.# +###.#.###.#.#.#######.#####.#.#.#.#.#.#####.###.#.###.#.#.#.#.#.#########.#.#.###.###.###.#.#.###.#.#####.###.###.###############.###.#####.# +#...#...#.#.#...#...#.........#.#.........#...#...#...#.#.#.#.#.............#.....#.#...#.#...#...#.#...#.....#...#.......#.....#...#.......# +#.###.#.#.#.#.#.#.#.###.#####.#.#########.#.#.#####.#.#.#.#.#######.###.#.#########.###.#.#####.#.###.#.#######.###.#####.#.#.#.###.#.#####.# +#.................#...#.....#.#.....#.#...#.#.....#.#.#.#.#.........#...#.....#.......#.#.#.....#.#...#.#.........#.....#.#...#.#.#...#...#.# +#.#.#.#.#####.#.#.###.#####.#######.#.#.###.#####.#.#.#.#.#########.#######.#.#.#####.#.#.#.#####.#.###.#.#.#####.#####.###.#.#.#.#####.###.# +#...........#...#.#.........#...#...#.#...#.....#...#.#.#...#.....#.#.....#.#...#.#...#.#.#.#.....#.#.#.#.#.#...............#.#.......#.....# +#.###.#.###.#.###.#########.#.#.#.###.###.#####.#####.#.###.#####.#.#.###.#.###.#.#.#.#.#.#.#.#####.#.#.###.#.###########.###.#########.##### +#.#.........#.....#.........#.#.#.#.#.....#...#...#.#.....#.....#...#.#...#.#.....#...#.#.#.#.#.....#.#...#.#.#...#.......#.#...#.......#...# +#.###.#.#.###.###.#.#####.###.#.#.#.#.#####.#.#.#.#.#####.#####.#.###.#.###.#.###.###.#.###.#.#.#####.###.#.#.#.###.#######.###.#.#########.# +#.......#...#.......#...#.#...#.#.#...#.....#.#.#.#.....#.#...#.#...#.#.#...#...#...#.#...#.#.#.#.......#.#.....#...#.....#...#...#.#.......# +#####.#.###.#.#####.#.###.#.###.#.###.###.###.###.#.###.#.#.#.#.###.#.#.###.###.#####.###.#.#.#.#.#.###.#.#######.#####.#.###.#####.#.###.#.# +#.......#.....#.....#...#.#.#...#...#.....#.#.#...#...#.#.#.#.#.#...#.#.......#.#.....#...#.#.#.#.#...#.#...#.....#.....#...#...#.......#...# +#.#####.#######.#####.#.#.#.#.#####.#######.#.#.#####.#.#.###.#.#####.#.#.###.#.#.#####.###.###.###.#.#.###.#.#####.#######.#.###.#########.# +#.#.....#.......#.....#.#.#.#.......#...........#...#.#.#.....#.....#...#.....#.#.....#.#.#...#.....#.....#.........#.....#.#.....#.....#...# +#.#.###.#.#######.#.###.#.#.#########.###########.#.#.#.#####.#####.###.#######.#.#####.#.#.#.#######.#####.###.#.###.#.###.#.#.###.###.#.#.# +#.#.#.....#.....#.#...#.#.#.....#...#...#...#.#...#.#.#.....#.#.......#.....#.....#...#.#.#.#.........#.....#.#.#...#.#.....#...#...#.#...#.# +#.#.#.#.###.#####.#.#.#.#.#####.#.#.#####.#.#.#.###.#.###.###.#.###.#.#####.#######.#.#.#.#.###.#####.#.###.#.#.#.#.#.###.#.###.#.###.#####.# +#.#.#.#.....#...#...#.#.#.....#...#.......#.#.#.#.#.#.#.#.....#.#.......#.#.#.....#.#.....#.#.......#.#.#...#.#.#...#...#.#...#...#.......#.# +#.#.#.#.#####.#.#.#.#.#.###.###.###########.#.#.#.#.#.#.#######.#.#####.#.#.#.###.#.#####.#.#.#####.#.#.#.###.#.#.#.#####.###.#.###.#####.#.# +#...#.#.......#.#...#.#.....#...#.........#.#.#...#.#.......#.#.#.#...#...#...#...#...#...#.#.....#...#.#.....#.#.#.....#...#...#.#.#.......# +#.###.#.#######.#####.###.#.#.#.#.#####.###.#.###.#.#######.#.#.###.#.###.#####.#.###.#.#.#.###.#.###.#.#####.#.#.#####.#.#.###.#.#.#.#.###.# +#.#...#.#...#...#...#...#.#...#.#.....#.....#.....#.......#...#.....#.#.#.#.....#...#.#.#...#...#.#.......#...#.#.....#.#.........#.#.#.....# +#.#.###.#.#.#.#.#.#.###.#.#####.#.#.#.#######.#####.#.#####.#.#######.#.#.#.#########.#.#####.#.#.#.#####.#.#.#.#####.#.###.#.#.###.#.#####.# +#.#.#...#.#.#.#.#.#.....#...#...#...#.#.......#...#.#.#.....#...#...#.#...#.....#...#.#.......#.#.........#.#.#.......#...#.#...#...#.......# +#.#.#.###.#.#.#.#.#######.###.#.#####.#########.#.###.#.#####.#.###.#.#########.#.#.#.###.#.#####.#########.#.#######.#.#.#####.#.#########.# +#...#.....#.#.#.#...#...#...#.#.....#.........#.#...#.#.#.....#.....#.#.........#.#...#.#.#.....#.#...#.....#.......#.#.#...#...#.#.....#...# +###########.#.#.###.#.#.###.#.###.###########.#.###.#.#.#.#######.###.#.#######.#.#####.#.#.###.#.#.###.###.#######.#.#.###.#.#.#.#.#.#.#.#.# +#.#.......#.#.#...#.#.#...#.#.....#.......#.#.#.......#.#.........#...#.........#.....#...#.#.....#.#.....#.......#.#.#...#.#.#...#.#.#.#.#.# +#.#.#####.#.#.###.#.#.#.#.#.#####.#.#.###.#.#.#.#.###.#.###.###.#.#.#.#######.#####.#.#.###.###.###.#.#####.#####.#.#.###.#.#.#.#####.#.#.#.# +#.......#.#.#...#.#.#.#.#.#...#.....#...#...#.#.#.#...#...#...#...#.#.#.....#.#.........#.#...........#...#...#.#...#.#...#.#.......#.#...#.# +#########.#.###.#.#.#.#.#.#.#.#.#####.#.#.###.#.#.#######.#.#.#####.#.#.#.###.#.#####.###.#####.#######.#.###.#.#####.#.###.#######.#.#####.# +#.......#...#...#.#.#.#.#.#.#.........#...#...#.#.#...#...#.#.....#.#.#.#...#...#...#.#...#...#.......#.#.#.........#.#...#...#.#...........# +#.#####.#####.###.#.#.#.###.#########.###.#.#####.#.#.#.#########.#.#.#.###.#####.#.#.#.#.#.#.#.#####.#.#.###########.#######.#.#.###.#.##### +#...#.........#...#.#.#...#.#.......#...#...#.....#.#...#.........#.#.#...#.#.....#.#.#.#.#.#.......#.#.#.........#...#.....#.#.............# +#.#.###########.###.###.#.#.#######.#.#.#####.#####.#####.#######.#.#.#.#.#.#.###.#.#.#.#.#.#######.#.#.#########.#.###.###.#.#.#.#####.###.# +#.#.#.......#.............#.....#...#.....#.......#.#.....#...#...#.#.#...........#.#.#.#.#.#...#...#...#.#.......#.#...#.......#.........#.# +###.#.#####.###.#.###.###.###.#.#.###.#.###.#####.#.#.###.#.#.#.###.#.#.#######.###.#.###.#.###.#.#######.#.#######.#.#.#######.#######.#.#.# +#...#.....#...#.....#.#...#...#.#.....#...#...#...#.#.#...#.#.#...#.#.#...#.#...#...#...#.#.....#.....#.......................#...#.....#.#.# +#.#######.###.#.###.#.#.#.#.#.#.#.###.#.#.###.#.###.#.#.###.#.###.#.#.###.#.#.#########.#.###.#####.#.###.#.#.###.#.#########.#.#.#.###.#.#.# +#.......#.#.#...#...#...#...........................#.#.#...#.#...#.#...#...#.....#...#.#.....#.....#...#.#.#...................#.#.#...#...# +#.#.###.#.#.###.#.#.#########.#.###.#.#.###.#.#########.#.#.###.###.#######.#####.#.#.#.#.#####.#######.#.#.###.#.#########.#.#.###.#.#.###.# +#.#.#.#...#...#.#.#.......................#.#.#.........#.#.#...#.#.#.....#.#...#...#...#...#...#.......#.#.....#...#.......#.#.#...#.....#.# +#.#.#.#####.#.#.###.###########.#########.#.#.#####.#####.#.#.###.#.#.#.###.#.#.#############.###.#######.#########.#.#######.#.#.#####.#.#.# +#S..........#.#...........................#.........#.....#.......#...#.......#...............#...................#...........#.........#...# +############################################################################################################################################# diff --git a/output/16-1.txt b/output/16-1.txt new file mode 100644 index 0000000..5d04ad1 --- /dev/null +++ b/output/16-1.txt @@ -0,0 +1 @@ +123540 \ No newline at end of file diff --git a/output/16-2.txt b/output/16-2.txt new file mode 100644 index 0000000..b7f636c --- /dev/null +++ b/output/16-2.txt @@ -0,0 +1 @@ +665 \ No newline at end of file diff --git a/src/main/scala/adventofcode/solutions/Day16.scala b/src/main/scala/adventofcode/solutions/Day16.scala new file mode 100644 index 0000000..ecde0d0 --- /dev/null +++ b/src/main/scala/adventofcode/solutions/Day16.scala @@ -0,0 +1,78 @@ +package adventofcode.solutions + +import adventofcode.Definitions.* + +@main def Day16 = Day(16) { (input, part) => + + case class Vec(i: Int, j: Int): + infix def +(that: Vec): Vec = Vec(i + that.i, j + that.j) + infix def rotateCW: Vec = Vec(-j, i) + infix def rotateCCW: Vec = Vec(j, -i) + + case class State(position: Vec, direction: Vec) + + val (grid, initialPosition, finalPosition) = + val lines = input.toLines + def find(c: Char): Vec = lines.zipWithIndex.flatMap((row, i) => row.zipWithIndex.collect { case (`c`, j) => Vec(i, j) }).head + (lines.map(_.map { + case '#' => false + case _ => true + }), find('S'), find('E')) + + val adjacent = + for + i <- -1 to 1 + j <- -1 to 1 + if i.abs + j.abs == 1 + yield Vec(i, j) + + def walkable(p: Vec): Boolean = grid(p.i)(p.j) + + val initialState = State(initialPosition, Vec(0, 1)) + + def shortestPathDijkstra(adjacency: State => Set[(State, Int)], source: State, earlyStopping: Set[State] = Set.empty): (Map[State, Int], Map[State, Set[State]]) = + import scala.collection.immutable.TreeSet + def search(queue: TreeSet[(State, (Int, Int))], distances: Map[State, Int], predecessors: Map[State, Set[State]], visited: Set[State], k: Int): (Map[State, Int], Map[State, Set[State]]) = + queue.headOption match + case Some((u, (du, _))) => + if earlyStopping.contains(u) then + (distances, predecessors) + else if !visited.contains(u) then + val newVisited = visited + u + val edges = adjacency(u).filter { case (b, w) => !visited.contains(b) && (!distances.contains(b) || distances(b) > du + w) } + val (newQueue, newDistances, newPredecessors, newK) = edges.foldLeft((queue.tail, distances, predecessors, k)) { case ((currentQueue, currentDistances, currentPredecessors, currentK), (v, w)) => + val newDistance = du + w + val newPredecessors = currentPredecessors + (v -> (currentDistances.get(v).filter(_ > newDistance).flatMap(_ => currentPredecessors.get(v)).getOrElse(Set.empty) + u)) + (currentQueue + (v -> (newDistance, currentK)), + currentDistances - v + (v -> newDistance), + newPredecessors, + currentK + 1) + } + search(newQueue, newDistances, newPredecessors, newVisited, newK) + else + search(queue.tail, distances, predecessors, visited, k) + case None => (distances, predecessors) + search(TreeSet((source, (0, 0)))(Ordering.by(_._2)), Map(source -> 0), Map.empty, Set.empty, 1) + + def adjacency(state: State): Set[(State, Int)] = + Set(state.position + state.direction).filter(walkable).map(p => state.copy(position = p) -> 1) ++ + Set(state.direction.rotateCW, state.direction.rotateCCW).map(d => state.copy(direction = d) -> 1000) + + val finalStates = adjacent.map(d => State(finalPosition, d)).toSet + + val (distances, predecessors) = shortestPathDijkstra(adjacency, initialState, finalStates) + + part(1) = finalStates.flatMap(distances.get).head + + def reconstructPath(target: State, predecessors: Map[State, Set[State]]): Set[State] = + def reconstruct(set: Set[State], acc: Set[State]): Set[State] = + if set.nonEmpty then + val newAcc = acc ++ set + reconstruct(set.flatMap(predecessors.get).flatten.diff(newAcc), newAcc) + else + acc + reconstruct(Set(target), Set.empty) + + part(2) = finalStates.filter(distances.contains).map(reconstructPath(_, predecessors)).head.size + 1 + +}