From 321d3f075b96b2af5dd92e3b01d5f8c489d07f06 Mon Sep 17 00:00:00 2001 From: Matt Harden Date: Sun, 9 Nov 2025 09:22:40 -0800 Subject: [PATCH] 2023 day 12. --- 2023/12/.gitignore | 1 + 2023/12/Cargo.toml | 7 + 2023/12/data/example1 | 6 + 2023/12/data/input | 1000 ++++++++++++++++++++++++++++++++++++++ 2023/12/src/data.rs | 5 + 2023/12/src/main.rs | 10 + 2023/12/src/part1/mod.rs | 17 + 2023/12/src/part2/mod.rs | 17 + 2023/12/src/puzzle.rs | 175 +++++++ Cargo.lock | 96 +++- 10 files changed, 1333 insertions(+), 1 deletion(-) create mode 100644 2023/12/.gitignore create mode 100644 2023/12/Cargo.toml create mode 100644 2023/12/data/example1 create mode 100644 2023/12/data/input create mode 100644 2023/12/src/data.rs create mode 100644 2023/12/src/main.rs create mode 100644 2023/12/src/part1/mod.rs create mode 100644 2023/12/src/part2/mod.rs create mode 100644 2023/12/src/puzzle.rs diff --git a/2023/12/.gitignore b/2023/12/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/2023/12/.gitignore @@ -0,0 +1 @@ +/target diff --git a/2023/12/Cargo.toml b/2023/12/Cargo.toml new file mode 100644 index 0000000..2941610 --- /dev/null +++ b/2023/12/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "y2023d12" +version = "0.1.0" +edition = "2024" + +[dependencies] +memoize = "0.5.1" diff --git a/2023/12/data/example1 b/2023/12/data/example1 new file mode 100644 index 0000000..c5bec3a --- /dev/null +++ b/2023/12/data/example1 @@ -0,0 +1,6 @@ +???.### 1,1,3 +.??..??...?##. 1,1,3 +?#?#?#?#?#?#?#? 1,3,1,6 +????.#...#... 4,1,1 +????.######..#####. 1,6,5 +?###???????? 3,2,1 \ No newline at end of file diff --git a/2023/12/data/input b/2023/12/data/input new file mode 100644 index 0000000..83c7cd8 --- /dev/null +++ b/2023/12/data/input @@ -0,0 +1,1000 @@ +?..#?.??#?...??##??? 1,2,2,1,5 +?????????#? 1,4,1 +????#?????.???# 1,3,2,4 +#???#???##?? 1,9 +??#???#??? 2,3 +..?.???#.????.???# 1,2,4,1,1 +??.???????#?#.??# 1,3,3,2 +???#?????#?. 5,1 +.???.???????????#?. 2,10 +????#?.??#??. 5,1 +.?.?.????#???????.?. 1,1,1,7,1 +..#???????.?.??? 5,1,1,1,1 +.???#?#?.? 4,2,1 +?????##??#.???##??#? 8,8 +#???..#.?#??# 2,1,2,1 +?#??????.#?#?.?..# 5,1,4,1 +????#.?.?#?? 3,1 +#??????### 1,1,4 +.?..??????? 1,3,3 +#?#??#?#?##??#.?? 3,1,1,5,2 +???#?.????.? 4,1,1 +?#??#..???????.?#?? 2,1,1,1,1,4 +#.##???.#??##????## 1,2,1,1,4,4 +??#?#??.?????#??## 1,1,1,1,5,2 +?#??#????#?#?#? 2,1,1,6 +??#???????#?????? 7,1,1,1 +???..??.?? 1,1 +.??#??????#?????###? 2,8,3 +??.???..#?.??#???? 2,2,1,3 +????.????? 2,1,1 +.#???##??##?#??.? 2,10 +.?#????.#?? 2,2 +?.?#???.#????#????#? 1,5,1,1,3,1 +?###????..#??#? 6,1,5 +?????.##?. 2,2 +??#???????????#?? 1,1,2,4,4 +?#?#??#?#.#?? 1,1,4,1 +#??#?.?????# 2,1,3,1 +?#?##..???#??#..??? 4,7,2 +.????#?????#?.??. 1,8,2 +??###??...#?###??.. 3,6 +???#?#.?.??#?.? 3,4 +?#?????#????#?? 1,9 +???#.??..????.#?##? 1,1,1,2,1,5 +???.?.????.. 3,1,2 +.?????????#.? 4,5,1 +?????.???.?#?? 1,3,2 +?.??..????# 2,1,1 +.?.???.????. 1,1,1,1 +..???#?#??#.?.?#??? 7,3 +#?#.#??#####??? 1,1,1,7 +???????#??#?#???## 1,3,1,6,2 +????????#????#?#?.?? 1,1,1,1,4,2 +??????????????##? 1,2,1,2,4 +??????###.??? 1,7,1 +???#.??#????.? 1,4 +????#?????.?#?#???. 10,5 +.????????##????.?? 1,2,3,2 +?.???.?.#. 1,2,1 +?#?#.????.#. 1,1,1,1 +.#.??????#?.#?? 1,1,2,2,2 +??##??????? 2,1,1 +?????###????#?? 9,2 +????#.??.??###.?. 2,5 +.?#????#.? 2,1 +???#?.?.???? 3,1,1 +.?#??#.??#???#?.?#? 4,8,2 +.?#????????#. 7,3 +.??##.??##..?.#? 4,3,1,1 +.???.#.??????? 3,1,3,1 +.????????#??.#??? 4,2,2,3 +?###?.???#. 4,1,1 +???#?.#???#??. 1,6 +?.??#?.??# 1,2,2 +?##???.#?? 2,1,3 +??#?????#?#?#?? 1,1,1,6,1 +?.?????#???.#?? 1,8,2 +???.#?#??#?.? 2,4,1,1 +?#????#???.??#??? 8,3,1 +?.?..???????? 1,1,1,2 +??#?#????#????#?? 1,1,6,5 +?#?#?#???#.???#? 6,2,1,1 +??#.?#.???#?.##?#? 1,1,1,3,5 +#??.#?????? 2,2,1 +????????#??. 2,1,2 +?#?????#?#??.? 3,4 +????#????#????#???? 1,14 +.#?#??##??.#.??? 3,5,1,2 +#??#???.???#?#? 7,2,1,1 +##..#??????? 2,2,4 +?#????.?????#??? 4,5 +#???.????#???? 1,1,8 +???.?#?.#??..?? 1,2,1,1,1 +??#??#???.?????##?.? 8,4 +??????..?? 2,1 +???.??.#???#..?# 2,3,1,2 +?#??..????.. 3,4 +..#???.???? 3,2 +???..????.??. 1,4 +?????????.#???#? 7,1,3 +?#?#???.??.. 3,1,2 +.?????.?????#?#??# 1,3,11 +#??###.?#???#? 6,1,1,2 +??????.?#? 2,2,1 +?????#?#??..? 3,1,1,1 +????????????#?.?. 2,7 +???.?.??????#??#? 1,10 +??????????. 2,3 +???##??#??. 1,8 +???.?#???? 1,4 +.???###..#?#?#? 6,3,2 +??..??????. 2,5 +#?????#??#???.#?.#? 2,3,3,2,1 +.???#?????# 1,4,1 +??##.??????.????? 4,1,1,1,1 +.????????#?? 5,2 +?.?????????#??????#? 1,6,1,1,3,1 +?.???#?.????? 1,4,1,1 +????????#?? 2,2 +.??#???#???. 2,3 +???...??#?#??????#? 1,6,1,2 +??.????????? 2,3,1 +?.#.??#?????#? 1,1,7 +##??#??.???#??.?? 2,4,5,2 +.#??????#?..?.??#?## 1,1,2,6 +.??????????? 2,6 +#????.??????? 3,3 +?#??#?#.?#??.?#?? 2,3,3,1 +???#?#?????#?.???.. 12,3 +#.???##??..???.# 1,6,1,1 +?#?###???#??????. 7,4,1 +?.????????#??.#?? 1,3,4,2 +???????????.. 3,2,1 +?.?.?##?.? 1,1,2 +.#.#?.???..??###. 1,2,3,4 +?#?.??????.#?#???.# 1,2,1,6,1 +.???.?????#?? 1,1,2,2 +?????.#????#.# 3,1,3,1 +?#???#?#?#??.?#??# 10,5 +???..???.?????#? 1,3,1,1,1 +???.#??#?#?????#??? 2,4,9 +?????..???#?.#? 1,2,1,1,1 +..???.#??#???? 2,1,3 +???.?#?????. 1,1,1,1 +????.???#???????? 1,1,1,4,5 +?..?..??????# 1,1,7 +??.?.?.###?. 1,3 +????.?.?#???#????.#? 1,1,2,4,1,1 +?????#??#????#?.#.# 3,4,1,2,1,1 +????#??.???.???? 3,3,1,1 +?.???##??.?#? 4,2 +???.?..???.?#??#? 1,1,1,1,5 +#???????.??#?##.??? 6,6,2 +#????#??.??. 2,3 +??????#??.?.?? 1,3,1,1 +??????.???##??.? 1,4 +??#????#??#??? 4,4,1,1 +.#??.?#??????.?? 3,8 +?#?..???##???#??. 2,6,2 +??##?.???#??# 4,1,2,2 +??????.?.?. 1,2,1 +?.?????????.????#? 1,2,4,3 +?????#????#?.?? 1,7,2 +??#??????? 2,3 +#??#?##??##??.?#???? 12,2,2 +??#??#??#??##??????. 2,4,2,5 +#??#?.?#??? 5,2 +####??????????? 5,4 +##.??#.?#?????? 2,2,3,2 +##?????.??#?.?? 2,1,1,3,1 +?.??##?##? 1,7 +?.???.?#?##?#?#??? 3,7,4 +??????..?#?????????. 5,4,1,2 +?.??##?#??#??? 2,4 +?.??????##?.#??#.? 1,2,1,3,2,1 +#?.???#?#????# 2,4,1,3 +#####???#???.??.??. 9,1,2,1 +.#??#?#??????? 7,1 +?????????#??# 2,2,1,2 +?#?.?#?#????#????# 2,4,1,1,2,1 +???????#??###? 2,10 +??.?.?????#????.#.#? 2,1,4,5,1,1 +????..#?.?#? 1,1,1,2 +???.?????? 1,3 +#???#?????#? 1,9 +????????##???? 1,1,5,2 +??.?#.?#?#??? 1,2,7 +.?.?#?????????.? 8,1 +????????.?????? 3,1 +?????.??##? 2,1,5 +##?????#????? 3,6,1 +?#?#?..?.?##? 2,1,1,2 +??????????#??..#? 12,1 +?????#??#?.??#?.? 6,2,3 +?.?.#??.?. 1,1 +.?#??.???.#. 3,1,1 +?.?#.????. 2,2 +??#?##???. 1,6 +..????#.?.?#? 5,1 +?###?????????.#?. 4,1,3,2 +???.?????#? 3,1,3 +???###???#.??#? 9,2 +?.??#???.??? 1,1,1,3 +?#??????##??.#?#???# 5,5,3,3 +#???#??.?#??###? 1,2,7 +.?#?#???.?.??.?.##?? 7,1,1,1,3 +??..#?.????. 2,1,1,2 +??..?.??#?????.?? 4,1 +????#?#?.#??? 7,1,2 +.??????????#?? 1,9 +??#..?????#.#.????? 3,2,2,1,1,1 +??.##..??? 1,2,1 +#..???????.##????#? 1,1,1,3,3,3 +##.??#?????#??? 2,1,1,1,1 +.###???##?????? 4,5,1 +#.?##?????????# 1,4,1,1,3 +?###.?.???#?????. 3,4 +?#?????#???#. 2,1,2,1 +.?.???????#? 1,1,2,4 +.???????#?????. 2,4 +.#..?.#??#??.? 1,2,3 +?.?#??.?.? 1,1 +?.???????#?? 4,3 +.???????#? 2,5 +????.??#?# 1,1,1 +??.?????????## 2,4,1,2 +#???#.???.????# 1,1,1,1,4 +?#??????.?.#.??.???? 7,1,1,1,1,2 +.?##?#???#?###.?? 13,1 +?.##??##?.?? 6,1 +.?????.??.????##?? 1,2,5 +???#???##.? 1,6 +???#??##?#?#?.? 2,1,7,1 +?..????##?#?## 1,2,8 +.?????###?..?..???? 6,2 +#...????..???. 1,4,3 +#?#?????..?.? 6,1 +????#??##??#? 2,2,6 +??#????#???##??????. 16,1 +???###?.??##?? 6,1,3 +?????????..#? 2,2,1,2 +???????.???? 2,1,1,2 +???????..?#?? 1,2,2 +??????##?#?????#? 3,5,1,1 +.?.??#??????#?.?? 3,2 +??????.?#???? 1,1,1,6 +????.##?#?## 2,7 +.#?????##? 1,2 +.??.#??.????### 2,1,4 +????..#??.?????#?? 1,1,1,1,3,4 +.????#?..#? 5,1 +#????.???? 2,1,4 +?##.??.?##?#??.? 2,2,7,1 +#??#???#??#??## 1,6,5 +???.?.?#?#???? 1,5,1 +#?.?#?..?.#????# 2,1,1,4,1 +????#..#?#??#????? 4,9 +???#?#????#????.? 1,4,1,5,1 +?.?????.#???? 3,4 +?????#.????#??.?? 2,2,1,5,1 +?????#??.#????.??. 7,1,1,1 +??.?.????#?.??#??.? 3,2 +??#?###??.#?? 6,2 +?##????..#.??. 3,1,1,2 +.?????##?#??#???#.? 8,5 +#??#???###?.. 1,3,5 +??????????#?????#. 11,1,2 +###?#?#??..??? 7,1,1,1 +#??#??#?#???..### 1,1,4,2,3 +#?.????????.? 2,2 +???.?????.???#.? 1,1,4,4,1 +?#.?????##???? 2,2,2,1 +??.#????#?????.?.. 1,1,8,1 +????.?.???????. 1,1,1,2,1 +.???????#????.???. 9,2 +???#???#???????? 1,4,1,1,1 +#??##.??## 2,2,3 +?#??????.#??#??? 2,3,1,2,1 +.????.??#??????..? 1,9 +?.#?#?#??...???? 5,3 +..?#????#?.???? 8,2 +.#??#??.##??#??????? 4,1,5,2,2 +#?.??#???#??? 1,10 +?.#?????????? 5,4 +?????????.? 2,4 +#???.?#?#????.?### 1,2,6,4 +#.??#?###??.?#??# 1,8,5 +.?.???#??#??#??#???? 1,13 +?#?#??#??#?????.??? 3,1,7,1 +.?#.?#.?#??.? 1,2,4 +????#.?#.???? 1,1,2,3 +?????#.?#?????.?. 1,1,6,1 +????.??????###... 2,2,5 +??.?#?#.????. 1,3,1,1 +???#.#??.? 1,1,2 +???.?????????.??? 1,5,1,2 +???????????.? 1,3 +?????#????? 1,2,1 +?.###????..??..? 1,3,1,1 +???#.??#??? 1,1,3 +?.?#??##?.?## 1,6,3 +?????#?#?????.?? 2,8,2 +??#.?#?.#..# 1,1,1,1 +???#?#??#.????##?#? 8,8 +.##?.?????.?. 2,2 +??????????##?#??. 2,8 +?##?.????????? 2,4,1 +?#?#.??.??. 3,1,1 +#..???.#?? 1,1,1 +#.?#??#??#???#?#.?.. 1,13,1 +#?????.?#??? 5,3 +?#??#??#???#.#?? 1,6,1,1,1 +#???..??????..?? 4,1,1,1,1 +.#?#.?????????#?.??? 1,1,5,5,1 +..##?#.#?????. 2,1,2,2 +#??.?.#?#?##?????. 2,1,7,1 +#???#??????? 1,5 +?.??#??#?.?..??.? 1,5,1,1,1 +???#??.?.??????#???. 1,4,1,1,6,1 +???#????.?.?.?#?. 1,5,1,1,3 +..???#??.#?# 1,1,1,1 +.?###.?.?.#?#.#?? 4,1,3,3 +???.?.??.????#.? 2,1,1,2,1 +???.?.???? 2,2 +????#?.?????#??.?##? 1,2,5,4 +..?????.#. 1,1,1 +?.???.????? 3,1,1 +???????..????#.???.? 2,1,1,3,1,1 +???.?..???#??#??? 1,1,8 +?????????.???.????.? 1,2,2,2,1,1 +.????.##?.. 1,2 +#?????????.??#???# 7,1,1,1,1 +?.?##..###???? 1,2,3,1 +.????#???#?#.??. 1,2,1,3,1 +??#.#????????? 2,1,3,3 +???.?..?#...?? 1,2 +??..??#??###??#?#??? 2,7,7 +?????#???..#. 2,5,1 +#???.???.?#????#??? 2,1,4,4 +??.##?????? 1,2,1 +?????.????..???#?. 2,2 +?????.???#?#???? 1,2,6,2 +?????..??.?. 1,1,1 +?.????.??? 1,1,1 +?????????????? 1,1,1,6 +???#?????.??#? 5,1,1 +??#??.#?#?##?#???#?? 2,1,6,1,1,2 +???????.??.?.?#?.?? 5,2,3 +?????#?????.??? 9,1,1 +.???##??.#?? 6,3 +???.?.??## 2,3 +?.???#?##??.???? 1,1,1,3,1 +????.?####???#??## 2,13 +??????????? 8,1 +#?.?##???#??#??? 1,10 +???#???##?????????.? 11,1,1 +.#?.??????# 1,3,2 +#?#?.#??.?.#.??.#?# 3,1,1,1,1,3 +?.???#?.??##?????? 1,2,2,3,1 +??#??#???.#.??? 3,1,1,1,1 +#?######???????# 11,1 +#???#???#?????.#.#? 12,1,2 +??#?#.????#?##?#??? 1,1,1,10 +?.??#?.??.? 1,3,2 +.?#??#?#?#??.?????? 9,5 +?#??#????????#?.??#? 2,12,1 +???#???????#?##? 3,3,4 +#?#????.????#??# 4,1,1,5 +???##?##????#.???? 8,2,2 +?????????.#?.###..? 1,7,1,3,1 +.#?????.?#?? 6,1 +#???#?#??#? 8,1 +????#?.#.. 1,1,1 +?#??#?#?.?????#? 1,1,1,3,2 +.?.?????##?#??##??? 1,1,13 +?.??????#????? 7,3 +?..?.?????#??? 1,2,2 +?.?????##??.???#??. 1,1,5,1,3 +????#?#??????#????.? 3,6 +??##??????? 4,2 +??.?#??#?????????.?? 5,4 +??#?#.?##?? 3,3 +?#??.??..?? 2,2,1 +????...?.??????#.#? 1,1,1,5,2 +????????###.? 2,6,1 +?.#?#??????#? 1,10 +?.?##??????.???? 1,6,1,1 +#???????##?? 6,2 +??##???.?.?#??#??##. 5,1,8 +?##..??##??#?. 2,5,1 +#.??.#????#?#?? 1,1,1,1,6 +.????..??#?. 1,1,4 +?..????????.#.. 2,1 +???#???????#??? 5,1,4 +???.???.???? 2,3,1,2 +??..#???##.??. 1,1,3,1 +?????.???????????# 1,1,1,1,7,1 +#???#???#????. 1,6,1 +##??#??????.?? 5,3,1 +.???#?..???.??? 5,2 +.?##?##??#?.#??#? 3,6,1,3 +.??#.??#???.??? 2,5,1 +.#.???#??##?#.????? 1,7,1,1,1 +##?.#???##?#??? 3,10 +#??.??###.?#.## 2,4,1,2 +??#???????#?.???# 5,6,1,1 +????##?#???.??# 2,4,2,2 +#?..???##?.? 2,5 +..????#??.? 2,2 +.???#?????. 2,1,2 +?#?..??..#???. 2,1,1,2 +????.???##????????. 1,1,1,10 +#?#?#.#???.?#? 1,3,3,2 +..##??????.????? 4,1,2,1 +??#???????? 4,2,1 +???.?????#. 3,1,1 +.#?????????? 3,5 +.???.???.?##???##?.# 1,2,8,1 +???###??#..???#????? 8,6 +?????.?????# 1,1,1,1 +???#.??##?#?.?.???? 1,1,4,2,1,1 +?.##?###???.???????# 1,9,1,2,2 +..?.??.??#????????? 1,6 +???##???#??????# 5,3,1,1 +.#??.???.?####?? 3,2,6 +??##??#.?#?.#??#???? 6,1,1,2,1 +.???#??.????##?? 5,7 +.?????#?.??# 1,1,2,1 +??..#???.???? 1,1 +?.??###???#.?.#???? 1,4,1,1,1,1 +??.?#.???? 2,1,1 +????#???.?#..###? 5,2,4 +???#????#? 3,2 +.?##??##???#?#?????? 9,1,1,3,1 +??#??..??#?#.?? 1,1,1,5,2 +#????#.???????? 1,4,1,1 +??##.??###??# 2,1,3,1 +???#?#??#? 1,1,1 +????????.??.?.#.#? 5,1,1,1,1,1 +?#??.??#.?.??#? 3,1,1,1,2 +?##?.#??#? 2,4 +???#????.?.?? 6,1,1 +?.???.???????? 1,1,6 +??.??.?.???..? 2,2,1,3 +?????#..?????. 1,1,2 +??#???#??#?#.?.? 1,10 +.??#????##?????. 5,3,1 +?.#?.??#?#? 2,1,4 +?.?#?####?.?.???.?.. 8,1,2,1 +??????#??.? 5,1 +?#??#????????? 5,1,1 +?..?????????###??? 1,1,1,3,7 +????????.?.?..?????? 1,1,4,1,1,5 +..?????.#???#..# 2,1,3,1 +???##????#.?.?#? 8,2 +#.???.###? 1,2,4 +?#.??..????## 2,1,1,4 +..??..#??. 2,2 +????.?.#??? 2,1,1 +??#??##??????.??#??? 10,1,1,1,1 +.##?..#?.?..#?#?..?. 2,2,4 +??..??????#?#. 2,2,3 +??????#???.##?##?? 1,5,1,6 +???#?.#???# 3,1,2 +#?????#?##?#.????. 3,7,4 +??#??#??#?#.?. 2,2,4,1 +#???????.. 4,1 +???.?.?###? 2,1,3 +?#?.?????##?#.#??. 3,1,2,2,1,2 +?#??????#.?? 1,1,2,2 +.#.?##????## 1,3,3 +.#?????????? 2,6 +?.????.??# 2,2 +..?##??.??.?##? 2,2 +???##?#????# 8,1 +.????#????.?. 7,1 +?.???..#?#??? 2,4 +?#???.#?????..#??## 4,3,1,2,2 +?????.?#??.???.? 1,2,3,1,1 +?.?#?????#???.. 1,2,1,1,1 +?#???#?#?.???#??#??? 9,1,7 +?????????. 4,1,1 +..#??.??.?#???#.?? 2,6 +.??????.?# 1,1,1 +???#..?#???#?##??# 1,1,10,1 +?.??..?#?? 1,2,1 +???#?.??#?? 3,5 +??#????????#? 3,1,2 +????????##?????#??. 6,2,1,3 +????#???.?#???.? 4,1,1,2,1 +.?#?????....?#??.??? 1,2,4,2 +???##???##?#???.? 12,1 +?.??????.? 3,1 +?.?...###????#?? 1,10 +??#??????#?.? 4,2 +.##?#.#????.? 2,1,1,1 +#???.????.?## 1,1,1,2 +?.?#?.??##??????? 2,2,3 +??.??????##?.??? 2,1,7,1 +?#??.???????????? 4,1,6,1 +?#??..???#?? 3,1,2 +.???.???##.? 2,4 +#???#??#.? 2,2,1 +????#????? 2,3,1 +?????#?..?????? 2,3,2 +????????#.??.??..?? 7,1 +?????#??#.?#???? 1,3,1,5 +????????#?? 2,1,1 +????.#?????.???.???? 1,1,6,1,1,1 +#????#?##?#?#??? 1,12 +???#??#??.???????? 5,2,1 +#???.#??.?.. 1,1,2,1 +?.?????#.?.??. 3,1 +#???.???????.??? 2,1,2,2,3 +.#??????#### 1,7 +?.?.?????#???? 1,2,1,2 +?????.##???##? 3,7 +??##????#??????.?? 5,1,1,1,1,2 +.#?#?????. 1,6 +.?#??.???? 2,3 +#.??#???#?????.#?#.? 1,11,3,1 +.?.??#?#?????###?. 8,3 +??????.???. 4,1,2 +.#??.?????#? 1,1,2,4 +???#.#?#?#? 4,1,3 +??###..#?? 3,2 +????????#??.#? 3,2,1 +.????##??..#??. 1,3,3 +?????.#???.???#???? 2,2,1,2,3 +??.???.????????#?.# 1,1,4,4,1 +?.??.??##????#???# 1,1,13 +.??#?#??..?##???? 3,6 +????#??.????##.. 3,1,1,1,2 +??.?.????#?.? 1,1,2,1 +????????#?#?#???? 4,11 +.??#?##?.?.?? 7,1,2 +???????????#????#? 2,1,1,2,7 +??#??????? 1,3,1 +###?.#?????#? 3,1,1,3 +????#?#?##??. 1,8 +??????##?##??#????. 1,2,9,1 +?.?.??????#? 1,1,3,3 +??????.#?#.??#? 1,1,1,1,2 +.??#??????????#.?# 6,1,1,1,1 +?#??#?#????.???. 7,1,1,1 +?.??#????#??.??.??? 1,1,1,6,1,2 +???.#.#.??.#.?#???? 1,1,1,1,1,5 +.#?.??????#????.??# 1,2,6,1 +.??#?????? 1,1,2 +.#??#????????.?????? 10,1,2,2 +.#????.?##? 1,1,3 +???#?##???.?#????#. 6,1,6 +??.???#??.??#?#?... 3,3 +#?#?.?#???.?. 1,2,3 +????.?#?#????#?#. 1,10 +??????.#?.?? 2,2,1 +?.##?..??.??##???? 1,2,2,1,2,2 +.??#??.?????.??? 3,2,2 +????#?#.?#??# 1,4,4 +???#????.?#.?.? 2,1,2,1,1 +???????????.#???. 4,1,1,1,4 +#????#?#?..??? 8,3 +?.???????. 1,4 +?#??#???????????? 6,1,2,1 +?#?????#.??#?##??. 1,3,7 +..???##??#?????????. 10,2 +????#??#.??.????? 1,6,1,1,3 +?.???.???????.??.. 1,3,3,1,1 +.????????? 2,2 +??.#?.?#????.?..?? 2,6 +?.???#????### 1,5,4 +?.??#????? 3,1 +??#???#.??#?? 2,1,1,1 +????##????????##? 1,2,4 +??.???#?.??? 1,1,2,1 +???.???#??.#????. 1,5,1,3 +.?#?????????? 2,2,1,1 +##???.???#.##?### 2,1,3,6 +?.?.#?..??#?????#?.# 1,1,1,7,1,1 +.#????.???#????. 4,2 +?#????#???#??? 1,2,1,4 +??##???#???#???? 5,5,1 +????#??????#??? 7,3 +.#???#?.???????.#. 3,1,1,1,1,1 +????#?????..?.??? 9,1,1,1 +.#?#?#??###?????.? 1,1,1,8,1 +?#???#.????. 1,1,1,1 +??????.????? 3,1,1 +.????????..? 2,1,1,1 +?????#?#??#. 2,6 +??????#???###???? 7,4,1 +..??##?????????#??. 6,2,1,1,1 +??#??##??.??#.???? 8,1,1,1,2 +?????###?????#?#? 9,5 +??..??#?.. 1,1,1 +.??.??????????##??? 1,4,6 +#?.?????#??#.??.??? 2,3,5,1 +?#??????#?#?? 2,3,1,2 +.??#??#????#?#.? 1,8 +..??.??.????## 1,2,1,3 +???###???# 4,2 +???.????#?#??#?#??? 3,2,10 +?#?.#.?.?####? 2,1,1,6 +???..???.#???????? 2,1,3,2,1 +??.?.?#??.#??# 1,2,2,1 +???##??### 2,2,3 +?#????.?#??????.?#?? 5,1,2,1,2 +..????#???# 1,2,1 +????#..??##??? 1,1,1,3 +##?#??##????#?.?? 10,1,1 +#???###?????? 7,1,1 +??????.??#?????##?? 4,1,5,1 +?.??????#???? 1,3,4,1 +???????#?##???..?. 1,1,8,1 +??#??##?#?#????. 9,1 +????###????????.?.?# 3,5,4,1,2 +??.?#???.?.??????. 1,1,2,1,4 +##???###.?????##..# 3,3,1,2,2,1 +??#??#?#.?.??#????? 6,4 +#?#???????#??#?#?#?? 3,1,2,4,1,2 +???#?????.?. 1,1,1,1 +?#.?#.????? 2,1,2 +???#??#?#??#?##??.? 4,1,9,1 +?...##????.??#?.? 1,3,1,3 +??.?.??#?#??#. 1,6 +???.#.??#???.? 1,1,2,1 +.??..???#?#???? 2,6,1 +?##??#?.?? 5,1 +#??#??#????.. 1,7 +??.???#?..#?#?????. 2,8 +???????#??#.???#? 1,1,3,1,2 +???????###?#. 3,3,1 +???.#?##????#?#?#? 1,5,8 +#.?????#??.???.#?? 1,1,3,3,3 +#??#???????????#??? 1,2,4,1,2,1 +???.?#.?###?.#.?# 1,1,4,1,2 +???#?????.?#???#? 4,3,1,3 +?#??.?..?#???? 4,3 +??#?#.#.?? 4,1,2 +???????.??#.?#? 1,2,1,1,2 +#?#?##???.????#?? 1,7,1,1 +??#??#?????#??. 7,2 +.?.??#?.???.?# 1,3,1,1 +.?.?????#????##?.. 1,1,1,3,3 +#.????.???????.?? 1,3,4,2,1 +???????#???????.??#. 1,2,1,4,1,1 +.?#?..??##. 2,4 +.?#????????.?????? 9,6 +?#????????.???# 3,3,1,1,2 +??.???#???? 1,3 +??#????????????? 8,1,2 +?????#???????#???#? 3,2,1,5,3 +??.???.???###? 1,2,6 +?#..???#..? 1,3,1 +??#?#??????#.#.??? 10,1,2 +???.??#???##??#???. 2,10 +?..??..?.??#?? 1,4 +????.??.???. 3,1,2 +???##??..??. 5,1 +??????#.??#????? 1,1,5,1 +???#???#????#?#???? 1,1,4,1,7 +.?..???#?.? 1,1 +?.???#??#?????##???? 1,2,5,7 +?#???.???#??.?????# 2,2,3,5 +????#????.#?.#? 1,5,1,2 +??.?#?#.?? 2,3,1 +?.?#?????#??. 5,2 +.????..????????? 1,1,1,1,4 +?#.????.#??. 1,2,2 +????..#??#?#?##? 1,9 +.?###????? 4,1 +?????#?????#?##.? 4,4 +#?.#??.????????##? 2,2,2,8 +????.????????#?... 2,1,1,3,2 +??.???.?.??. 1,1,1,1 +?##?.????##??##??? 3,2,8 +?????????#??#?????? 3,9,3 +#?.#??#?.?#????? 1,1,3,2,1 +???.#??#????#???.?? 1,1,6,4,2 +???#...###??????. 4,5,3 +???????#...?? 2,4,1 +#??#?#??#?#?. 7,3 +?.?###????###??? 3,7 +??#..?????????? 2,2,2,1 +.??????????#?? 1,1,1,3 +#??#?????#???.??#?? 10,2,1 +?..?.#??#.??? 1,2,1,1 +#..?????#..# 1,1,3,1 +#?#.??#???#?? 3,3,1 +#??#????#?.???.??? 1,1,3,1,1,1 +?.?.??????#?#.#?#? 1,1,1,5,1,2 +#?.?#????#? 1,2,5 +??????..#??#?????.? 1,1,1,5,2,1 +????#??#????? 4,5,1 +?..?????#??#????.?? 1,10,1,1 +??#?#???.??????? 6,1,2 +#?.??#??#?. 2,1,2 +????#.??.??? 1,2,1,1 +?#?#.???#?? 1,1,2 +?.#??.?.??# 3,3 +.?#.###?#????#?.##.? 1,11,2 +?.????#????.???#?? 1,1,1,1,1,3 +#.?#??.?#??#??? 1,2,5,1 +.?#????????.??# 5,1,1,1 +???.#?.????? 2,2,2,1 +#????#??#?#??.???? 13,1,1 +?#?#?????? 4,1,1 +???##??????.??.???. 5,2 +?.????.?..#. 1,1 +???????.???#?#?##.. 1,2,8 +.#.?????#.?.#?????.# 1,4,1,1,1,1 +???##?.?????? 4,4,1 +?????#???####? 2,9 +.????#.????.# 5,2,1 +??#?..#?#???# 2,1,2,1 +??##.#??#?#????#?. 4,2,1,1,2 +#.#???.?...?#????#?? 1,1,1,1,7 +?.????.??????#?.#? 1,2,1,5,1 +??#???.???#??. 1,1,3,1 +?##?#?#???#.#???? 10,1,1,1 +.?#???????????. 1,2,5 +???????..??#?#??? 5,1,5,1 +.???.#?#??????##??#. 1,14 +?#??.?.???.#? 4,1,3,1 +..??##???????#.?? 5,5,1 +.#?????#??? 1,5,1 +#????#.?.?????. 3,1,5 +?#?##?#?????# 5,2,3 +???????#?.?..?????.# 7,1,3,1 +?#??#??.?? 1,4 +???#..???. 4,2 +??.#?#.?#?? 1,3,1 +???#??##??.#???#? 2,3,5 +???#?????#??.???.?.? 11,2 +.??..???#??? 2,4,1 +?.?#.?##?.?#?.? 1,2,2,3 +??#??????#?? 3,3 +.?#??#???? 2,5 +????.??????#???#?.? 3,2,7,1 +???#?..#?.?#???#?#? 2,2,2,6 +.??#??#?#.??.#? 2,1,1,1,1 +.?###..????#? 3,2,1 +?.?????????? 1,4,1,2 +?#??#???..???#? 6,4 +???#??#??.#? 3,1,1 +???.?.####?.# 1,1,5,1 +.?????????.?.? 5,1 +.???????#???#?. 1,9 +.??#??#???.??#. 4,1 +#??#..##?.???. 4,2,3 +.????.??.?#?.?.?#?. 4,1,3,1,1 +??.?#??????#??????.? 1,3,3,3,1,1 +#?????.?#?.#??.? 1,4,1,2,1 +?.?????????.? 3,3 +?????#???#?? 5,1 +..??#???#???.#.#.? 6,1,1,1,1 +??.#.##??? 1,1,4 +?#??????##.##?. 1,2,3,3 +.???#.????? 3,3,1 +.???#?#????#??.?.?? 7,1,1,1,1,1 +..??#??#??#???.?##?. 6,1,3 +??#???#.???????? 7,1,3 +???.#??.???.???# 1,1,3,1,2 +??#.##?#?? 2,5 +.???#?#.#???????? 4,1,2,2,1 +?#?#.?##????#? 2,1,4,2 +?.?.?.#????#??.?? 1,1 +.???.#????.?.?????. 2,5,5 +?#?.??.????? 2,1,3 +????###?#?.????.#??. 7,3,1,1 +?..?#?.##? 2,2 +?.?????.##???#.??## 1,1,1,6,1,2 +??#?.?.#???.? 1,2 +???.#?.#?????#. 1,1,2,2,4 +..??????????#? 1,8 +??##?#?.???? 3,1,1 +?#?.???#.?? 2,1,2 +##???##?#???.?? 7,2,1,1 +???##??##??#.?.???? 11,1,1,1 +??.??????##??.#..?? 1,6,1,1 +????#?#?.??#?? 6,1,1 +.???.#.#??.?#? 3,1,2,2 +??????.??#.??.?#?.? 5,2,2,3 +?.??#?????#.?#??##. 1,3,1,1,1,6 +?.???#???#?#?.????? 2,1,6,1,1 +#.##?????????#? 1,3,1,2,2 +?.#??????. 4,1 +????.??.????#?????#? 3,1,1,1,6 +??##?#?##???# 9,1 +?#?####.####??? 6,4 +??.#??.????# 1,3,2 +.????#??.?#?? 6,3 +??????###?.???.??? 2,4,1,3 +.??#??.????#?? 4,1,1 +??.#?????#???????# 1,5,3,1,1 +???????##?#??#??#??? 1,10,1,2 +????#??#?####?.#. 3,2,5,1 +#???##???#.?.???# 7,2,4 +??#??.????? 3,3 +?#??..?????. 1,3,1 +???#???#.??.?##?. 6,3 +??#??.??.??.#. 4,1,1,1 +??????.??#?#??..???? 1,3,5,1,2 +??#?##??#.??.?# 6,1,1 +###????.#????.##.? 3,3,1,1,1,2 +#?#?#?#?#??#???????# 1,1,3,6,2,1 +??.#???#???????? 5,6 +?.???????.? 1,1,2 +?????????????? 1,5,2 +????????#? 2,3,2 +???.???.?###??#?? 2,1,4,1,1 +????????#?#??? 6,3 +???????#??.? 1,1,4 +.#??.#?#??? 3,4,1 +???###????.????#. 7,2 +.???#?????? 1,3,3 +???????????????#.?#? 2,2,6,1 +.###??..?? 3,1,2 +?..????#.????.??#. 1,5,2,1,1 +??#????.??#???.. 2,4 +###???.??#?#?..???? 6,6,1 +?#????????. 4,4 +?#??????????? 5,1,1,1 +??..??.#????. 1,5 +?.?##.?????? 2,5 +..??????#?? 3,1,1 +???????#???????#?. 2,9 +?.?.#?.?#.??#?#??# 1,1,2,1,8 +????##???#?????..??? 11,1,2 +?.??.??.#?? 1,1 +.??#???#?#?#?.??# 4,5,1 +#??#??.??.##?.#??# 5,1,3,4 +.#.##??.???????#?##? 1,3,4,7 +..??.?#?#???#?#?.? 1,1,3,4 +.??.##???..???????#? 5,1,7 +??????...# 2,1,1 +?????#??.#.? 1,2,1,1 +??#???#?#??#??? 9,2,1 +????????#?????.. 2,8 +?????.?#???#..?#???? 2,1,4,1,3,1 +.?????.??#?? 3,2 +#?????#?#.#???#?? 3,3,1,2,2 +??#????###????.???? 11,1,1 +.??.????.??? 1,2 +#.#???#????#???.#.?? 1,7,1,2,1,2 +.#.###????? 1,6 +??????##?.?..?#?. 7,1,2 +.###?#??????#???? 5,7 +??#??#.##??.?.??# 4,4,1,1 +???????#?#?? 1,5 +??????????##.??? 1,4,4,3 +#?##????..??# 7,1,1 +#?????#??#????.?? 1,1,5,1,1 +.?#?#.#?.??????#?? 3,1,1,1,5 +?.?#.?##???#..#. 1,5,1,1 +#??#?????.#?.?????. 6,1,1,1,2 +.?#???.??##??.?? 4,6,1 +??????#???#???????#? 13,1 +??#??##???????.???? 9,2,1,1 +#?#???#..#??????? 7,1,1,1 +????????.?#??#??. 2,6 +?.???.##????#?####?? 2,2,8 +?..##?????#?????#? 2,2,7 +..???.???.# 2,1 +???#.????.? 4,3,1 +##?????#????.?????? 2,5,3,3,1 +????#??##?. 3,2 +??#??.?#?..????.?? 5,3,1 +.?..?????..##???.??? 1,4,2,1,1,1 +.??????##???????? 7,1 +??.?????.? 1,4 +....??.???# 1,2 +.??.????????????? 1,9,1 +??#?#????#.??#????#? 9,9 +??.#??#??.?? 4,1 +??.??#?????? 1,6 +.#?###..#???????.? 5,1,6,1 +.????????.??.?? 8,2 +#??#.?#??#??.#? 4,4,2 +?.????.????????#?. 3,9 +??###????????? 8,1,1 +?#.##??.#? 1,2,1 +????????.#??#. 8,1,1 +?..#?.?..##???##??? 2,10 +.?##?##???.??????#?? 6,2,1,4 +?.????.??.##???.?? 2,2 +.????.?????#???? 4,6 +???.??????? 2,2,4 +????.?#?????.???#? 2,5,5 +?.#??????????..?#.?? 1,5,1,2 +????#.?###?????##??? 5,12 +??.??#???????#????. 1,1,1,4,2,1 +.?????#???? 1,4 +???##??????## 6,2 +#?#?????.???.?#?.? 4,2,3,2,1 +??#?##?##?#?????#?? 9,1,5 +?????#??## 5,2 +?#.????#??#????#?? 1,1,4,1,4 +??.??????#?#.?? 2,8,1 +?????.????. 1,1,2 +..?.??????????????? 1,1,1,4,1,1 +?.?#??#???????#???.? 7,6,1 +???##?.#???#?#??? 5,2,6 +?##?##??????? 6,4 +##?.#??#?. 3,1,1 +?.?#?.??#.?? 1,1,1,1 +?.???#?##?????#. 1,8,1,1 +?.?.??????#???## 1,1,1,1,4 +.???##???.# 4,1 +??#?##???.. 7,1 +#.???#?.#.???? 1,4,1,2 +?.?#????#?? 1,2,4 +#?.#????#??????????# 2,1,1,4,1,5 +??????.?#??.?##? 3,1,2,1,3 +???#???.???#?#?#??. 3,6 +#??.??#..? 3,1 +????.???????? 2,1,1,1 +??.??#?#..???????? 1,4,8 +##?.?????? 2,2 +?..#?.##????????# 1,1,11 +.#?#???.?#???.?? 1,1,2,2,1 +???????.??##?? 2,1,4 +???#?.#???????##? 2,2,1,7 +???????.??.??. 2,1,2,1 +?..?#?..???#?? 2,1 +???#?.#?.????#?.??? 4,2,5,1 +??#??.#???#??.?#?. 4,1,3,1 +?.????..?????#??# 3,1,5 +???##??#???#.?? 1,5,2,1 +?????.???. 2,1 +?????#.??.?? 6,1 +?#????????. 1,5 +????.???#??#????.# 4,2,1,3,1,1 +?#.?#?#.##?????##? 1,3,9 +????.???#.#?##??.? 1,1,4,1,4,1 +#.??.?.??##????#?#? 1,1,1,5,1,2 +???.?????##?#?? 1,4,2,2 +????#?.?.? 1,1,1 +?#??.?...?#???????.? 2,1,8 +??##???##?? 4,2 +????#??#??#??#??? 11,1 +#?????????##???? 4,9 +????????#????#??#?? 1,15 +???.#?.?#? 1,2,1 +?.#..#???#??. 1,1,2,3 +?.???????? 2,4 +????????#????#?.? 1,1,7,1 +??..????????.?.??#?? 7,3 +#??????#??##.. 1,2,1,2 +?.?##????#.?...?. 3,1 +#?#?#?###??##?#? 9,2,2 +?.##???#?#???#?#?? 2,12 +##?.?#.#????#?? 2,2,7 +????#?#??#???. 5,1,1 +?????#.?.###??.??? 6,4,3 +????#?###??. 1,1,3,1 +?????#??.?#????. 1,5,2,1 +???.??#??#?.??#?.??. 3,7,3,1 +.?#?.?#..?#?##??#.?? 3,1,7 +?.#????#.????#?#? 3,1,1,6 +#??.?????..????.???? 1,4,1,2,3 +?###????..?##?? 8,4 +.#???????#?##?#?? 1,1,1,1,5 +???##?????#?#??#.?.# 1,11,1,1,1 +?????????? 2,2 +????#??.#??? 6,1 +#????????????.?##? 4,1,2,2,2 +?.?##?#????#???#?#.? 1,4,9 +?#.?###?.??????#??.? 2,3,8,1 +?????..#?..?? 3,1,2,1 +#?#?#?#?????.?# 5,1,1,1,1 +?#???#??.#??# 1,4,4 +??#??#?##?? 2,5 +????#.#???#???#??.# 1,2,3,6,1 +#???..#?#???????.?# 3,1,2,1,1,1 +?????#????? 1,4,3 +?????.?????#?#????. 2,1,1,5,1 +???#??????#. 2,3,2 +#??.?????? 1,1,4 +.?#?????.???????? 3,1,1,4 +#.???????????#?????? 1,4,1,1,3,1 +?.?###?#?????#??. 4,1,6 +?#?##??????? 6,1 +.????#?#### 1,2,4 +???..?#?????? 1,1,1,3 +?#?.??.?.#???#??? 2,1,2,4 +#?????????.?#??#??? 2,2,2,1,1,1 +???#?????????. 1,3,5 +##.??.#.?.?##??#? 2,1,1,7 +.???##?.?.##? 4,3 +??##?????#???#? 4,7 +.?.??????##?..?????? 8,1 +??#???????#???.?? 1,1,1,5,1 +.#??#?#???????#???. 1,4,1,2,1,1 +??.????#?.##. 1,2,1,2 +?#?????#??.???#?. 2,2,3,3 +?#?????#??????#..? 13,1 +??#.???#???#??..?#? 1,9,2 +##??.?...?????#? 3,1,1,1,2 +#?.??#??.? 2,2 \ No newline at end of file diff --git a/2023/12/src/data.rs b/2023/12/src/data.rs new file mode 100644 index 0000000..5b7bd47 --- /dev/null +++ b/2023/12/src/data.rs @@ -0,0 +1,5 @@ +#[cfg(test)] +pub const EXAMPLE1: &'static str = include_str!("../data/example1"); + +#[allow(unused)] +pub const INPUT: &'static str = include_str!("../data/input"); diff --git a/2023/12/src/main.rs b/2023/12/src/main.rs new file mode 100644 index 0000000..27c3237 --- /dev/null +++ b/2023/12/src/main.rs @@ -0,0 +1,10 @@ +mod data; +mod part1; +mod part2; +mod puzzle; + +fn main() { + use data::INPUT; + println!("Part 1: {}", part1::run(INPUT)); + println!("Part 2: {}", part2::run(INPUT)); +} diff --git a/2023/12/src/part1/mod.rs b/2023/12/src/part1/mod.rs new file mode 100644 index 0000000..46a595c --- /dev/null +++ b/2023/12/src/part1/mod.rs @@ -0,0 +1,17 @@ +use crate::puzzle::Puzzle; + +pub fn run(input: &str) -> usize { + let puzzle: Puzzle = input.parse().expect("parse failed"); + puzzle.0.iter().map(|r| r.solution_count()).sum() +} + +#[cfg(test)] +mod test { + use super::*; + use crate::data::EXAMPLE1; + + #[test] + fn test1() { + assert_eq!(run(EXAMPLE1), 21); + } +} diff --git a/2023/12/src/part2/mod.rs b/2023/12/src/part2/mod.rs new file mode 100644 index 0000000..b7beba2 --- /dev/null +++ b/2023/12/src/part2/mod.rs @@ -0,0 +1,17 @@ +use crate::puzzle::Puzzle; + +pub fn run(input: &str) -> usize { + let puzzle: Puzzle = input.parse().expect("parse failed"); + puzzle.0.iter().map(|r| r.unfold(5).solution_count()).sum() +} + +#[cfg(test)] +mod test { + use super::*; + use crate::data::EXAMPLE1; + + #[test] + fn test1() { + assert_eq!(run(EXAMPLE1), 525152); + } +} diff --git a/2023/12/src/puzzle.rs b/2023/12/src/puzzle.rs new file mode 100644 index 0000000..c52040d --- /dev/null +++ b/2023/12/src/puzzle.rs @@ -0,0 +1,175 @@ +use std::{num::ParseIntError, str::FromStr}; + +use memoize::memoize; + +#[derive(Clone, Debug, PartialEq, Eq, Hash)] +pub enum Cell { + Unknown, + Operational, + Damaged, +} + +#[derive(Debug)] +pub struct Record { + pub springs: Vec, + pub damaged_groups: Vec, +} + +#[derive(Debug)] +pub struct Puzzle(pub Vec); + +#[derive(Debug)] +pub struct ParseError; + +impl From for ParseError { + fn from(_: ParseIntError) -> Self { + Self + } +} + +#[derive(Debug, Clone, PartialEq, Eq, Hash)] +enum Pat { + Damaged, + OperationalSection, +} + +fn make_pattern(damaged_groups: &[usize]) -> Vec { + damaged_groups + .into_iter() + .map(|&size| vec![Pat::Damaged; size]) + .collect::>() + .join(&Pat::OperationalSection) +} + +impl Record { + pub fn solution_count(&self) -> usize { + solution_count_helper( + false, + self.springs.clone(), + make_pattern(&self.damaged_groups), + ) + } + + pub fn unfold(&self, times: usize) -> Self { + Record { + springs: vec![self.springs.clone(); times].join(&Cell::Unknown), + damaged_groups: vec![self.damaged_groups.clone(); times].concat(), + } + } +} + +#[memoize] +fn solution_count_helper(in_damaged_section: bool, springs: Vec, pattern: Vec) -> usize { + let result = { + if springs.len() < pattern.len() { + return 0; + } + match &pattern[..] { + [] => match &springs[..] { + [] => 1, + [Cell::Damaged, ..] => 0, + [Cell::Operational | Cell::Unknown, springs @ ..] => { + solution_count_helper(false, springs.into(), pattern) + } + }, + [Pat::OperationalSection, pattern_rest @ ..] => match &springs[..] { + [] | [Cell::Damaged, ..] => 0, + [Cell::Operational | Cell::Unknown, springs @ ..] => { + solution_count_helper(false, springs.into(), pattern_rest.into()) + } + }, + [Pat::Damaged, pattern_rest @ ..] => match &springs[..] { + [] => 0, + [Cell::Operational, ..] if in_damaged_section => 0, + [Cell::Operational, springs @ ..] => { + solution_count_helper(false, springs.into(), pattern) + } + [Cell::Damaged, springs @ ..] => { + solution_count_helper(true, springs.into(), pattern_rest.into()) + } + [Cell::Unknown, springs @ ..] if in_damaged_section => { + solution_count_helper(true, springs.into(), pattern_rest.into()) + } + [Cell::Unknown, springs @ ..] => { + solution_count_helper(true, springs.into(), pattern_rest.into()) + + solution_count_helper(false, springs.into(), pattern) + } + }, + } + }; + result +} + +impl FromStr for Record { + type Err = ParseError; + + fn from_str(s: &str) -> Result { + let (springs, damaged_groups) = s.split_once(' ').ok_or(ParseError)?; + Ok(Record { + springs: springs + .chars() + .map(|s| { + Ok(match s { + '?' => Cell::Unknown, + '.' => Cell::Operational, + '#' => Cell::Damaged, + _ => return Err(ParseError), + }) + }) + .collect::>()?, + damaged_groups: damaged_groups + .split(',') + .map(|s| s.parse()) + .collect::>()?, + }) + } +} + +impl FromStr for Puzzle { + type Err = ParseError; + + fn from_str(s: &str) -> Result { + Ok(Puzzle( + s.lines().map(|s| s.parse()).collect::>()?, + )) + } +} + +#[cfg(test)] +mod test { + use super::Record; + + fn solution_count(s: &str) -> usize { + s.parse::().unwrap().solution_count() + } + + #[test] + fn test1() { + assert_eq!(solution_count("???.### 1,1,3"), 1); + } + + #[test] + fn test2() { + assert_eq!(solution_count(".??..??...?##. 1,1,3"), 4); + } + + #[test] + fn test3() { + assert_eq!(solution_count("?#?#?#?#?#?#?#? 1,3,1,6"), 1); + } + + #[test] + fn test4() { + assert_eq!(solution_count("????.#...#... 4,1,1"), 1); + } + + #[test] + fn test5() { + assert_eq!(solution_count("????.######..#####. 1,6,5"), 4); + } + + #[test] + fn test6() { + assert_eq!(solution_count("?###???????? 3,2,1"), 10); + } +} diff --git a/Cargo.lock b/Cargo.lock index 08d09ff..367edf7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,17 @@ # It is not intended for manual editing. version = 4 +[[package]] +name = "ahash" +version = "0.7.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "891477e0c6a8957309ee5c45a6368af3ae14bb510732d2684ffa19af310920f9" +dependencies = [ + "getrandom", + "once_cell", + "version_check", +] + [[package]] name = "aho-corasick" version = "1.1.3" @@ -53,6 +64,12 @@ dependencies = [ "syn 2.0.101", ] +[[package]] +name = "cfg-if" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801" + [[package]] name = "coord_2d" version = "0.3.6" @@ -251,12 +268,32 @@ dependencies = [ "syn 1.0.109", ] +[[package]] +name = "getrandom" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "335ff9f135e4384c8150d6f27c6daed433577f86b4750418338c01a1a2528592" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + [[package]] name = "grid" version = "0.16.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fb6ae361963ea5fe52038156ea1729f3b4e4ccc0711c362ab2b2d2c0a259e7c3" +[[package]] +name = "hashbrown" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" +dependencies = [ + "ahash", +] + [[package]] name = "hashbrown" version = "0.15.2" @@ -282,7 +319,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3954d50fe15b02142bf25d3b8bdadb634ec3948f103d04ffe3031bc8fe9d7058" dependencies = [ "equivalent", - "hashbrown", + "hashbrown 0.15.2", ] [[package]] @@ -326,12 +363,56 @@ dependencies = [ "syn 2.0.101", ] +[[package]] +name = "lazy_static" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" + +[[package]] +name = "libc" +version = "0.2.177" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2874a2af47a2325c2001a6e6fad9b16a53b802102b528163885171cf92b15976" + +[[package]] +name = "lru" +version = "0.7.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e999beba7b6e8345721bd280141ed958096a2e4abdf74f67ff4ce49b4b54e47a" +dependencies = [ + "hashbrown 0.12.3", +] + [[package]] name = "memchr" version = "2.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" +[[package]] +name = "memoize" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8d1d5792299bab3f8b5d88d1b7a7cb50ad7ef039a8c4d45a6b84880a6526276" +dependencies = [ + "lazy_static", + "lru", + "memoize-inner", +] + +[[package]] +name = "memoize-inner" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6dd8f89255d8ff313afabed9a3c83ef0993cc056679dfd001f5111a026f876f7" +dependencies = [ + "lazy_static", + "proc-macro2", + "quote", + "syn 1.0.109", +] + [[package]] name = "nom" version = "8.0.0" @@ -711,6 +792,12 @@ version = "0.9.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" +[[package]] +name = "wasi" +version = "0.11.1+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ccf3ec651a847eb01de73ccad15eb7d99f80485de043efb2f370cd654f4ea44b" + [[package]] name = "winnow" version = "0.5.40" @@ -806,6 +893,13 @@ dependencies = [ "position", ] +[[package]] +name = "y2023d12" +version = "0.1.0" +dependencies = [ + "memoize", +] + [[package]] name = "y2024d01" version = "0.1.0"