From e6488986e570c7fd13f3d713f5fe64c2d2e0c6c9 Mon Sep 17 00:00:00 2001 From: monoid Date: Tue, 17 Dec 2024 14:22:53 +0900 Subject: [PATCH] Add solution day 16 --- day_16/example_1.txt | 15 +++++ day_16/example_2.txt | 17 ++++++ day_16/input.txt | 141 +++++++++++++++++++++++++++++++++++++++++++ day_16/solve_1.ts | 100 ++++++++++++++++++++++++++++++ day_16/solve_2.ts | 123 +++++++++++++++++++++++++++++++++++++ 5 files changed, 396 insertions(+) create mode 100644 day_16/example_1.txt create mode 100644 day_16/example_2.txt create mode 100644 day_16/input.txt create mode 100644 day_16/solve_1.ts create mode 100644 day_16/solve_2.ts diff --git a/day_16/example_1.txt b/day_16/example_1.txt new file mode 100644 index 0000000..6a5bb85 --- /dev/null +++ b/day_16/example_1.txt @@ -0,0 +1,15 @@ +############### +#.......#....E# +#.#.###.#.###.# +#.....#.#...#.# +#.###.#####.#.# +#.#.#.......#.# +#.#.#####.###.# +#...........#.# +###.#.#####.#.# +#...#.....#.#.# +#.#.#.###.#.#.# +#.....#...#.#.# +#.###.#.#.#.#.# +#S..#.....#...# +############### \ No newline at end of file diff --git a/day_16/example_2.txt b/day_16/example_2.txt new file mode 100644 index 0000000..54f4cd7 --- /dev/null +++ b/day_16/example_2.txt @@ -0,0 +1,17 @@ +################# +#...#...#...#..E# +#.#.#.#.#.#.#.#.# +#.#.#.#...#...#.# +#.#.#.#.###.#.#.# +#...#.#.#.....#.# +#.#.#.#.#.#####.# +#.#...#.#.#.....# +#.#.#####.#.###.# +#.#.#.......#...# +#.#.###.#####.### +#.#.#...#.....#.# +#.#.#.#####.###.# +#.#.#.........#.# +#.#.#.#########.# +#S#.............# +################# \ No newline at end of file diff --git a/day_16/input.txt b/day_16/input.txt new file mode 100644 index 0000000..1c70d86 --- /dev/null +++ b/day_16/input.txt @@ -0,0 +1,141 @@ +############################################################################################################################################# +#.....#.........#.......#.....................#.....#...#.........#.#.....#.......#.........#.........#.......#...#.........#...#.....#...#E# +#.#.#.#.#####.###.#.#####.###.#.#.#.#.#######.#.###.#.#.###.#.###.#.#.#.###.###.#.###.#.###.#.#######.###.###.#.#.#.#.#####.#.#.#.#.#.#.#.#.# +#.#.#.#.....#...#.#.#.......#.#.#.#.#.#.....#...#.#...#...#.................#...#.....#.#...#.#.....#...#...#.#.#...#.#.....#.#...#.#...#...# +#.#.#######.###.#.#.#.#######.#.#.#.#.#.###.#.###.#######.###.#.#.###########.#######.#.#.###.#.#######.###.#.#######.#.#####.#####.#######.# +#.#.#.....#...#...#.......#...#...#...#...#.......#.....#.....#.#.....#.......#.......#.#.#...#.......#.....#.#.......#...#.#.#...#.....#...# +###.#.#.#####.#############.###############.#.###.#.###.#######.###.#.#.###########.###.#.###.#####.#########.#.#########.#.#.#.#.#.###.#.#.# +#...#.#...........................#...#...#...................#.....#.#...........#.....#.#...#.....#.......#...#.....#...#...#.#.#...#...#.# +#.#.#.###.#.#.#.#####.#.#####.###.#.#.#.#.#.#.#####.#####.#.#.#.###.#.#########.#.#.#####.#.###.#####.#.#########.#####.#######.#.#########.# +#...................#.#.#...#.#...#.#...#...#.....#.....#.#.#...#.#...#.......#.#.#.....#...#...#...#.#.....................................# +#.###.#.#.###.###.#.#.###.#.#.#.###.###.#####.###.#.###.###.###.#.#####.#####.###.#.###.#####.###.#.#.###############.#######.#.###########.# +#.#...#.#.....#...#...#...#...#...#.......................#.......#.....#...#...............#.....#...#.....#.....#.#.................#.....# +#.#.#.#.###.###.#####.#.#########.#.#.###.###.#.#.#######.#####.#.#####.###.#.#.#.###.#####.#.###########.#.###.#.#.#######.#.#.#.#####.###.# +#...#.#.......#.....#.#.#.....#...#.#.....#.....#...........#...#.......#...#.#.#.#...#.......#.....#...#.#.....#.......#...#...#.....#.....# +#.###.#.#.#.#.#####.###.###.###.#####.#####.#.#############.#.#.#########.###.#.###.###.#####.#.###.#.#.#.#####.#########.#######.###.#####.# +#.#.....#.#...#...#...#...#.#...#...#.....#.........#.......#.....#...#.....#.#.....#...#.....#...#...#...#.#...#.........#.........#.#.....# +#.#.#.###.#.###.#.###.###.#.#.#.#.#.#####.#########.#.#######.###.#.#.###.#.#.#.#####.###.#######.#########.#.###.#########.#########.#.###.# +#.#.......#.....#.#.....#.....#.#.#.#.........#.....#.........#.#...#...#.#.#.#...#...#...#.......#.......#.....#.........#.#...#.....#.....# +#.#.#.#####.#######.###.###.#.#.#.#.###.#######.###############.#######.###.#.#####.###.###.###.###.###.#.#.#############.#.###.#.###.#.###.# +#.#.#.......#.......#.#.#.#.#.#.#.#...#.#.......#...............#.....#.....#.#.....#...#...#...#.....#.#.#.#...........#.#...........#...#.# +#.#.#.#####.#.#######.#.#.#.#.###.###.###.#######.#######.#.#####.###.#.#####.#.#####.###.###.###.###.#.###.#.#########.#.###.#.#.#.###.#.#.# +#...#.#.....#.....#.......#.#.......#.#...#.......#.......#.#.......#...#.....#...#...#...#...#.#...#.#...#.#.....#.#...#.#...#.............# +###.#.#.###.#####.#.#######.#########.#.#.#.#######.###.#.###.###########.#######.#.#######.###.###.#.###.#.#####.#.#.###.#.###.#.#.#.#.##### +#.............#...#.#.......#...#.#...#...#...#...#.#.#.#.....#.#.........#...#...#.#.....#.#.......#...#...#.....#.#.....#.#...#.#...#.#...# +###.#.#.###.#.#.#.#.#.#######.#.#.#.#####.#####.#.#.#.#.###.###.#.#########.#.#.#.#.#.###.#.#.#########.#####.#####.#.###.#.#.#.#.#####.#.#.# +#...#.....#.#.#.#...#.......#.....#.....#.......#.....#...#.....#.#.........#...#.#...#.#...#...#.............#.....#.#...#.#.#.#.......#.#.# +#.#.#.###.#.#.#.###########.#.#.#.#####.#.###########.###.#####.#.###.#.#########.#####.#######.#.#######.#.#######.#.#.#.#.#.###.#####.#.#.# +#.#.#...#.#...#.#...#...#...#.#...#.....#.#.....#...#...#...#...#.#...#.#...........#.......#.....#.....#.#...#...#.#.#.#...#.............#.# +#.#.#.#.#.#####.#.#.#.#.#.###.#####.#####.#.###.#.#.#######.#####.#.###.#.#######.#.#.###.###.#####.###.#####.#.#.#.#.#.#.#####.###########.# +#.#.#.#.#.......#.#...#...#.....#...#.#...#.#.#...#.......#.#.....#...#.#.#.#.....#...#.#.#.....#...#.#...#...#.#...#...#.......#.........#.# +#.#.###.#######.#.###.#########.#.###.#.###.#.###########.#.#.#######.#.#.#.#.#####.###.#.#.#####.###.###.#.###.#######.#.#######.#########.# +#.#.....#.......#...#...........#.#...#...#.#...#.......#.#.#...#...........#.#.......#...#.#.#...#.#...#...#.#...........#.........#.......# +#.#.###############.#.#########.#.###.###.#.#.###.#.#####.#.###.#.#.#######.#.###.#.###.###.#.#.###.#.#.#####.#.###########.#######.#.####### +#...#.............#...#.....#...#...#.#...#.#.....#.....#.#.#...#.#.......#.#...#.#...#.#.#.....#.....#.........#.....#...#.#.....#.#...#...# +###.#.#########.#.#####.#.###.#####.#.#.###.###.#######.#.#.#.###.###.###.###.#.#####.#.#.###.#.#.#######.#######.###.#.#.#.#####.#.###.#.#.# +#...#.#.....#...#...#...#.#...#.#...#...#.#...#.#.....#.....#.....#...#.#...#.#.#.....#...#.#.#.#.#.....#.#.....#...#...#...#...#.#...#...#.# +#.#.#.#.#####.#####.#.#####.###.#.###.###.###.###.###.#############.###.###.###.#.#######.#.#.#.###.###.###.###.###.#########.#.#.###.#####.# +#.#...#.....#...#.....#.....#...#.#.#.#.....#...#.#.#.......#.......#.......#...#.......#...#.#.......#.#...#.#.....#.........#.#...#.....#.# +#.#.###.###.###.#####.#.#.#####.#.#.#.#.#######.#.#.#######.#.#.#####.#######.###.###.#####.#.#########.#.###.#######.###.#####.#.#.###.#.#.# +#.#...#...#...#.....#.#.#...#.......#.#.......#.#.#.......#...#.#...#...#.....#.....#.#.....#.....#.....#.#.....#.......#...#...#.#...#.#.#.# +#.###.###.#.#.#####.#.#.#.#.#.#.#.#.#.###.#.#.#.#.#.#.###.#####.###.###.#.###.#####.#.#.#########.#.#.###.#.#.#.#.#####.###.#.###.###.#.#.#.# +#...#...#.#.....#.#.#.#...#...#...#.#...#.#.#.#.#.#.#.#...#...#...#...#...#...#...#.#.#.....#.#...#.#.#...#...#...#.....#...#.#...#.#.#.#...# +###.###.#.###.#.#.#.#.#####.###.###.###.#.#.#.#.#.###.#.#####.###.#.#.#####.#.#.#.###.#####.#.#.###.###.###.#.#####.#.###.###.###.#.#.#.###.# +#...#.....#.#.#...#.#.#.....#...#...#...#.#.#.#...#...#.#.......#.#.#.........#.#...#.........#...#...#...#.#...#...#.....#.#...#.#.#.#.#...# +#.#.#.###.#.#.###.#.###.#####.###.#.#.#####.#.#####.###.#.###.###.#############.###.#.###.###.###.###.###.#####.###.#######.#.#.#.#.#.#.#.### +#.#.#.#.#...#...#.#.........#.#...#.#.#.............#.....#.#.#.............#...#.#.#...#...#...#...#...#...#...#.....#.....#.#.#.#.#.#...#.# +#.#.#.#.###.###.#.#########.#.#.###.#.#.###.#######.#######.#.#.###.#######.#.###.#.#####.#.#######.#.#####.#.###.#.###.#.#.#.#.#.#.#.#####.# +#.#.#.#.......#.#.....#...#.#.#.#...#...#...#...#...#.....#...#.#...#.....#...#...#.....#.#...#.....#.......#...#.#.#...#.#...#.#.#.#.......# +#.###.#.#######.#######.#.#.#.#.###.#####.###.#.#.###.#.#.#####.#.#####.#########.#####.#.###.#.###############.#.#.#.###.###.#.#.#.###.##### +#.....#...#.....#.......#.#.#.#...#.#.....#...#...#...#.#.#.....#.....#...........#...#...#.#...#.#.....#.......#.....#.#.#...#.#.....#.....# +###########.#####.###.#.#.###.###.#.###.#.#.#######.###.#.#.###.#####.#######.###.###.#####.#####.#.#.###.#.#####.#####.#.#.###.###########.# +#.#.........#...#.#...#.#.....#...#.#...#.#.#...#...#...#...#.#.....#.......#...#.............#.....#.....#.#...#.......#.#...#.....#...#...# +#.#.###.#####.#.#.#.###.###.###.###.#.###.#.#.###.###.#######.#.#.#########.#.#.#####.#######.###.#########.#.###.#####.#.###.#####.#.#.#.#.# +#.............#...#.....#...#...#.....#...#...#...#.............#.....#.....#.#.#.....#.....#...#.#.....#...#...#.......#.#.......#.#.#...#.# +#.###.#.#.#.#.###########.#.#.#.#############.#.###.#########.###.#.###.#######.#.#####.###.###.#.###.#.#.#####.#######.#.#.###.###.#.#####.# +#...#.#...#.#.......#.....#.#.#...........#...#.#...#.#.....#.....#...#.........#...#...#.#.....#...#.#.#.#...#.........#.#.#.........#...#.# +#.#.#.#####.#######.#.#####.#.###########.#.###.#.###.#.###.#.#######.#######.#.#.#.#.###.#########.#.#.#.#.#.#.#########.#.#.#.#.#####.#.#.# +#.#.#.#...#.............#...#.........#...#...#.#.....#.#.#...#.....#.......#...#.#.#.....#...#...#.#.#.#...#.#.#.....#...#.#...#.....#.#...# +#.#.#.#.#####.#.#.#.###.#.###########.#.#.#.#.#.#####.#.#.#########.#####.#.#####.#.#####.#.#.#.###.#.#.#####.#.#.###.#.#.#.###.#####.#.##### +#.#.#.#.....#...#.#.#...#.#...........#.#.#...#...#...#.........#.....#...#.#.....#.....#.#.#...#...#.#.#...#.#...#...........#.............# +#.###.#####.###.#.#.#.###.#.#.#########.###.#####.#############.#.###.#.#####.#####.#####.#.###.#.###.#.#.#.#.#####.#########.#.#.#######.#.# +#.....#...#...#.#.#...#.#.#.#.........#.#...#.....#.....#.....#...#...#.........#.#.#.....#...#.....#.#.#.#...#.#...#.#.....#.#.#...#...#...# +#.#.###.#.###.#.###.#.#.#.#.#####.#####.#.###.#####.#.#.#####.#####.#######.###.#.#.#.#####.#.#####.#.#.#.#####.#.###.#.#.#.#.#.###.#.#.###.# +#.#...............#...#.#.#.......#...#.....#...#...#.#.....#...#.......#...#.#...#...#...#.#.....#.#.#.#.#.....#...#...#.#.#...#.#.#.#.....# +#.#.#.#####.#.###.#####.#.#.#######.#.#########.#.###.#####.#.###.#######.#.#.###.#######.#.###.#.#.###.#.#.###.###.#####.#.###.#.#.#.####### +#...#...#.#.....#.......#.#.#.......#.........#...#...#...#...#.....#.......#.....#.....#.......#.#.#...#.#.#.....#...#...#.....#.#.#.......# +#.#####.#.#.###########.#.#.#.#########.#####.#####.###.#.#####.#####.#####.#.#######.#.#.###.#####.#.#.#.###.#######.#.#######.#.#.###.#.#.# +#.......#...#.......#...#.#.#.#.......#...#...#...#...#.#.......#...#.#.....#.#.......#.#...#.......#...#...#.#.....#.#.#.........#.........# +#########.#.#.#####.#.#.#.#.#.#.#.###.#.#.#.#.#.#.#.#.#.#########.#.#.#.#.###.#.#.#.###.#########.###.#####.#.#.#.###.#.#####.#.#####.#.###.# +#.....#.#.#...#...#...#...#.#.#.#...#...#...#.#.#.#.#.#.........#.#...#.....#...#...#.#.........#.#.......#.#...#.....#.#...#...............# +#.#.#.#.#.#.###.#.###.#.###.#.#.###.#.#####.###.#.###.#.#.#######.#########.#########.#########.#.###.###.#.#######.###.#.#.###.###.#.#####.# +#...#...#.#...#.#...#.#...#.#.....#.#.......#...#...#.#.#.#.....#.......#...#.......#...#.....#.#.....#...#.#.......#...#.#.....#...#.....#.# +###.#.###.#.#.#.#####.###.#####.###.#########.#####.#.#.#.#.###.#########.###.###.#.#.#.#.#.###.#####.#.###.#.###.#.###.#.#######.#.#.#####.# +#...#.#.....#.#.......#.#.......#...#...#.....#...#.#.....#...#...........#...#...#...#.#.#.#.........#...#.#.#.#.#...#.........#.#.#.#.....# +#.#.#.#.###.#.#.#######.#########.#.#.#.#.#####.#.#.#########.###############.#.#.#####.#.#.#.###.###.###.#.#.#.#.###.#######.###.#.#.#.##### +#.............#.............#.....#.#.#...#...#.#...........#.#.......#.......#.#.#...#...#.#.......#.....#.....#...#.#...#...#...#.#.#...#.# +###.#.###.#.#.#############.#.#####.#.#####.#.#.#.#.###.#.#.#.#.#####.#.#.#.#.#.#.#.#.#####.#######.#########.#####.#.#.#.#.###.#.###.###.#.# +#.........#...#.......#.....#.#.....#.......#...#.#.....#...#.#.#...#...#.#.#.#.#.#.#.#...#.#.....#.#.......#.#.....#.#.#.#.#...#.#.....#.#.# +#.#.#.#####.###.#.#####.#####.###.###############.#.#.#.#.###.#.#.#.#####.#.#.#.###.#.#.#.#.#.#.#.#.#.#####.#.#.#.#.#.#.#.###.###.#.###.#.#.# +#.#.....#.......#.#.....#...#...#.#.............#...#...#.....#...#...#...#.#.#.....#.#.#.#.....#.#.#.#.....#.#.#...#...#.#...#.#.#.#...#...# +#.#.#.#.#######.###.#####.#.###.#.#.#####.###########.#################.###.#.#######.#.#.#.#######.#.#.#####.#.#########.#.###.#.#.#######.# +#...#...#...#...#...#.#...#.#...#.#...#.#...........#.#.....#...........#.#.#.#.......#.#.#.#.......#.#...#...#.#...#...#.#...#.#.#.........# +#.#.#.###.#.#.###.###.#.#.###.###.###.#.#######.#####.#.#.#.#.###########.#.#.#.#######.#.#.#.#######.###.#####.#.#.#.#.#.###.#.#.#.#.####### +#...#.#...#.......#.....#...#.#...#...#.......#.......#.#.#...#.....#.......#.#.#.....#.#...#...#.......#.......#.#.#.#.#...................# +###.#.#.###.#.#.###.#######.#.#.###.#########.#.#########.#####.###.#.#########.#.#.###.#######.#######.###########.#.#.#######.#.#.###.#.#.# +#.#.#...#...#.#.#...#.....#.#.#.#...#.......#.............#...#...#.............#.#.#...#...#...#.......#...#.......#.#.........#.#.....#.#.# +#.#.#.###.###.#.#####.###.#.#.###.###.#####.#####.###.###.#.#.###################.###.###.#.#.###.#.#.###.#.#.###.#.#.#.###.#####.###.#.#.#.# +#...#.#...#...#.......#.#...#.....#.....#.........#...#.....#...#.....#.........#.....#...#...#.......#...#.#.#...#.#.....#.......#.#.....#.# +#.###.#.###.#########.#.#.#############.###########.###.###.#.#.#.###.#####.#####.#####.#######.###.###.###.#.#.###.#####.#########.#.#.#.#.# +#.....#...#...............#.............#...#.....#.#...#...#.#.....#...#...#...#.#.....#.....#.#.....#.#...#.#.#...#.#...#.....#.........#.# +#####.###.###.#############.#######.#.###.#.###.###.#.###.#.###########.#.###.#.#.#.###.#.#.#.#.#######.#.#####.#.###.#.###.###.#.###.#.#.#.# +#...#...#...#.#.......#...#...#...#.#.#...#...#...#.#...#.#...........#.#.#...#...#.#.....#.#...#.....#.#.....#.#...#.#.#.....#.......#.#...# +#.#.#.#.#.#.#.#.#####.#.#.###.###.#.###.#####.#.#.#.###.#.###.#######.#.#.#.#######.#.#####.#####.###.#.#####.#.#.#.#.#.#.###.#.#.###.#.#.#.# +#.#...#.#.#.#.#.#.#...#.#...#.#...#.....#...#.#.#.#.#...#...#.#.....#...#.....#...#.#.....#.....#...#...#...#.#...#...#.#.....#.#...#.....#.# +#.###.#.#.#.#.#.#.#.#####.#.#.#.#.#######.#.#.###.#.#.#####.###.###.#########.#.###.#####.#.#.#.###.#####.#.#.###.###.#.###.#.#.#.#.#.#.#.#.# +#.#.............#.......#...#.#.#.........#.#...#.#.#.#...#.....#.#.#.....#.#.#.#...#...#.#...#.#...#.....#.#.#.......#...#.#.#.#.#.....#...# +#.###.#####.#####.#####.#.###.#.#####.#####.###.#.#.#.#.#.###.###.#.#.###.#.#.#.#.###.###.#####.#.###.#######.#.#####.###.#.#.###.###.#.#.### +#...#.....#.#.#...#.......#...........#.......#.#...#.#...........#...#...#...#.#.#.....#.....#.......#...#...#.....#...#.#.#.#.....#...#...# +#.#.#####.#.#.#.#.#.#######.###.#######.#######.#.###.#########.#.#####.#####.#.#.#####.###.#.#########.#.#.#######.#####.#.#.#.#####.#.#.#.# +#.#.#.....#.#...#...#.#.....#.........#.#...#...#...#...#.....#.#...#...........#.....#.....#.....#.....#...#.....#.#.....#.#.....#...#...#.# +#.#.###.###.#.#######.#.#############.#.#.#.#.#########.#.###.#.#.#.#.#####.#########.###.###.###.#.#########.#.#.#.#.#########.###.#.#.#.#.# +#.#...#.#...#...#.....#.....#.......#.#...#.#.........#...#.#.#.#...#.....#.........#...#.#...#...#.#.......#.#.#...#.#.......#.#...#...#...# +#.###.#.#.#####.#.###.#####.#.#####.#.#####.#######.#.#####.#.#.###.#####.#####.###.###.#.#.#.#####.#.###.###.#.###.#.#####.#.###.###.#.#.### +#.#...#.#.#...#.#...#.#.....#...#...#...#.......#.#.#.....#.......#.#...#.#...#...#...#.#.#.#.#.....#.........#.#...#...#...#...............# +#.#.###.#.#.#.#.###.#.#.#######.#.#####.#.#####.#.#.###.###.#####.#.#.#.#.#.#.###.#####.#.#.#.#.#######.#######.###.###.#.#######.#.#.#.#.#.# +#.#...#...#.#.#...#.#.#.#.......#.....#.#.....#...#.#...#.........#...#...#.#.....#.....#...#.#.....#...#.....#...#...#...#.........#...#.#.# +#####.#######.###.#.###.#.#.#########.#.###.#.#.###.#####.#####.#######.###.#####.#.###############.#####.###.#.#.###.#####.#########.###.#.# +#.....#.......#...#...#.#.#.#.....#...#.#...#...#...#.....#...#.#...........#...#.#...#...........#.......#...#.#...#.#...........#...#...#.# +#.#######.#.###.#####.#.#.#.#.###.#.###.#.#####.#.###.#####.#.###.###########.###.###.###.#######.###.#####.###.###.###.#########.###.#.###.# +#.#.....#.#...#.......#...#.#.#.#.......#.....#.#.#...#...#.#...#.....#.....#.....#...#...#.....#...#.#...#.#...#.#...#.....#...#.......#...# +#.#.###.#.#.#.#.#####.#####.#.#.#####.#######.#.#.#.###.###.#.#.#####.#.#####.#####.###.###.#######.#.#.#.#.#.###.###.#####.#.###########.#.# +#.#...#.#...#...#...#.#.....#.#.......#.....#.#.#.#...#...#...#.......#...#.....#...#.....#.........#.#.#.#.#...#...#.....#.#.............#.# +#.###.#.###.#####.#.#.#.#.###.#.#####.###.#.#.#.#.###.#.#.###.###########.#.#####.###.###.#.#########.###.#.###.#.#.#####.#.###.#.#########.# +#.....#.....#...#.#.#...#.....#.#...#.....#.#.#.#...#.#.#...#...#.......#.#...#...#...#.#.#...#.#...#.....#...#...#.#...#.#...#.#.#.#.......# +#.#########.#.#.#.#.#.###.#####.#.#.#########.#.###.#.#####.###.#.#####.#.###.#.###.###.#.###.#.#.#.#####.###.#####.#.#.#.###.#.#.#.#.#####.# +#...#.....#.#.#...#.#.#...#.......#.........#...#...#.......#.#...#.....#.....#.#.......#.#...#...#...#.....#.#...#.#.#.#.#...#.#.#...#...#.# +###.#.#####.#.#####.###.###.###############.#.###.#.#######.#.#####.#########.#.#########.#.#######.#.#.#####.###.#.#.#.#.#.###.#.#####.#.### +#.#.#...#...#...#...#...#...#...#...#...#...#.#.....#.....#.#...#.#.........#.#.....#...#.#.......#.#.....#...#...#...#.#...#...#...#...#...# +#.#.###.#.#.###.#.###.###.#.#.#.#.#.#.#.#.###.#.#.###.###.#.#.#.#.###.#####.#.#####.#.#.#.#####.#.###.#.###.###.#######.#######.###.#.#####.# +#.#...#.#.....#.#...#.#...#.#.#...#...#.#.#.....#.#...#.#.#...#...#.........#.....#...#...#.....#...#.#.#...#.......#...#.....#...#...#...#.# +#.###.#.###.###.###.#.#.#.#.#.###.#######.#.###.###.###.#.#######.###.#.###.#####.#########.###.###.#.#.#.#######.###.###.#.#.#.#####.#.###.# +#...#...#...#...#.#.#.#...#.#...#.#...#.......#...#.....#.........#...#.#.....#.....#.......#.#.#.#.#...#.......#.....#...#.#.#.#...#...#...# +#.#####.#.#.#.###.#.#.###.#.###.###.#.#.###.#.###.#####.###########.#.#.#.###.#.###.#######.#.#.#.#.###.#######.#.#######.#.#.###.#.###.#.### +#.......#...#...#.#.#.#...#...#.....#.....#.#...#...#...#.....#.....#.#...#.#.#.....#...#...#.....#.#...#.....#.........#.#.#.....#...#.#...# +#.#########.###.#.#.#.#.#####.#.###.#.###.#.#.###.###.###.###.#.#.#########.#.#.#####.#.#.###.#####.#.###.###.#.#######.#.#.###.#.###.###.#.# +#...#.#...#...#.#.#...#.#...#...#.....#...#.#...#...#...#.#.....#.#.........#.#.....#.#.#...#.#...#.#.#.#...#.#.#.....#.#.#.#.#...#.........# +###.#.#.#.#.###.#.#####.###.###.#.###.#.#######.###.###.#.#.###.#.#.#.#######.#####.#.#.###.#.#.#.#.#.#.###.#.#.#.#.###.#.#.#.###.#.###.###.# +#...#.#.#...#...#.....#...#...#...#...#.........#...............#...#.......#.....#.#.#.....#.#.#...#...#...#...#.#.....#.#.#.........#...#.# +#.###.#.#.###.###.#######.#.#.#####.#.#.#######.#.#####.#.###.###.#########.#.###.#.#.###.###.#.#######.#.#######.#########.#.#.#.#######.#.# +#.....#.#.....#.........#.#.#...#...#.........#...............#...#.....#...#...#.#...#...#.....#.....#.#.......#.#.........#.#...#.....#...# +#####.#.#.#####.#.###.#.#.#####.#.###.#.###.#########.#.###.#.#.#.#####.#.#.#.#.#.#####.###.#########.#.#######.#.#.###.#####.###.#.###.###.# +#.#...#...#.#.........#.#.#.....#.....#.#...#.....#.#...#...#...#.......#.#.#.#...#.#...#...#.......#.......#...#.............#...#...#.....# +#.#.#####.#.#.#####.#.###.#.###.#####.#.#.#.#.#.#.#.###.#.#######.#.###.#.#.#.#####.#.###.###.###.#.#####.###.###.#.###.#####.#.#.###.####### +#.#.#.......#.......#.......#.......#...........#...#...#...#.....#.#...#.#.#.#...#.....#.#...#...#...#...#...#.#.....#.....#.#.#.#...#.....# +#.#.###.#.#.###.#####.###############.#.#.#.#.#.#####.#.###.#######.#.###.#.#.#.#.#.#####.#.###.#####.###.#.###.###.#.#####.###.#.#.###.#.### +#.....................#.........#.....#...#.#.#.....#...#...........#...#.#.#.#.#...#...............#...#.#.#.......#.....#.#...#...#...#...# +#.###.###.#.###.#.#.###.#######.#.###.#.###.#.#####.###.#################.#.#.#.#####.#####.#.###.#####.###.#.#.###.#####.#.#.###.#######.#.# +#...#.....#.....#.#...#...#...#.#.#...#...#...#...#...#...........#.......#.#.#...#...#.......................#.#.......#...#.#...#.........# +#.#.#####.#######.###.###.#.###.#.#.#.#.#.#######.#.#############.#.#######.#.#####.###.#######.#.#.#########.#.#.#.###.#####.#.#####.#.###.# +#S#...............................................#.................#.......#...........#.........#...........#.......#...............#.....# +############################################################################################################################################# \ No newline at end of file diff --git a/day_16/solve_1.ts b/day_16/solve_1.ts new file mode 100644 index 0000000..c4c5500 --- /dev/null +++ b/day_16/solve_1.ts @@ -0,0 +1,100 @@ +import { BinaryHeap } from "jsr:@std/data-structures/binary-heap"; + +export type Map = string[][]; + +export async function readMap(path: string): Promise { + const text = await Deno.readTextFile(path); + return text.trim().split("\n").map((line) => line.trim().split("")); +} + +export function displayMap(map: Map): void { + console.log(map.map((line) => line.join("")).join("\n")); +} + +export type Position = [number, number]; +export type Face = 0 | 1 | 2 | 3; +export type RotateDir = "clockwise" | "counter-clockwise"; +export function rotateFace(face: Face, dir: RotateDir): Face { + if (dir === "clockwise") { + return ((face + 1) % 4) as Face; + } else { + return ((face + 3) % 4) as Face; + } +} +export function faceToStr(face: Face): string { + return ["E", "S", "W", "N"][face]; +} +export function strToFace(str: string): Face { + const face = ["E", "S", "W", "N"].indexOf(str); + if (face === -1) { + throw new Error(`Invalid face: ${str}`); + } + return face as Face; +} +export function moveForward([x, y]: Position, face: Face): Position { + switch (face) { + case 0: return [x + 1, y]; + case 1: return [x, y + 1]; + case 2: return [x - 1, y]; + case 3: return [x, y - 1]; + } +} + +// S is start +// E is end +// # is wall +// first face the east + +export class Solver { + readonly map: Map; + + constructor(map: Map) { + this.map = map; + } + + public minCost([x,y]: [number, number], face: Face): number { + const heap = new BinaryHeap<[Position, Face, number]>( + ([, , cost1], [, , cost2]) => cost1 - cost2 + ); + const visited = new Set(); + heap.push([[x, y], face, 0]); + while (heap.length > 0) { + const [[x, y], face, cost] = heap.pop()!; + if (this.map[y][x] === "E") { + return cost; + } + if (this.map[y][x] === "#") { + continue; + } + const key = `${x},${y},${face}`; + if (visited.has(key)) { + continue; + } + visited.add(key); + for (const dir of ["clockwise", "counter-clockwise"] as RotateDir[]) { + heap.push([[x, y], rotateFace(face, dir), cost + 1000]); + } + heap.push([moveForward([x, y], face), face, cost + 1]); + } + return -1; + } + + findStart(): Position { + for (let y = 0; y < this.map.length; y++) { + for (let x = 0; x < this.map[y].length; x++) { + if (this.map[y][x] === "S") { + return [x, y]; + } + } + } + throw new Error("No start found"); + } +} + +if (import.meta.main) { + const map = await readMap("input.txt"); + displayMap(map); + const solver = new Solver(map); + const start = solver.findStart(); + console.log(solver.minCost(start, strToFace("E"))); +} \ No newline at end of file diff --git a/day_16/solve_2.ts b/day_16/solve_2.ts new file mode 100644 index 0000000..d65769e --- /dev/null +++ b/day_16/solve_2.ts @@ -0,0 +1,123 @@ +import { BinaryHeap } from "jsr:@std/data-structures/binary-heap"; +import { + displayMap, + Face, + moveForward, + Position, + readMap, + RotateDir, + rotateFace, + Solver, + strToFace, +} from "./solve_1.ts"; + +function moveBackward([x, y]: Position, face: Face): Position { + switch (face) { + case 0: + return [x - 1, y]; + case 1: + return [x, y - 1]; + case 2: + return [x + 1, y]; + case 3: + return [x, y + 1]; + } +} + +class Solver2 extends Solver { + public getPathBlocks( + [x, y]: [number, number], + face: Face, + ): Position[][] { + const heap = new BinaryHeap<[Position, Face, number]>( + ([, , cost1], [, , cost2]) => cost1 - cost2, + ); + const costMap = new Map(); + const visited = new Set(); + heap.push([[x, y], face, 0]); + let endY = -1, endX = -1, endCost = -1, endFace = -1 as Face; + while (heap.length > 0) { + const [[x, y], face, cost] = heap.pop()!; + const key = `${x},${y},${face}`; + if (this.map[y][x] === "E") { + endY = y; + endX = x; + endCost = cost; + endFace = face; + costMap.set(key, cost); + visited.add(key); + break; + } + if (this.map[y][x] === "#") { + continue; + } + if (visited.has(key)) { + continue; + } + visited.add(key); + costMap.set(key, cost); + for ( + const dir of ["clockwise", "counter-clockwise"] as RotateDir[] + ) { + heap.push([[x, y], rotateFace(face, dir), cost + 1000]); + } + heap.push([moveForward([x, y], face), face, cost + 1]); + } + if (endY === -1) { + return []; + } + // find the path back to the start + let curX = endX; + let curY = endY; + let curFace = endFace; + let curCost = endCost; + const getPathes = (pos: Position, face: Face, cost: number) => { + const key = `${pos[0]},${pos[1]},${face}`; + if (costMap.get(key) !== cost) { + return []; + } + if (cost < 0) { + return []; + } + if (cost === 0) { + return [[pos]]; + } + const pathes: Position[][] = []; + for ( + const dir of ["clockwise", "counter-clockwise"] as RotateDir[] + ) { + const r = getPathes(pos, rotateFace(face, dir), cost - 1000); + if (r.length > 0) { + pathes.push(...r); + } + } + const ba = getPathes(moveBackward(pos, face), face, cost - 1); + pathes.push(...ba.map((p) => [pos, ...p])); + return pathes; + }; + return getPathes([curX, curY], curFace, curCost); + } +} + +if (import.meta.main) { + const map = await readMap("input.txt"); + displayMap(map); + const solver = new Solver2(map); + const start = solver.findStart(); + const pathes = solver.getPathBlocks(start, strToFace("E")); + for (const path of pathes ?? []) { + for (const [x, y] of path) { + map[y][x] = "O"; + } + } + displayMap(map); + let count = 0; + for (const row of map) { + for (const cell of row) { + if (cell === "O") { + count++; + } + } + } + console.log(count); +}