From 1e8a87ecdaeec9d850bd1bde623c0488a7640660 Mon Sep 17 00:00:00 2001 From: Andrej Orsula Date: Fri, 20 Dec 2024 16:35:41 +0100 Subject: [PATCH] aoc2024/day20: Add solution Signed-off-by: Andrej Orsula --- README.md | 2 +- aoc2024/input/2024/day20.txt | 141 ++++++++++++++++++++++ aoc2024/src/day20.rs | 225 +++++++++++++++++++++++++++++++++++ aoc2024/src/lib.rs | 2 +- 4 files changed, 368 insertions(+), 2 deletions(-) create mode 100644 aoc2024/input/2024/day20.txt create mode 100644 aoc2024/src/day20.rs diff --git a/README.md b/README.md index 05cc953..34d2b27 100644 --- a/README.md +++ b/README.md @@ -36,8 +36,8 @@ My solutions to [Advent of Code](https://adventofcode.com) puzzles. | 17 | [Chronospatial Computer](https://adventofcode.com/2024/day/17) | [day17.rs](aoc2024/src/day17.rs) | 2.124 µs | 26.82 µs | | 18 | [RAM Run](https://adventofcode.com/2024/day/18) | [day18.rs](aoc2024/src/day18.rs) | 219.9 µs | 291.5 µs | | 19 | [Linen Layout](https://adventofcode.com/2024/day/19) | [day19.rs](aoc2024/src/day19.rs) | 8.696 ms | 8.696 ms | +| 20 | [Race Condition](https://adventofcode.com/2024/day/20) | [day20.rs](aoc2024/src/day20.rs) | 377.8 µs | 19.90 ms | - diff --git a/aoc2024/input/2024/day20.txt b/aoc2024/input/2024/day20.txt new file mode 100644 index 0000000..b9b1a9f --- /dev/null +++ b/aoc2024/input/2024/day20.txt @@ -0,0 +1,141 @@ +############################################################################################################################################# +#.....#.........#...#...#.........###.....###...#.......#...###...#.......###...#...#.....#...###.....#...###...###...#.......#...#...###...# +#.###.#.#######.#.#.#.#.#.#######.###.###.###.#.#.#####.#.#.###.#.#.#####.###.#.#.#.#.###.#.#.###.###.#.#.###.#.###.#.#.#####.#.#.#.#.###.#.# +#...#.#...#...#...#...#...#.....#...#...#.....#.#.#.....#.#.#...#.#.#.....#...#...#.#...#.#.#...#...#...#...#.#.#...#.#.#.....#.#.#.#.....#.# +###.#.###.#.#.#############.###.###.###.#######.#.#.#####.#.#.###.#.#.#####.#######.###.#.#.###.###.#######.#.#.#.###.#.#.#####.#.#.#######.# +#...#...#.#.#...#...#.......###...#...#...#.....#.#.#...#.#.#.#...#.#.....#.....#...#...#.#...#.###.#.......#.#.#...#.#.#.....#.#.#.#.....#.# +#.#####.#.#.###.#.#.#.###########.###.###.#.#####.#.#.#.#.#.#.#.###.#####.#####.#.###.###.###.#.###.#.#######.#.###.#.#.#####.#.#.#.#.###.#.# +#...#...#...###...#...#.....#.....#...#...#.....#.#.#.#.#.#.#.#.....#.....#...#.#.###...#.#...#...#.#...#...#.#.#...#.#...#...#.#...#...#...# +###.#.#################.###.#.#####.###.#######.#.#.#.#.#.#.#.#######.#####.#.#.#.#####.#.#.#####.#.###.#.#.#.#.#.###.###.#.###.#######.##### +#...#...#...#...#...###...#.#.....#.#...#.......#.#.#.#.#.#.#.#.......#.....#.#.#...###.#.#.#...#...#...#.#.#.#...#...###.#...#...#.....#...# +#.#####.#.#.#.#.#.#.#####.#.#####.#.#.###.#######.#.#.#.#.#.#.#.#######.#####.#.###.###.#.#.#.#.#####.###.#.#.#####.#####.###.###.#.#####.#.# +#.#.....#.#...#.#.#.#...#.#.#.....#.#.#...#...###.#...#...#...#.#...###.#.....#.#...#...#.#...#.....#.###.#.#.....#.#...#.#...#...#.....#.#.# +#.#.#####.#####.#.#.#.#.#.#.#.#####.#.#.###.#.###.#############.#.#.###.#.#####.#.###.###.#########.#.###.#.#####.#.#.#.#.#.###.#######.#.#.# +#.#.#...#.....#...#.#.#.#.#.#.....#.#.#.###.#.#...#...#.........#.#.#...#.....#.#...#...#...#...#...#.#...#.#...#.#.#.#.#.#.....#.....#...#.# +#.#.#.#.#####.#####.#.#.#.#.#####.#.#.#.###.#.#.###.#.#.#########.#.#.#######.#.###.###.###.#.#.#.###.#.###.#.#.#.#.#.#.#.#######.###.#####.# +#.#...#.#...#.#.....#.#.#.#.#...#.#.#.#.#...#...#...#...#...#...#.#.#.....#...#.#...###...#.#.#.#...#.#.#...#.#...#...#...#.......###.......# +#.#####.#.#.#.#.#####.#.#.#.#.#.#.#.#.#.#.#######.#######.#.#.#.#.#.#####.#.###.#.#######.#.#.#.###.#.#.#.###.#############.################# +#.....#.#.#...#.....#.#...#.#.#...#...#.#.#.......#...###.#...#.#.#...#...#...#.#...#...#.#.#.#.#...#.#.#.#...#...#...#...#.#...#.....#.....# +#####.#.#.#########.#.#####.#.#########.#.#.#######.#.###.#####.#.###.#.#####.#.###.#.#.#.#.#.#.#.###.#.#.#.###.#.#.#.#.#.#.#.#.#.###.#.###.# +###...#.#.........#.#.....#.#.........#.#.#...#...#.#...#.....#.#.#...#.....#.#.#...#.#...#.#.#.#...#.#.#...#...#...#...#...#.#...#...#...#.# +###.###.#########.#.#####.#.#########.#.#.###.#.#.#.###.#####.#.#.#.#######.#.#.#.###.#####.#.#.###.#.#.#####.###############.#####.#####.#.# +#...#...#...#...#.#.#...#.#...#.......#...#...#.#...#...###...#.#.#...#.....#...#...#...#...#.#.#...#.#...#...###...###...###.#.....#.....#.# +#.###.###.#.#.#.#.#.#.#.#.###.#.###########.###.#####.#####.###.#.###.#.###########.###.#.###.#.#.###.###.#.#####.#.###.#.###.#.#####.#####.# +#...#...#.#.#.#...#.#.#.#.#...#...........#...#...#...#...#...#.#.#...#.........#...#...#.#...#.#.#...#...#.....#.#.#...#.....#.....#.#.....# +###.###.#.#.#.#####.#.#.#.#.#############.###.###.#.###.#.###.#.#.#.###########.#.###.###.#.###.#.#.###.#######.#.#.#.#############.#.#.##### +###...#...#...#...#...#.#.#.#.....###...#.#...#...#...#.#.#...#...#...###...###.#.#...#...#...#...#...#...#.....#.#.#.#...#...#...#...#.....# +#####.#########.#.#####.#.#.#.###.###.#.#.#.###.#####.#.#.#.#########.###.#.###.#.#.###.#####.#######.###.#.#####.#.#.#.#.#.#.#.#.#########.# +#...#.....#.....#.....#.#.#.#...#.#...#...#...#.#...#.#.#.#.........#.#...#.#...#.#...#.#...#...#.....#...#...###.#.#.#.#...#.#.#.#...#.....# +#.#.#####.#.#########.#.#.#.###.#.#.#########.#.#.#.#.#.#.#########.#.#.###.#.###.###.#.#.#.###.#.#####.#####.###.#.#.#.#####.#.#.#.#.#.##### +#.#.......#.#.......#.#.#.#...#.#.#.......#...#.#.#...#.#...###...#.#.#...#.#...#.#...#...#.#...#.#...#.#...#.#...#...#...###...#...#.#.....# +#.#########.#.#####.#.#.#.###.#.#.#######.#.###.#.#####.###.###.#.#.#.###.#.###.#.#.#######.#.###.#.#.#.#.#.#.#.#########.###########.#####.# +#.........#.#.#...#...#.#.#...#.#.#...#...#.#...#.#...#.#...#...#.#.#.#...#.#...#...#.......#...#.#.#.#.#.#...#.#.......#...........#.......# +#########.#.#.#.#.#####.#.#.###.#.#.#.#.###.#.###.#.#.#.#.###.###.#.#.#.###.#.#######.#########.#.#.#.#.#.#####.#.#####.###########.######### +#.........#.#...#.....#.#.#.#...#...#.#...#.#...#.#.#.#.#.###...#.#.#.#.#...#.......#.#...#...#.#.#.#.#.#.###...#.....#.#...#.......###.....# +#.#########.#########.#.#.#.#.#######.###.#.###.#.#.#.#.#.#####.#.#.#.#.#.#########.#.#.#.#.#.#.#.#.#.#.#.###.#######.#.#.#.#.#########.###.# +#.........#.#.........#...#.#.......#...#.#.#...#...#.#.#...#...#.#.#.#.#...#...#...#.#.#.#.#.#.#.#.#...#.....#.....#.#...#...#.......#.#...# +#########.#.#.#############.#######.###.#.#.#.#######.#.###.#.###.#.#.#.###.#.#.#.###.#.#.#.#.#.#.#.###########.###.#.#########.#####.#.#.### +#.........#.#.............#.#...###.###...#...###...#.#.#...#...#.#.#.#.#...#.#...###.#.#.#.#.#.#.#.#.........#...#...#...#...#.#...#...#...# +#.#########.#############.#.#.#.###.#############.#.#.#.#.#####.#.#.#.#.#.###.#######.#.#.#.#.#.#.#.#.#######.###.#####.#.#.#.#.#.#.#######.# +#...........###...#...#...#...#.....#.............#.#.#.#.#...#.#...#...#...#.......#.#.#.#.#.#.#.#.#.......#.....#.....#...#...#.#.........# +###############.#.#.#.#.#############.#############.#.#.#.#.#.#.###########.#######.#.#.#.#.#.#.#.#.#######.#######.#############.########### +#.....#...#.....#...#...#...#...#.....#.......#.....#.#.#.#.#.#.....#.......#...#...#...#...#.#.#...#...#...#...###.#.........#...#...#...### +#.###.#.#.#.#############.#.#.#.#.#####.#####.#.#####.#.#.#.#.#####.#.#######.#.#.###########.#.#####.#.#.###.#.###.#.#######.#.###.#.#.#.### +#...#.#.#.#.........#...#.#.#.#...#...#.....#.#...###.#.#...#.#...#.#...#.....#.#...........#.#.#.....#...###.#.#...#.......#...#...#...#...# +###.#.#.#.#########.#.#.#.#.#.#####.#.#####.#.###.###.#.#####.#.#.#.###.#.#####.###########.#.#.#.###########.#.#.#########.#####.#########.# +#...#...#.#.........#.#...#...###...#.......#...#...#.#.....#.#.#.#.###.#.#...#.#.......#...#...#.#.........#.#...#...#...#.....#.#.........# +#.#######.#.#########.###########.#############.###.#.#####.#.#.#.#.###.#.#.#.#.#.#####.#.#######.#.#######.#.#####.#.#.#.#####.#.#.######### +#.......#.#.......#...#.........#.............#...#.#.#...#.#.#.#...#...#...#.#.#...###.#.......#.#.#.......#.#.....#...#.#.....#.#.#...#...# +#######.#.#######.#.###.#######.#############.###.#.#.#.#.#.#.#.#####.#######.#.###.###.#######.#.#.#.#######.#.#########.#.#####.#.#.#.#.#.# +#...###.#.#...#...#.###.......#.........#...#...#.#.#.#.#.#.#.#.#.....#.......#.#...#...#...#...#...#.........#...#.....#...#.....#...#...#.# +#.#.###.#.#.#.#.###.#########.#########.#.#.###.#.#.#.#.#.#.#.#.#.#####.#######.#.###.###.#.#.###################.#.###.#####.#############.# +#.#.....#...#.#...#.#.....#...#.......#.#.#.#...#.#.#.#.#.#.#.#.#.#...#.......#.#...#...#.#...###...#...#...#.....#.#...#...#.#.....#...#...# +#.###########.###.#.#.###.#.###.#####.#.#.#.#.###.#.#.#.#.#.#.#.#.#.#.#######.#.###.###.#.#######.#.#.#.#.#.#.#####.#.###.#.#.#.###.#.#.#.### +#...#.....#...###...#...#...#...#.....#...#...###...#...#...#...#.#.#.....#...#.#...###.#...#.....#...#...#.#.......#.....#...#...#.#.#.#.### +###.#.###.#.###########.#####.###.###############################.#.#####.#.###.#.#####.###.#.#############.#####################.#.#.#.#.### +#...#.#...#.#.......###...#...###...........###...........#.......#...#...#.###.#.....#.....#.............#.......#.....#...#.....#.#.#.#...# +#.###.#.###.#.#####.#####.#.###############.###.#########.#.#########.#.###.###.#####.###################.#######.#.###.#.#.#.#####.#.#.###.# +#.....#...#.#...#...#...#...###...#.......#.....#.........#.#.....#...#...#...#.#.....###...#...#...#...#.......#.#...#.#.#.#.....#.#.#.#...# +#########.#.###.#.###.#.#######.#.#.#####.#######.#########.#.###.#.#####.###.#.#.#######.#.#.#.#.#.#.#.#######.#.###.#.#.#.#####.#.#.#.#.### +#.........#...#.#...#.#.#.......#...#...#.......#.........#...###...#####...#.#.#.#.......#...#...#.#.#.#...#...#.#...#.#.#.......#...#.#.### +#.###########.#.###.#.#.#.###########.#.#######.#########.#################.#.#.#.#.###############.#.#.#.#.#.###.#.###.#.#############.#.### +#.#.........#...#...#.#.#...........#.#.#.....#.#.........#.....#...###.....#.#...#.#.............#...#...#...###...#...#.........###...#...# +#.#.#######.#####.###.#.###########.#.#.#.###.#.#.#########.###.#.#.###.#####.#####.#.###########.###################.###########.###.#####.# +#.#.#.....#.....#.###.#.###.......#...#...#...#...###...###...#...#...#...#...#...#.#.#.........#...................#...#...#...#...#.......# +#.#.#.###.#####.#.###.#.###.#####.#########.#########.#.#####.#######.###.#.###.#.#.#.#.#######.###################.###.#.#.#.#.###.######### +#...#...#.......#...#.#.#...#...#.#...#...#.........#.#.#...#.#...#...###...#...#.#...#.......#.....................###...#...#.....#.......# +#######.###########.#.#.#.###.#.#.#.#.#.#.#########.#.#.#.#.#.#.#.#.#########.###.###########.#######################################.#####.# +#...###...........#...#...#...#.#...#...#...#.....#...#...#.#.#.#...###.......#...#.....#...#.....#...........#...###...#...#...#...#.#.....# +#.#.#############.#########.###.###########.#.###.#########.#.#.#######.#######.###.###.#.#.#####.#.#########.#.#.###.#.#.#.#.#.#.#.#.#.##### +#.#.#.............#.....#...###...........#.#...#...#.....#...#.......#...#...#...#...#.#.#.#...#...#.........#.#.....#...#...#...#...#.....# +#.#.#.#############.###.#.###############.#.###.###.#.###.###########.###.#.#.###.###.#.#.#.#.#.#####.#########.###########################.# +#.#.#...........#...###...#...........#...#...#.###...###.#...#.......#...#.#...#.#...#...#...#.#.....#.........#.....#.....#.........#.....# +#.#.###########.#.#########.#########.#.#####.#.#########.#.#.#.#######.###.###.#.#.###########.#.#####.#########.###.#.###.#.#######.#.##### +#.#...###...###.#.#.......#.........#...#...#...#...#...#...#.#.......#.#...###.#...#......E#...#.#.....#...#.....###...#...#.......#.#.....# +#.###.###.#.###.#.#.#####.#########.#####.#.#####.#.#.#.#####.#######.#.#.#####.#####.#######.###.#.#####.#.#.###########.#########.#.#####.# +#...#.#...#...#...#...#...#...#...#.......#.......#...#.....#...#.....#...###...#...#.#######...#...#.....#.#...#.......#...#...#...#...#...# +###.#.#.#####.#######.#.###.#.#.#.#########################.###.#.###########.###.#.#.#########.#####.#####.###.#.#####.###.#.#.#.#####.#.### +#...#.#...#...###.....#.#...#...#.............#...#...#...#...#...###...#...#.....#...#########.......#####...#...#.....###...#...###...#...# +#.###.###.#.#####.#####.#.###################.#.#.#.#.#.#.###.#######.#.#.#.#################################.#####.#################.#####.# +#...#.....#.....#.....#...#.....#.......#...#...#...#...#...#...#...#.#.#.#.....#############################.#.....#...#...........#.#.....# +###.###########.#####.#####.###.#.#####.#.#.###############.###.#.#.#.#.#.#####.#############################.#.#####.#.#.#########.#.#.##### +###...#...#...#...#...#...#.###...#.....#.#.............#...#...#.#...#.#...#...#############################...#...#.#...#...#...#.#.#.....# +#####.#.#.#.#.###.#.###.#.#.#######.#####.#############.#.###.###.#####.###.#.###################################.#.#.#####.#.#.#.#.#.#####.# +#.....#.#...#...#...#...#...#...###.......#...#.....#...#.....###.....#.....#...#...#######################...###.#.#.#.....#.#.#.#.#.#.....# +#.#####.#######.#####.#######.#.###########.#.#.###.#.###############.#########.#.#.#######################.#.###.#.#.#.#####.#.#.#.#.#.##### +#.#.....#...###...#...#...###.#.#.....#.....#...#...#...#...#####...#.......#...#.#.#.....#################.#.#...#.#.#.#...#...#.#.#.#.....# +#.#.#####.#.#####.#.###.#.###.#.#.###.#.#########.#####.#.#.#####.#.#######.#.###.#.#.###.#################.#.#.###.#.#.#.#.#####.#.#.#####.# +#.#.#.....#.......#.#...#.....#.#...#.#.........#.......#.#...#...#...#.....#.#...#.#...#.###...#.......#S..#...#...#.#...#...###.#.#...#...# +#.#.#.#############.#.#########.###.#.#########.#########.###.#.#####.#.#####.#.###.###.#.###.#.#.#####.#########.###.#######.###.#.###.#.### +#...#.........#.....#.#...#...#.....#.#.......#...###...#.#...#.....#...#...#.#...#...#.#.#...#.#.....#.#...#...#.....#.....#...#...###...### +#############.#.#####.#.#.#.#.#######.#.#####.###.###.#.#.#.#######.#####.#.#.###.###.#.#.#.###.#####.#.#.#.#.#.#######.###.###.############# +#.............#.#.....#.#.#.#.......#.#.....#.....#...#...#.....###...#...#.#.###...#.#.#.#...#.#.....#...#...#...#...#...#...#...###.......# +#.#############.#.#####.#.#.#######.#.#####.#######.###########.#####.#.###.#.#####.#.#.#.###.#.#.###############.#.#.###.###.###.###.#####.# +#...#...#...#...#.....#.#.#.#.......#.....#.......#.......#...#.#...#...#...#.#...#.#...#...#.#.#.....#...#.....#...#.#...###...#...#.#.....# +###.#.#.#.#.#.#######.#.#.#.#.###########.#######.#######.#.#.#.#.#.#####.###.#.#.#.#######.#.#.#####.#.#.#.###.#####.#.#######.###.#.#.##### +###...#...#...#...###...#...#...........#.........#...#...#.#...#.#.#...#...#.#.#.#.....#...#.#.....#...#...#...#...#...#...###...#...#.....# +###############.#.#####################.###########.#.#.###.#####.#.#.#.###.#.#.#.#####.#.###.#####.#########.###.#.#####.#.#####.#########.# +#...###.......#.#.#...###...............###...#...#.#.#...#...###.#.#.#.#...#...#.....#.#...#.#...#...###...#.....#.......#...###.#...#...#.# +#.#.###.#####.#.#.#.#.###.#################.#.#.#.#.#.###.###.###.#.#.#.#.###########.#.###.#.#.#.###.###.#.#################.###.#.#.#.#.#.# +#.#.#...#...#...#...#...#...............#...#.#.#.#.#...#.#...#...#...#.#...#.........#.#...#...#.#...#...#...#.........#...#...#...#.#.#...# +#.#.#.###.#.###########.###############.#.###.#.#.#.###.#.#.###.#######.###.#.#########.#.#######.#.###.#####.#.#######.#.#.###.#####.#.##### +#.#...#...#...#...#...#.....###...#.....#...#...#.#...#...#.###.....#...#...#.#.....#...#...#.....#...#.....#.#.......#.#.#.#...#...#.#.....# +#.#####.#####.#.#.#.#.#####.###.#.#.#######.#####.###.#####.#######.#.###.###.#.###.#.#####.#.#######.#####.#.#######.#.#.#.#.###.#.#.#####.# +#.......#...#...#...#.#...#.....#...###...#.#.....###...#...#...###.#.###...#.#.#...#...#...#.....#...###...#.#...#...#.#.#.#.....#.#...#...# +#########.#.#########.#.#.#############.#.#.#.#########.#.###.#.###.#.#####.#.#.#.#####.#.#######.#.#####.###.#.#.#.###.#.#.#######.###.#.### +###...#...#.........#...#...#...#.....#.#.#.#.#.....#...#...#.#.#...#.#.....#.#.#...#...#.#...#...#...#...###...#...###...#.......#...#.#...# +###.#.#.###########.#######.#.#.#.###.#.#.#.#.#.###.#.#####.#.#.#.###.#.#####.#.###.#.###.#.#.#.#####.#.#########################.###.#.###.# +#...#...#.........#.###.....#.#.#...#.#.#...#.#.###...###...#.#...#...#.....#.#.#...#...#.#.#.#...#...#.............#...........#.....#.....# +#.#######.#######.#.###.#####.#.###.#.#.#####.#.#########.###.#####.#######.#.#.#.#####.#.#.#.###.#.###############.#.#########.############# +#.#.....#.#.......#.....#...#.#.#...#.#.#.....#.....#...#...#.....#...#.....#.#.#.#...#.#.#.#...#.#.#...#...........#.#.......#.....###.....# +#.#.###.#.#.#############.#.#.#.#.###.#.#.#########.#.#.###.#####.###.#.#####.#.#.#.#.#.#.#.###.#.#.#.#.#.###########.#.#####.#####.###.###.# +#.#.#...#.#...#...#.....#.#.#.#...#...#.#.....#...#...#...#...#...#...#.....#...#...#.#.#...#...#.#.#.#.#.............#...###.#...#...#.#...# +#.#.#.###.###.#.#.#.###.#.#.#.#####.###.#####.#.#.#######.###.#.###.#######.#########.#.#####.###.#.#.#.#################.###.#.#.###.#.#.### +#.#.#...#.###.#.#.#.#...#.#.#.#.....#...#.....#.#.#.......#...#.#...#.......#.........#.....#...#.#.#.#.#...#.......#...#...#...#...#...#...# +#.#.###.#.###.#.#.#.#.###.#.#.#.#####.###.#####.#.#.#######.###.#.###.#######.#############.###.#.#.#.#.#.#.#.#####.#.#.###.#######.#######.# +#.#.#...#...#.#.#.#.#...#.#.#.#...#...###.....#.#.#.......#.....#...#.......#...#.....#...#.###...#.#.#.#.#.#.....#...#...#...#...#.......#.# +#.#.#.#####.#.#.#.#.###.#.#.#.###.#.#########.#.#.#######.#########.#######.###.#.###.#.#.#.#######.#.#.#.#.#####.#######.###.#.#.#######.#.# +#...#...#...#...#...###.#.#.#.#...#.....#.....#.#.#...#...#.........#.......###.#...#.#.#...#.......#.#...#.#...#.......#...#...#.......#.#.# +#######.#.#############.#.#.#.#.#######.#.#####.#.#.#.#.###.#########.#########.###.#.#.#####.#######.#####.#.#.#######.###.###########.#.#.# +#.......#...###.........#.#.#.#...#...#.#.#...#.#...#.#...#...#.....#.......#...#...#.#.....#.#.....#.....#.#.#.........###...#.........#...# +#.#########.###.#########.#.#.###.#.#.#.#.#.#.#.#####.###.###.#.###.#######.#.###.###.#####.#.#.###.#####.#.#.###############.#.############# +#.......#...#...#...#...#.#.#...#.#.#...#...#.#.....#.....###.#.#...#...#...#.#...###.#.....#...###...#...#.#...........###...#.............# +#######.#.###.###.#.#.#.#.#.###.#.#.#########.#####.#########.#.#.###.#.#.###.#.#####.#.#############.#.###.###########.###.###############.# +#...#...#.#...#...#.#.#.#.#...#.#.#.###.......#...#.........#.#.#...#.#...#...#.#.....#.......#.......#...#...#.......#...#...#.....#...#...# +#.#.#.###.#.###.###.#.#.#.###.#.#.#.###.#######.#.#########.#.#.###.#.#####.###.#.###########.#.#########.###.#.#####.###.###.#.###.#.#.#.### +#.#...#...#.#...#...#.#.#...#...#...#...#.....#.#.#.....#...#.#.#...#.....#...#.#.......#.....#...#...#...###.#.....#.#...###.#...#.#.#...### +#.#####.###.#.###.###.#.###.#########.###.###.#.#.#.###.#.###.#.#.#######.###.#.#######.#.#######.#.#.#.#####.#####.#.#.#####.###.#.#.####### +#.....#.###.#...#...#.#...#...###...#.....#...#.#.#.###...###.#.#...#...#.###...#.......#.......#.#.#.#.....#.#.....#.#.#.....#...#.#.....### +#####.#.###.###.###.#.###.###.###.#.#######.###.#.#.#########.#.###.#.#.#.#######.#############.#.#.#.#####.#.#.#####.#.#.#####.###.#####.### +#.....#...#...#.#...#.#...#...#...#.........#...#.#...#.....#...#...#.#.#.#.......#...#.....#...#...#.#.....#...#...#...#.....#.###.#...#...# +#.#######.###.#.#.###.#.###.###.#############.###.###.#.###.#####.###.#.#.#.#######.#.#.###.#.#######.#.#########.#.#########.#.###.#.#.###.# +#.#.....#...#.#.#.....#...#.###.......#...#...###...#...#...###...#...#.#.#.#.....#.#.#...#.#.....#...#...........#.........#.#...#.#.#...#.# +#.#.###.###.#.#.#########.#.#########.#.#.#.#######.#####.#####.###.###.#.#.#.###.#.#.###.#.#####.#.#######################.#.###.#.#.###.#.# +#.#.#...#...#...###.......#...###.....#.#.#.#.......#...#.....#...#...#.#.#...#...#.#.#...#.#.....#...#...#...#...#...#...#.#.#...#...###...# +#.#.#.###.#########.#########.###.#####.#.#.#.#######.#.#####.###.###.#.#.#####.###.#.#.###.#.#######.#.#.#.#.#.#.#.#.#.#.#.#.#.############# +#.#.#...#.###.......#...#...#.#...#...#.#.#.#.#...#...#.......#...#...#.#.....#.....#.#...#.#.......#...#...#.#.#.#.#...#.#.#.#.............# +#.#.###.#.###.#######.#.#.#.#.#.###.#.#.#.#.#.#.#.#.###########.###.###.#####.#######.###.#.#######.#########.#.#.#.#####.#.#.#############.# +#.#.#...#...#.#...#...#.#.#.#.#...#.#.#.#.#.#.#.#.#...........#.#...#...#...#.#.......#...#.#.....#.....#.....#.#.#...#...#.#.#...#...#...#.# +#.#.#.#####.#.#.#.#.###.#.#.#.###.#.#.#.#.#.#.#.#.###########.#.#.###.###.#.#.#.#######.###.#.###.#####.#.#####.#.###.#.###.#.#.#.#.#.#.#.#.# +#...#.......#...#...###...#...###...#...#...#...#.............#...###.....#...#.........###...###.......#.......#.....#.....#...#...#...#...# +############################################################################################################################################# diff --git a/aoc2024/src/day20.rs b/aoc2024/src/day20.rs new file mode 100644 index 0000000..709349e --- /dev/null +++ b/aoc2024/src/day20.rs @@ -0,0 +1,225 @@ +use aoc_runner_derive::aoc; +use strum::IntoEnumIterator; + +#[aoc(day20, part1)] +#[must_use] +pub fn part1(input: &str) -> usize { + Maze::<141>::parse(input).solve1::<100>() +} + +#[aoc(day20, part2)] +#[must_use] +pub fn part2(input: &str) -> usize { + Maze::<141>::parse(input).solve2::<100>() +} + +struct Maze { + map: nalgebra::SMatrix, + start_pos: (usize, usize), + goal_pos: (usize, usize), +} + +impl Maze { + fn parse(input: &str) -> Self { + let input = input.as_bytes(); + let width = unsafe { input.iter().position(|&c| c == b'\n').unwrap_unchecked() }; + let mut map = nalgebra::SMatrix::from_element(b'.'); + let mut start_pos = (0, 0); + let mut goal_pos = (0, 0); + input + .chunks(unsafe { width.unchecked_add(1) }) + .enumerate() + .for_each(|(y, line)| { + line.iter() + .enumerate() + .take(width) + .for_each(|(x, &b)| match b { + b'S' => start_pos = (x, y), + b'E' => goal_pos = (x, y), + b'#' => map[(x, y)] = b, + _ => {} + }); + }); + + Self { + map, + start_pos, + goal_pos, + } + } + + fn compute_cost(&self, start_pos: (usize, usize)) -> nalgebra::DMatrix { + let mut cost = + nalgebra::DMatrix::from_element(self.map.nrows(), self.map.ncols(), usize::MAX); + cost[start_pos] = 0; + let mut queue = std::collections::VecDeque::new(); + queue.push_back(start_pos); + while let Some(pos) = queue.pop_front() { + let new_cost = unsafe { cost[pos].unchecked_add(1) }; + Direction::iter() + .map(|dir| dir.step(pos)) + .filter(|&new_pos| self.map[new_pos] == b'.') + .for_each(|new_pos| { + if new_cost < cost[new_pos] { + queue.push_back(new_pos); + cost[new_pos] = new_cost; + } + }); + } + cost + } + + fn solve1(self) -> usize { + let cost_from_start = self.compute_cost(self.start_pos); + let cost_from_goal = self.compute_cost(self.goal_pos); + let original_cost = cost_from_start[self.goal_pos]; + self.map + .column_iter() + .enumerate() + .flat_map(|(y, col)| { + col.into_iter() + .enumerate() + .filter(|(_, &b)| b == b'#') + .map(move |(x, _)| (x, y)) + }) + .map(|pos| { + Direction::iter() + .filter(|dir| { + let from_start_pos = dir.step(pos); + if from_start_pos.0 >= self.map.nrows() + || from_start_pos.1 >= self.map.ncols() + { + return false; + } + let start_cost = cost_from_start[from_start_pos]; + if start_cost == usize::MAX { + return false; + } + let from_goal_pos = dir.reverse().step(pos); + if from_goal_pos.0 >= self.map.nrows() + || from_goal_pos.1 >= self.map.ncols() + { + return false; + } + let goal_cost = cost_from_goal[from_goal_pos]; + if goal_cost == usize::MAX { + return false; + } + let new_cost = + unsafe { start_cost.unchecked_add(goal_cost).unchecked_add(2) }; + new_cost < original_cost + && unsafe { original_cost.unchecked_sub(new_cost) } >= L + }) + .count() + }) + .sum() + } + + fn solve2(self) -> usize { + let cost_from_start = self.compute_cost(self.start_pos); + let cost_from_goal = self.compute_cost(self.goal_pos); + let original_cost = cost_from_start[self.goal_pos]; + self.map + .column_iter() + .enumerate() + .flat_map(|(y, col)| { + col.into_iter() + .enumerate() + .filter(|(_, &b)| b == b'.') + .map(move |(x, _)| (x, y)) + }) + .map(|pos| { + let start_cost = cost_from_start[pos]; + let pos = (pos.0 as isize, pos.1 as isize); + (-((pos.0).min(20))..=20) + .map(|dx| (dx, unsafe { pos.0.unchecked_add(dx) })) + .filter(|&(_, new_x)| new_x < self.map.nrows() as isize) + .map(|(dx, new_x)| { + let dx_abs = dx.abs(); + let max_y = unsafe { 20_isize.unchecked_sub(dx_abs) }; + (-((pos.1).min(max_y))..=max_y) + .map(|dy| (dy, unsafe { pos.1.unchecked_add(dy) })) + .filter(|&(_, new_y)| new_y < self.map.ncols() as isize) + .filter(|&(dy, new_y)| { + let new_pos = (new_x as usize, new_y as usize); + let goal_cost = cost_from_goal[new_pos]; + if goal_cost == usize::MAX { + return false; + } + let new_cost = unsafe { + start_cost + .unchecked_add(goal_cost) + .unchecked_add(dx_abs.unchecked_add(dy.abs()) as usize) + }; + new_cost < original_cost + && unsafe { original_cost.unchecked_sub(new_cost) } >= L + }) + .count() + }) + .sum::() + }) + .sum() + } +} + +#[derive(Clone, Copy, strum::EnumIter)] +enum Direction { + N, + E, + S, + W, +} + +impl Direction { + fn reverse(self) -> Self { + match self { + Self::N => Self::S, + Self::E => Self::W, + Self::S => Self::N, + Self::W => Self::E, + } + } + + fn step(self, pos: (usize, usize)) -> (usize, usize) { + match self { + Self::N => (pos.0, pos.1.wrapping_sub(1)), + Self::E => (unsafe { pos.0.unchecked_add(1) }, pos.1), + Self::S => (pos.0, unsafe { pos.1.unchecked_add(1) }), + Self::W => (pos.0.wrapping_sub(1), pos.1), + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + use indoc::indoc; + + const SAMPLE: &str = indoc! {" + ############### + #...#...#.....# + #.#.#.#.#.###.# + #S#...#.#.#...# + #######.#.#.### + #######.#.#...# + #######.#.###.# + ###..E#...#...# + ###.#######.### + #...###...#...# + #.#####.#.###.# + #.#...#.#.#...# + #.#.#.#.#.#.### + #...#...#...### + ############### + "}; + + #[test] + pub fn part1_example() { + assert_eq!(Maze::<15>::parse(SAMPLE).solve1::<64>(), 1); + } + + #[test] + pub fn part2_example() { + assert_eq!(Maze::<15>::parse(SAMPLE).solve2::<76>(), 3); + } +} diff --git a/aoc2024/src/lib.rs b/aoc2024/src/lib.rs index 93c4ab5..0866c96 100644 --- a/aoc2024/src/lib.rs +++ b/aoc2024/src/lib.rs @@ -19,7 +19,7 @@ pub mod day17; pub mod day18; pub mod day19; -// pub mod day20; +pub mod day20; // pub mod day21; // pub mod day22; // pub mod day23;