From 928d3342eb2ef5b0a3ef9ac2a54f3ba3ad509a38 Mon Sep 17 00:00:00 2001 From: Ariejan de Vroom Date: Mon, 16 Dec 2024 10:35:34 +0100 Subject: [PATCH] Day 16 - Puzzle 1 --- lib/input/day_16.txt | 141 ++++++++++++++++++++++++++++++++++ lib/solutions/day_16.rb | 117 ++++++++++++++++++++++++++++ spec/input/day_16_test.txt | 15 ++++ spec/solutions/day_16_spec.rb | 61 +++++++++++++++ 4 files changed, 334 insertions(+) create mode 100644 lib/input/day_16.txt create mode 100644 lib/solutions/day_16.rb create mode 100644 spec/input/day_16_test.txt create mode 100644 spec/solutions/day_16_spec.rb diff --git a/lib/input/day_16.txt b/lib/input/day_16.txt new file mode 100644 index 0000000..69e75d3 --- /dev/null +++ b/lib/input/day_16.txt @@ -0,0 +1,141 @@ +############################################################################################################################################# +#.......#...#.....#.................#.......#.........#.......#.....#.......#.....#.#.....................#.........#.......#.#............E# +#.###.#.#.#.###.#.#.#####.#########.#####.###.#.#######.#####.#.###.#.#.#.#.#.#.#.#.#.#.###.#####.#.###.#.#.###.###.#.###.#.#.#.#.###.###.### +#.#...#...#.....#.........#...#.....#...#.....#.....#...#...#...#...#.#.............#.....................#.#.#...#.#.#.#.#...#.#...#.......# +#.#.###.#######.###.#######.###.#####.#.#####.#####.#.#####.#.###.###.###.###.#.###.#.#######.#.###.#.###.#.#.###.###.#.#.#####.#.#.#####.#.# +#.#.#...#.............................#.....#.#.....#.........#.#...#...#.....#.........#...#.#.#...#.#...#.....#.....#.#.......#.#...#...#.# +###.#####.###.#.###.#.#.#####.#.###.#######.###.###.#########.#.###.###.#.###############.#.###.#.###.###.#####.#######.#########.#####.#.#.# +#...#.......#...#.#.#.#.......#.....#.............................................#.......#...#.........#.....#.#.......#...#...#.#.....#...# +#.###.#####.###.#.#.#.#######.#########.#.###.#.#.#####.#.###.#.#.#.#.#.#.#######.#.#########.#######.#.#####.#.#.#####.#.#.#.#.#.#.#######.# +#.....#...#...#...#.#...#...#.#.......#.#.#...#.#...#...#.#...#.#.#...#.#.......#.#.....#...#...#...#.#.....#.#...#...#...#...#.#.#.......#.# +#.#####.#.###.#.###.#.#.#.###.###.###.###.#.###.#.###.###.#.#.#.#.###.#.#.#####.#.#####.#.#####.#.#.###.###.#######.#.#########.#.#######.#.# +#.#...#.#...#...#...#.#.#...#.....#.#...................#...#...#.#...#.#.#...#.#...#...#.#...#.#.#.................#.....#...#.#.....#...#.# +#.###.#.###.###.#.#####.#.#.#######.###.#.#.#.###.#####.#######.#.#####.#.#.###.###.#.###.#.#.#.#.###.#.#.#.#.#.#.#.#.#####.#.#.#.#####.###.# +#.....#...............................#.#.#...#.#.#.....#.#.....#.#...#.#.#...#.....#.#...#.#...#...#.#...#...#.#.#.#.#.....#...#...#...#...# +#######.#.#####.###.#.#.#############.#.#.#####.#.#.###.#.#.#####.#.#.#.#.#.#.###.###.#.###.#######.#.#####.###.#.#.#.#.#######.###.#.###.### +#.......#.#...#.....#.#.....#.......#...............#.#.#.#.#...#...#.#.#.#.#.#...#.....#...#.......#.....#...#.#.#.#...#.#.........#.#.....# +#.#.#####.#.#.#.#####.#.###.#.#####.#################.#.#.#.#.#.#####.#.#.###.#.###.###.#.###.###########.#####.###.#.###.#.#.#######.####### +#.#.#...#.#.#...#.....#...#.....#.#...................#...#.#.#.....#.#...#...#.#...#...#.#...#.........#.....#.#.........#.#.#.......#.....# +#.#.#.#.###.#####.#############.#.###################.###.#.#######.#.#####.###.#.###.###.#.#######.###.#####.#.#.#####.#.#.#.#.#.#.###.###.# +#.#.#.#.....#...#.......#...#...#...........#...#...#...#.#.....#...#...........#...#.......#.....#...#.#.....#.#.....#.#.#.#...#.#.....#.#.# +#.#.#.#######.#.#######.#.#.#.#.#####.#####.#.#.#.#.###.#.#####.#.#.#########.#.###.#.#######.###.###.#.#.#####.#####.#.#.#.#######.#####.#.# +#.#.#...#.....#.#.....#.#.#.#...#.........#...#...#.#...#...#.#.#.#.....#...#...#...#.#.....#...#...#.#...........#...#.#.#.....#.........#.# +#.#.###.#######.#.#.#.#.#.#.#.###.#######.#####.###.#.#.#.#.#.#.#.#####.#.#.#####.#####.###.###.###.###.###########.#####.#####.#.#.#######.# +#.#...#...........#.#.#...#.#.....#.....#.#...#...#.#.#...#...#.#...#.....#.#...#.....#.#.#.....#.#...#.#...................#...#...#.......# +#.###########.#####.#####.#.###.###.###.#.#.#.#####.#.#######.#.###.#####.###.#.#####.#.#.#######.###.###.#.#########.#####.#.###.#.#.#####.# +#.........#...#.....#...#.#.#...#...#...#...#.......#.#.....#.#...#...#.#.#...#.....#.#.#.......#...#.....#...#...........#.#.....#...#.#...# +#.#######.#.###.#####.#.#.#.###.#.###.#######.#.#####.#.#.###.###.#.#.#.#.#.#.#####.#.#.#.###.###.#.#######.#.###.#######.#.#######.###.#.### +#.........#.....#.....#.#.#...#.#...#.........#.#...#...........#...#.#...#.#.........#.#.#...#.........#...#.....#.......#...#...#...#.#...# +#.#####.#######.#.#####.###.#.#####.#########.###.#.#.#########.#.###.#.###.###########.#.#.#.#.#####.#.#.#####.#######.#####.#.#.#.#.#.#.#.# +#.#...#.#.......#.#...#...#.#...#...#...#...#...#.#.#.#.......#.#.....#...#.....#.......#.#.#.#.#...#.#...#.....#.......#.....#.#...#.#.....# +#.###.#.#.#######.#.#.###.#####.#.###.#.###.###.#.#.###.#####.#.#####.###.#####.#.#######.#.###.#.#.#.#####.###.#.#.###.#.#####.###.#.###.#.# +#...#...#...#.#...#.#...#...#...#.#...#...#...#...#.....#...#.#.#.....#...#.....#...#...#.#.....#.#.#.#.....#.#...#.#...#...#.......#.#...#.# +#.#.#.#####.#.#.#####.#####.#.#.#.#.#.###.#.#.#########.###.#.###.#.###.###.###.###.###.#.#.#######.#.#####.#.#####.#.###.#.#######.#.#.#.#.# +#.#.#.#...#...#.....#.........#.#.#.#...#.#.#...#.....#.....#.....#...#.#.#.#.....#...#.............#.....#.......#.#.#...#.........#...#...# +#.#.#.#.#####.#####.#############.#.###.#.#.###.#.###.#############.#.#.#.#.#.#######.#.#########.#######.#######.#.#.###.#########.#####.#.# +#...#...#.....#.#...#...#...#.....#...#.#.#.#.......#.............#.#...#.#.#.#.....#.#.#...........#.....#.........#.#.......#.......#...#.# +#.#####.#.#####.#.###.#.#.#.#.#########.#.#.#####.#.#############.#.#####.#.###.###.#.#.#.#.#######.#.#####.#.#######.#.#.#####.###.#.#.#.#.# +#.#.............#.#...#...#.#.#.........#...#.......#...#.....#...#.#...#.#.....#...#.#.#.#.#.....#.#.#.....#.#...#.....#.#.....#...#.#.#...# +#.#.#.###.#.###.#.#.#######.#.###.#.#########.#######.#.#.###.#.#.#.#.#.#.#######.#.#.###.###.###.#.#.#.#.#####.#.#########.#####.#.###.#.#.# +#...#.......#.#.#.#...#...#.#...#.#.#.......#.#.......#...#...#.#...#.#...#.#.....#.#.#...#...#...#...#.#.......#.#.......#.....#.#...#...#.# +#.#.#########.#.#.###.#.#.#.#.#.#.###.#####.#.#.###########.###.###.#.#.#.#.#.#####.#.#.#.#.###.#######.#######.#.#.#####.#####.#.###.###.#.# +#.....#...#.....#.....#.#.......#.#...#.#...#.#.#...#.....#...#...#...#.#...#.#...#.#...#.#.#.....#...#.#.....#...#.#.#...#.....#...#.....#.# +#.#.###.#.###.#########.###.#.###.#.###.#.###.#.#.#.#.###.###.###.#####.#.###.#.###.#####.#.#.###.#.#.#.#.###.#.#.#.#.#.###.###.###.#####.#.# +#...#...#...#.#.......#.#.#.#.#...#.#.....#.......#.#.#.....#...#.#.....#.#...#.#.....#...#.#.#.....#...#.#...#.#.#.#.#.......#.....#.....#.# +#.#.#.#####.#.#.#####.#.#.#.#.###.#.#.#.#.#.#######.#.#.###.###.#.#.###.#.#.###.#.#####.###.#.#####.#.###.#.#.#.###.#.#########.###.#.###.### +#.....#.#...#...#.....#.#...#.....#.#.#.#.#.....#...........#...#.#...#.#.#.#.....#.....#...#.#.....#...#.#.#.#.....#.......#...#...#.#.#...# +#.#.###.#.#############.#.#####.#.#.#.#.#.#####.#####.#######.#.#.###.#.#.#.#######.#####.###.#.###.###.#.#.#.#######.#.#####.#.#####.#.#.#.# +#.#.......#.........#...#.#.....#.#.#.#.#.....#.....#.#...#...#.#.#.#.#...#.........#...#.#.....#.....#...#.........#.#.....#...#.....#...#.# +#.#.#.#####.#######.#.###.#.#####.#.#.#.#.#########.###.#.#.#####.#.#.#######.#########.#.#.###.#####.###.#####.###.#.#####.###.#.#####.###.# +#...#.#.....#.......#...#.#.....#...#.#.#.#...........#.#.#.#.....#.#.#.....#...........#...#...#...#...#.#...#...#.#.#.#...#.#.#...#...#...# +###.#.###.#.#######.###.#.#####.#.###.###.#.#####.###.#.#.#.#.#####.#.#.###.#########.#####.#####.#.###.###.#.#.#.#.#.#.#.#.#.#.###.###.#.### +#...#.....#.#...........#.#.....#.....#...#.#...#.#...#.#.#.#.#.....#.#.#...........#.#...#.......#.#.#.....#.#.#.......#.#...#...#.#...#...# +#.###.#######.###.#########.###########.#.#.#.###.#.###.#.#.#.#.#####.#.#.#########.###.#.#######.#.#.#######.#.#.#######.#####.#.#.#.#.###.# +#.#.........#.#.#.......#...#.....#.....#.#.#...#.#.#...#.#.#.#.......#.#.#.......#...#.#.#.........#...#.....#.#...#...#...#.....#...#.#...# +#.#.#######.#.#.#######.#.#####.#.#.#####.#.###.#.#.#.###.#.#.#########.#.#.###.#.#.#.#.#.#.###########.#.#####.###.#.#.###.#.#.#######.#.### +#.#.#...#...#...#.....#...#.....#...#.....#.....#.#...#.#...#.......#...#.#.#...#...#...#.#.#.#...........#.......#.#.#.....#...#.......#...# +#.#.#.#.#.#####.#.###.#######.#######.#######.###.#####.###.#######.#.#####.#.###.#.#####.#.#.#.#.#####.#.#.#######.#.#####.###.#.#########.# +#.#.#.#.#...#...#.......#.....#.....#...#.....#.......#.....#.......#...#...#...#.#.#...#.#.#...#...#...#.#...#.......#...#.#...#.#.....#...# +#.#.#.#.###.#.#####.###.#.#####.###.###.#.#####.#.#####.#####.#######.#.#.#####.#.###.#.#.#.###.###.#.###.#.#.#.#.#####.###.#.#.#.#.#.###.#.# +#...#.#...#...#.....#...#.#...#...#...#.#...#.#.#.#...#.#.............#.#.#...#.#...#.#...#...#...#.#.#...#.#...#...........#.#.#.#.#.#...#.# +#.#####.#.#####.#.###.###.#.#.#######.#.###.#.#.###.#.#.#.#####.#######.#.#.#.#.###.#.###.###.#####.###.###########.#########.#.#.#.###.###.# +#...#...#...#.......#.#.....#...#.......#.#.#.#.........#...#.#.#.....#.....#.#...#.#.#.....#.....#...#.#.........#.#.......#.#.#...#...#.#.# +###.#.#####.#######.#.###.#####.#.#####.#.#.#.#############.#.#.#.###.#.###.#####.#.#.#.###.#.###.#.#.#.###.#####.###.#####.#.#####.#.###.#.# +#.#...#.#...#.......#...#.#.#...#.........#...#.....#.......#...#...#.#...#.#.....#.#.#...#.#...#.#.#.#...#...#.#.....#.#...#...#...........# +#.#####.#.###.#########.#.#.#.###.###########.#####.#.#######.###.###.#.#.###.#####.#.###.#.#####.#.#.###.###.#.#######.#.#####.#.#.#.#.#.#.# +#...#...#.....#.......#.#...#.#.#...#.........#.....#.#...#.....#.#...#.#.......#...#...#...#.....#.#.....#.............#.....#...#...#.#.#.# +#.###.#.###########.#.#.###.#.#.###.#.#########.#####.#.#.#######.#.###.#.###.#.#.#.###.#.#.#.#####.###.###.###.#.#####.#####.#####.###.#.#.# +#.....#.........#...#.#.#...#.#...#.#...#.#.....#...#...#.........#.#...#.#...#...#.....#.#.......#.....#...#...#.#...#.....#.......#.#.#.#.# +#.#####.#######.#.#.###.#.###.#.###.#.#.#.#.#.###.#.###############.#.#####.#.#.#########.#.#####.#####.#.#.###.###.#.#.###########.#.#.#.#.# +#.#...#.#...#...#.#.#...#...#.#.#.....#.#...#.#...#.......#.....#...#.......#.#...#.......#.#.....#...#.#.#...#.......#.#...#.......#...#.#.# +#.#.###.###.#.###.###.#######.#.#.#####.#####.#.#####.#.#.#.###.#.###########.###.#.#.#####.#.#.###.###.#####.#.#####.###.#.#.#.###.#.###.#.# +#...#...#...#.#.#...........#.#...#...#.#.....#.#...#.#.#.#...#.#.......#.....#.#.#.#...#.#...#.#.....#.#.....#...#.#.....#.#.#.#...#.#.#...# +#.###.###.#.#.#.#.#.#######.#.#####.#.#.#.#####.#.#.###.#.#####.#########.#####.#.#####.#.#####.#.###.#.#.#######.#.#######.#.#.###.#.#.##### +#.....#...#.#.#.#.#.......#.#...#...#.....#.....#.#.#...#.....#...........#...#...#...#.#.........#.......#.#.....#...#...#...#.....#.#.....# +#.#####.#####.#.#.#.#.###.#.###.#.#######.#.#####.#.#.#######.#.###.#####.###.#.###.#.#.###########.#######.#.#####.#.###.#########.#.#.##### +#.#.#.........#...#...#.....#...#.#.........#.....#.#.#.....#.#.#.#...........#.#...#.#...........#.........#...#...#...#.........#...#.....# +#.#.#.#######.#######.#.###.#.#.#.###.#######.#####.#.###.#.#.#.#.#.#.#######.#.###.#.#.#########.###.#.###.###.###.###.###.#####.#####.###.# +#.#...............#...#...#...#.#.............#.#...#...#.#.#...#.#.#.#...#.#.#.#...#.#.........#...#.#.......#.....#.#...#...#...#...#.#.#.# +#.###############.#.#.#########.###############.#.###.#.#.#.#####.#.#.#.#.#.#.#.#.#.#.###.#########.#.#.#####.#######.#.#.#.#.#####.#.#.#.#.# +#...#...#.......#.#...#.......#...#...#.......#...#.....#.#.....#...#.#.#.#...#...#...#...#.......#.#.#.....#...#...#.......#.......#.....#.# +#.#.#.#.#.###.###.#.###.#####.###.#.#.#.#.###.#.###.#####.###.###.###.#.#.#########.###.###.#####.#.#.#.###.#.###.#.#.#.###.###############.# +#.#...#.....#...#.#.....#.......#...#.#.#.#...#...#.#.....#...#.....#...#...#.......#...#.#.#.#...#.#.....#.#.#...#...#.#...#...........#...# +#.#######.#.#.#.#.#.#####.#####.#####.###.#######.#.#####.#.###.#####.#####.#.#######.###.#.#.#.###.#####.#.###.#######.#.###.#######.###.#.# +#.......#...#.#...#.#.....#...#.....#...........#.#.......#.....#...#...#...#.....#...#...#.#.....#.#.....#.....#.....#.....#.#.....#.#...#.# +#########.###.#######.#####.#.#########.#####.###.#.###.#.#####.#.#.###.#.#####.#.#.###.###.#####.#.#.###########.#.#.#####.#.#.###.#.#.###.# +#.....#...#...#.....#.......#...#.....#...#...#...#.....#.#...#.#.#...#.#.....#.#.........#.....#...#.....#.........#...#...#.#...#.#.#...#.# +#.###.#.#.#.###.###.#.#########.#.###.###.#.###.#######.#.#.#.###.###.###.###.#.###.#####.#####.#####.#.#.#.#######.###.#.###.#####.#.###.### +#...#...#.......#...............#...#.....#...#.#.....#.#...#...#.#.....#...#.#...#.....#.....#.....#...#.#.........#...#.....#...#.....#...# +#.#.#####.#####.#########.#####.###.#######.#.#.#####.#.#######.#.#####.#####.###.#####.#.###.#####.#####.###.#.#####.#########.#.#.#######.# +#.#...#...#...#.#...#.............#.....#...#.#.....#.......#...#...#.#.....#...#.#.......#.#.#...#.....#...#.#.#...#...#...#...#.#.#.....#.# +#.###.#####.#.###.#.#############.#####.#.###.#####.#######.#.#####.#.#####.###.#.#########.#.###.#####.#.#.###.#.#.###.#.#.#.###.#.#.###.#.# +#.#.#.......#.....#...........#.....#...#.#...........#...#.#.#.......#...#.....#.....#.....#.....#...#.#.......#.#...#...#...#.#.#.#...#.#.# +#.#.#####################.###.#####.#.###.#.###.#.###.#.#.###.#.#####.#.#.#####.#####.###.#.#####.#.#.#.#########.###.#########.#.#####.#.#.# +#.#.......#.......#.....#...#...#.....#...#...#...#.#...#.....#.#.....#.#...........#...#.#.....#.#.#.#.......#...#...#...#.....#.......#.#.# +#.#.#####.#.#.#####.###.#.#####.#####.#######.#.###.###########.###.###.###.#####.#.###.#.#.#####.#.#.#######.#####.###.#.#.#.#.#########.#.# +#.........#.#.....#...#.#.#...#.....#.........#.#.........#.............#.#...#.....#...#.#.....#.#.#.....#...#.....#...#...#.#.........#...# +#####.###.#######.###.#.###.#.#####.#.#########.###.#.###.#.#####.#####.#.#.#.#####.#.###.#####.#.#.###.###.###.#####.#######.#############.# +#.....#...........#...#.....#...........#.........#.#.#...#.#...#.#...#...#.#.....#.#.......#...#.#.#...#...#...#.....#...................#.# +#.#.#.#.#########.#.#####.#############.#####.###.###.#####.#.#.#.#.#.###.#.#####.#.###.###.#.###.#.#.###.###.###.#####.#####.#.#######.###.# +#.#.#.#...#...#...#.....#.#.......#...#.....#...#...#.#.....#.#.#...#...#.#.....#.....#.#.#.#...#.#.#...#.#.......#...#...#...#.#.....#.....# +#.#.#.#.###.#.#####.###.#.#.#####.#.#.#####.#.#####.#.#.#####.###.###.#.#.#####.###.###.#.#.###.#.###.#.#.#####.#####.###.#.###.#.###.####### +#.#.#.#.#...#.....#.#...#.#.#.....#.#.#.......#...#.#.#.......#.....#.#.#.#...#...#.....#.#...#.....#.#.#.#...#.#...#...#.#.#...#.#...#.....# +#.#.#.#.#.#######.#.#.#####.#.#.#####.#.#######.#.#.#.###.#####.###.#.#.#.###.###.#.#####.#.###.###.###.#.#.#.#.#.#.#.#.#.#.#.###.#.###.#.### +#.#.#...#.....#...#.#.......#.#.#...#...#...#...#...#.....#.....#...#.#.......#.#.#.......#.#...#.......#...#...#.#.......#.#.#...#...#.#...# +#.#.#.#####.#.#.#############.###.#.#.#.#.###.#######.#.#.#.#####.#####.#####.#.#.###.###.#.#.###.#.#########.#.#.#####.#.###.#.#####.#.###.# +#...............#...........#...#.#...#...#...#.....#.#...#.....#.....#...#.....#...#...#...#.......#.........#...#.....#.....#...#...#...#.# +#.#.#.###.#.#.#.###.#######.###.#.###.#####.###.#####.#########.#####.#.#.#########.###########.#####.#############.#.###########.#.#######.# +#.#...#.....#.#.....#.....#.....#...#.....#.#.#.......#.....#.#...#...#.#.........#...........#.#...#.#.....#...#...#.#...#.#.....#.#.......# +###.#.#.###.#.#######.#.#.#####.###.#####.#.#.#.#######.#.#.#.###.#.###.#######.#.###########.###.#.#.#.###.#.#.#.#.#.#.#.#.#.#.###.###.###.# +#...#...#.....#.....#.#.#.....#.#.#...#.#.#.#.#.#.......#.#.....#.#.#.......#...#.........#...#...#.#...#...#.#...#.#...#.#...#.#...#...#...# +#.###.###.#.###.###.#.#.#####.#.#.###.#.#.#.#.#.#####.#.#.#####.#.#.#########.#####.#####.#.###.###.#.#.#####.#####.#####.###.#.#.###.###.### +#.....#.........#.#.#.#.#.....#.#...#...#.#.#.#.#...#.#.#.#.....#.#.......#...#...#.#...#.#...#.#.#.#.........#...#.....#...#.#.#...#.#.....# +#.###.#.###.#.#.#.#.#.#.#####.#.#.#.###.#.#.#.#.#.#.#.#.#.#####.#.#######.#.#.#.#.###.#.#####.#.#.#.#.#####.#####.###.#.###.#.#.###.#.####### +#.......#...#.#.......#.#...#.#.#...#...#.#.#.....#.......#...#.#.......#...#.#.#.#...#...#...#.#.....#...#.......#.#.....#.#.#.#.#.#.......# +#####.#.#.#.#.###.#####.#.#.#.#.#.###.###.#.#####.###.#.###.#.###.#####.#####.#.#.#.#####.#.###.#######.#.#######.#.#####.#.###.#.#.###.###.# +#.#...#...#...#.......#...#.#.#...#...#...#.....#.#...#.#...#...#.#...#.....#...#.#.....#...#...#.....#.#...#...#.#...#...#.#...#.#.........# +#.#.#.###.#.#########.#.###.###.###.###.#######.###.#.###.#####.#.#.#.#####.#.###.#####.#####.###.###.#.###.#.#.#.#.#.#.###.#.###.#.###.###.# +#...#.#...#...#...#...#...#.#...#...#.#.....#.#.....#.....#...#.#.#.#.....#.#.#.....#.#.#.....#...#.#...#...#.#.#.#.#.#...#...#...#.......#.# +#.###.#.###.#.#.#.#.#.###.#.#.###.###.#.#.#.#.#######.#.#####.#.#.#.#######.#.#.###.#.#.###.#.#.###.#####.###.#.#.#.#.###.###.#.###.#.#.###.# +#...#...........#...#...#.#.......#.....#.#.........#.#.#.....#.#.#.#.....#...#.#.....#...#.#...#.......#.....#.#...#...#.....#...#.#.#.#...# +###.#.#####.###########.#.#######.#####.#.#########.#.#.#.#.###.#.#.#.###.#.#.#.#########.#.#####.#.#######.###.###########.###.#.#.#.#.#.### +#.#.#.............#.....#.#.......#...#.#.#.......#.#.#.#.#.....#.#.....#.#...#.......#...#...#...#.#.....#...#...........#.....#...#.#.#.#.# +#.#.#.#############.#####.#.#######.#.#.#.#.#####.###.#.#.#######.#.#####.###########.#.#####.#.#####.###.###.###########.#.#######.#.#.#.#.# +#...#.............#...#...#.........#.#.#...#...#.....#.#.#...#...#.#.......#.....#...#.....#.#.#.....#.#.#...#.......#...#.#.............#.# +#.###.###########.###.#####.#.#######.#.#####.#########.#.#.#.#.#####.#####.#####.#.#.#####.#.#.#.#####.#.#####.#####.#.###.###.###.#.#.###.# +#.#...........#.....#.......#.#.....#.#.................#.#.#.#.......#.....#...#.#.#.....#.#...#.......#...........#.#...#...#.#...#.#.#...# +#.###.#######.#.###.#########.###.###.#.#.#######.#######.#.#####.#####.#####.#.#.#.#.#####.#.#.###.#.#########.#.###.###.###.#.#.#.#.#.#.#.# +#...#.#.....#...#...#.......#...#.....#.#.#.....#...#.#...#...#.......#.....................#.....#.#.....#...#.....#.....#...#.#.#...#...#.# +###.#.#.#.#.#####.#####.###.###.#.#####.#.#.###.#.#.#.#.#####.#.#.###.#.#.#.#.#####.#.#######.###.#.###.#.#.#.#.###.#######.###.#.#.#.#####.# +#.#...#...#...#.#.....#...#.....................#.#.#.#.#...#.......#.....#...#.....#.#...#.....................................#.#.#.#...#.# +#.#######.#.#.#.#####.###.###.#.#########.###.#.###.#.#.#.#.#####.###########.#.###.#.#.#.#.#.#.#####.#.#####.#.#.#####.#####.#.#.#.#.#.###.# +#.....#...#.#.#.....#...#...#.#.....#.#...#...#.........#.#.........................#...#...#.#.#...#.#...#...#.................#.#...#.#...# +#.#####.#.#.#.#.#######.#.#.#.#####.#.#.###.###.#.#####.#.#######.#.###.#.#.#.###.#.#.#######.#.#.#.#.#.###.#.#.###.#.###.#.###.#.#.###.#.### +#...#...#.#.#...#.......#.#.#.......#.......#.....#...#.#.#.....#.#...#.#.#.....#.#.........#...#.#...#.#...#.#.#.#.#.#...#...#.#.#.....#.#.# +#.#.#.###.#.###.#.#########.#####.#.###############.#.#.#.#.#.#.#.###.#.#########.#########.#.###.#####.#.###.#.#.#.###.#####.#.#.#####.#.#.# +#.#.#.....#.........................................#.#...#.#...#.....#.........#.#.....#...#.#...#.....#.#...#.#.#...#.#...#.........#.#.#.# +#.#.#####.###.#.#.#.#.#.#####.#.#.#.#########.#.#####.#######.#.###############.#.#.#.#.#.###.###.#######.#.###.#.###.#.#.###########.###.#.# +#.#.......#.#.#.#.#...#...#...#.#.#.#.......#...........................#.....#.#.#.#.#.#...#...#...#.....#.#...#.#...#...........#...#...#.# +#.#########.#.###.#######.#.#.###.###.#####.###########.#.#.#.#######.#.#.#.###.#.#.#.#####.#.#.###.#.#######.###.#.###.###.#####.#.###.###.# +#S........................#.#.........#.................#.............#...#.......#.#.........#.....#.............#.........#.....#.........# +############################################################################################################################################# \ No newline at end of file diff --git a/lib/solutions/day_16.rb b/lib/solutions/day_16.rb new file mode 100644 index 0000000..7c2f8c2 --- /dev/null +++ b/lib/solutions/day_16.rb @@ -0,0 +1,117 @@ +require 'set' + +class Day16 + class DayOne + DIRECTIONS = [ + [0, -1], # North + [1, 0], # East + [0, 1], # South + [-1, 0] # West + ] + + TURN_COST = 1000 + MOVE_COST = 1 + + def initialize(input) + @grid = input.split("\n") + @rows = @grid.size + @cols = @grid[0].size + end + + def run! + # Run, Dijkstra, Run! + start_xy = find_cell('S') + end_xy = find_cell('E') + + direction = 1 # East + + # 3D array to keep track of distances from each cell + distances = Array.new(@cols) { Array.new(@rows) { Array.new(4, Float::INFINITY) } } + + # Start cell + distances[start_xy[0]][start_xy[1]][direction] = 0 + + # Priority queue for [cost, x, y, direction] + p_queue = [] + p_queue << [0, start_xy[0], start_xy[1], direction] + + while !p_queue.empty? + # Sort priority queueu by cost + p_queue.sort_by! { |a| a[0] } + + # Get next items from queue + cost, x, y, direction = p_queue.shift + + # Reached end + return cost if x == end_xy[0] && y == end_xy[1] + + # Skip if we already have a lower cost to this cell + next if distances[x][y][direction] < cost + + # Try moving forward + new_x = x + DIRECTIONS[direction][0] + new_y = y + DIRECTIONS[direction][1] + + if can_move?(new_x, new_y) + new_cost = cost + MOVE_COST + if new_cost < distances[new_x][new_y][direction] + distances[new_x][new_y][direction] = new_cost + p_queue << [new_cost, new_x, new_y, direction] + end + end + + # Try turning + # Left + left_dir = (direction - 1) % 4 + new_cost = cost + TURN_COST + # Turning costs, even if we do not move. But only do it if moving + # afterwards makes sense + if new_cost < distances[x][y][left_dir] + distances[x][y][left_dir] = new_cost + p_queue << [new_cost, x, y, left_dir] + end + + # Right + right_dir = (direction + 1) % 4 + new_cost = cost + TURN_COST + + if new_cost < distances[x][y][right_dir] + distances[x][y][right_dir] = new_cost + p_queue << [new_cost, x, y, right_dir] + end + end + + # Can't move forward... + return Float::INFINITY + end + + def can_move?(x, y) + return false if x < 0 || x >= @cols + return false if y < 0 || y >= @rows + return false if @grid[y][x] == '#' + true + end + + + def find_cell(ch) + @grid.each_with_index do |row, y| + x = row.index(ch) + if x + puts "Found #{ch} at #{x}, #{y}" + return [x, y] + end + end + nil + end + end + + def part_one(input) + sim = DayOne.new(input) + sim.run! + end + + def part_two(input) + 0 + end +end + diff --git a/spec/input/day_16_test.txt b/spec/input/day_16_test.txt new file mode 100644 index 0000000..6a5bb85 --- /dev/null +++ b/spec/input/day_16_test.txt @@ -0,0 +1,15 @@ +############### +#.......#....E# +#.#.###.#.###.# +#.....#.#...#.# +#.###.#####.#.# +#.#.#.......#.# +#.#.#####.###.# +#...........#.# +###.#.#####.#.# +#...#.....#.#.# +#.#.#.###.#.#.# +#.....#...#.#.# +#.###.#.#.#.#.# +#S..#.....#...# +############### \ No newline at end of file diff --git a/spec/solutions/day_16_spec.rb b/spec/solutions/day_16_spec.rb new file mode 100644 index 0000000..ff188ec --- /dev/null +++ b/spec/solutions/day_16_spec.rb @@ -0,0 +1,61 @@ +require "spec_helper" +require "solutions/day_16" + +RSpec.describe Day16 do + let(:input) { File.read(File.join(__dir__, '..', '..', 'spec', 'input', 'day_16_test.txt')).strip } + + describe '#part_one' do + it 'calculates example 1' do + expect(subject.part_one( +<<-INPUT +############### +#.......#....E# +#.#.###.#.###.# +#.....#.#...#.# +#.###.#####.#.# +#.#.#.......#.# +#.#.#####.###.# +#...........#.# +###.#.#####.#.# +#...#.....#.#.# +#.#.#.###.#.#.# +#.....#...#.#.# +#.###.#.#.#.#.# +#S..#.....#...# +############### +INPUT + )).to eq(7036) + end + + it 'calculates example 2' do + expect(subject.part_one( +<<-INPUT +################# +#...#...#...#..E# +#.#.#.#.#.#.#.#.# +#.#.#.#...#...#.# +#.#.#.#.###.#.#.# +#...#.#.#.....#.# +#.#.#.#.#.#####.# +#.#...#.#.#.....# +#.#.#####.#.###.# +#.#.#.......#...# +#.#.###.#####.### +#.#.#...#.....#.# +#.#.#.#####.###.# +#.#.#.........#.# +#.#.#.#########.# +#S#.............# +################# +INPUT + )).to eq(11048) + end + end + + describe '#part_two' do + it 'calculates the correct solutions for part two' do + expect(subject.part_two(input)).to eq(0) + end + end +end +