From 994be7e629d3fea41313622b8d34b3b0a478321f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Moritz=20H=C3=B6lting?= <87192362+moritz-hoelting@users.noreply.github.com> Date: Tue, 12 Dec 2023 10:49:03 +0100 Subject: [PATCH] day 12 --- Cargo.lock | 8 + Cargo.toml | 14 +- day-12/Cargo.toml | 12 + day-12/src/bin/input.txt | 1000 ++++++++++++++++++++++++++++++++++++++ day-12/src/bin/part1.rs | 116 +++++ day-12/src/bin/part2.rs | 135 +++++ 6 files changed, 1275 insertions(+), 10 deletions(-) create mode 100644 day-12/Cargo.toml create mode 100644 day-12/src/bin/input.txt create mode 100644 day-12/src/bin/part1.rs create mode 100644 day-12/src/bin/part2.rs diff --git a/Cargo.lock b/Cargo.lock index 8e8fc9d..e1c86a8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -143,6 +143,14 @@ dependencies = [ "rayon", ] +[[package]] +name = "day-12" +version = "0.0.0" +dependencies = [ + "indoc", + "nom", +] + [[package]] name = "deprecate-until" version = "0.1.1" diff --git a/Cargo.toml b/Cargo.toml index 7455f1d..cace6c3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,16 +1,10 @@ [workspace] resolver = "2" members = [ - "day-01", - "day-02", - "day-03", - "day-05", - "day-06", - "day-07", - "day-08", - "day-09", - "day-10", - "day-11" + "day-*" +] +exclude = [ + "day-04" ] [workspace.dependencies] diff --git a/day-12/Cargo.toml b/day-12/Cargo.toml new file mode 100644 index 0000000..464b9fb --- /dev/null +++ b/day-12/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "day-12" +version = "0.0.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +nom.workspace = true + +[dev-dependencies] +indoc.workspace = true diff --git a/day-12/src/bin/input.txt b/day-12/src/bin/input.txt new file mode 100644 index 0000000..df0de98 --- /dev/null +++ b/day-12/src/bin/input.txt @@ -0,0 +1,1000 @@ +??#??????#..????? 9,2,1 +???##??#?.?#? 5,1,2 +????.????. 1,1 +.?#?????###???. 1,6,1 +????.?#????#?? 2,1,1,3 +??.??.?????#??##.??. 1,1,10,2 +??.?###?#??????? 2,7,2,1 +?????..#??? 4,1,1 +#?????.?#???.#.???# 1,2,4,1,1,1 +??#??#???????.?#??? 1,1,7,1,2,1 +?..??#????#?.? 3,1,2 +..#?????.#.??#.? 1,3,1,1,1 +?#.?.?.??#??#?#??? 2,1,1,10 +???#???.##?#?????#.? 1,1,1,6,1,1 +?.??#?#?#?#?? 1,8 +?#??????????? 2,4 +??????????? 4,1,1 +????.??#?. 4,3 +??###??????? 6,1,1 +?.?.?#???#?. 1,5 +??.?#?..???.???###? 3,1,7 +?????#.????? 3,2 +#???????.??????#?? 1,4,1,2 +?#??????????#??. 1,11 +?.????..?..???##? 4,1,6 +??????#???#?#?.# 9,1 +?#?#??#?##?##?.#??. 10,3,3 +??##?#?##????? 8,2 +???#??#?.?.???? 5,2 +?.??.?..??????.????# 1,2,1,6,3,1 +?..#????#???#??.?.?# 1,1,9,1 +.?#?.?????. 2,1 +???##.???#???#???? 1,2,5,3,1 +??????????#?.?.??.?? 9,1,1,2 +.#.???????.???#. 1,1,5,1 +??.??????##?.? 1,2,4 +?????.??.#?.#.# 1,2,1,1,1 +#?????.???????#.??? 1,2,1,1,2,2 +?#?????..#????#? 6,1,4 +.??????.???#??? 5,1,1 +??????.?#??# 5,2,1 +??.?#?.?#?.#??? 2,2,1 +???#?.#???.?. 4,3,1 +???.?#??##..##??.? 1,6,3 +???##?#?#.#???.?#?. 1,7,3,3 +#.?????#??????? 1,1,1,6,1 +?#.?.???## 2,1,2 +?.???#????? 1,2,1 +?.?#??.?#?#??.#.?#?? 1,1,6,1,1,1 +??.????.??#? 1,1,1,2 +??.#?#??.???#??#?? 1,1,1,1,7,1 +.?.??.#??? 1,1,2 +????????##?#??#?..? 6,1 +?.???#?#.?.?#. 1,5,1,1 +?.??#???.??. 4,1 +???.???.?#.?#??#?.?? 2,1,2,2,3,1 +.#????#.??#? 6,4 +#?????.???##?? 4,1,1,4 +..????#??????????# 10,3 +#????##?#..? 9,1 +?#.?.?#?.? 2,1,1 +?#?.##???????.????? 3,4,2,3,1 +?..?#??##????????# 1,7,1,1 +??.?#.??????????. 1,2,2,6 +?????..?#?????????#? 1,1,8,1,2 +??#???????. 1,4 +?????????##.?????? 1,8,1,4 +#?#.?.??###??#????? 1,1,1,9 +??#????#.??????.??## 1,6,2,2,4 +...#?.?###? 1,4 +#.?#..???#??. 1,1,1,3 +.????.???? 1,3 +.#??..???????.?? 1,1,5,1 +.???????????? 3,1,4 +?..#????????.?.#?? 1,3,1,1,1,1 +???##??.???.#? 2,2,3,1 +??#?#?????????. 7,1,1,1 +???###?#??.???? 9,2 +??#?.??????.??##???? 3,1,2,4,1 +????.???.? 2,1,1 +?..?...#??????#?.?? 1,2 +????#???#?????? 4,1,6 +??####??????.#??#?? 8,2,1,1 +#???????.??.??. 5,1,1,2 +????#?#??? 1,5 +.????????? 1,2 +???????#?. 1,2 +?###..##?. 3,2 +.??#??.??????.??#.#? 2,1,4,1,1,1 +?.??###?#.? 5,1 +???##???#?#?.#?. 11,2 +.??#???..?#?????#? 3,5,2 +???#?#.???#?? 1,1,4 +?.??.??.?? 1,1,1 +???.???#???#?????#? 1,12 +?#??????#?.?#? 3,1,3,2 +?#??.?##???.?.?. 3,5,1 +????.?????# 1,4,1 +.???#???#??#.?.??? 2,5 +##??????.?????? 5,1,1,1,1 +.?##?###??#?? 7,2 +#.????#?????????#??# 1,2,11,2 +?.??.?#???????? 1,1,10 +???#????#?? 2,1,1 +????..??#? 1,2 +.??..????##.? 2,5 +?????.#??? 1,2,1 +?..#.??????? 1,1,4 +??#??#???###?# 5,3,1 +???.?#..#? 1,1,1 +??#?????#??#????##?? 1,2,1,3,1,6 +??????????###??.?. 2,3,3,1,1 +??#???.??? 3,1,1 +????????.? 1,3,1 +?#??.??????.? 2,6 +???.#????..#??#? 2,1,3,5 +????.?#??#? 2,2,2 +??.????#????#????#. 1,1,5,1,1,1 +??#.????##?#??# 1,8 +?#???##?#???.#????. 8,2,1 +#?#????#?#?.#?. 1,8,2 +#??#??##?#???. 1,10 +.?????.??? 5,2 +?#???????.?. 2,1,1,1 +?.##???..??#??###?? 5,1,8 +?.??????#???.?#? 1,7,1 +??.#??????.?.#???#? 1,4,1,1,3 +.?#?????#????# 2,10 +##??????#..? 2,2,3,1 +#?????.??????#?? 6,2,2 +?.#?????#?# 5,1,1 +????.???#????????? 4,12 +???##??.??????? 5,3,1 +#?????#.?.??.#?# 1,1,1,1,3 +??##??#?#? 4,1,1 +???????????.?? 1,3,1,2 +.?????#.??#. 2,2,1 +#.#?.??##????#????? 1,1,1,7,1,1 +?.??#.??????? 1,1,1,2 +#?.?#??#?????.?.? 1,5,1,1 +?.?.??#??? 1,4 +???#??#?.?.????#.#. 7,1,2,2,1 +..???#????????#? 5,1,1,2 +##????..#???? 3,2,4 +?.?.#?#?????.#??#?? 6,1,4 +???.?##???.????.?? 2,4,1,1,1,1 +??#????.?#?. 4,2 +?#???????#?#???#? 3,1,7,2 +????..#???? 2,5 +?#.?????????? 2,1,3,1 +?????.??##?#.#????? 1,1,6,1,2,1 +#??#??..#? 1,1,1 +?????#????#??#???.? 2,8,1,1,1 +.????##?.#????#?. 4,6 +.?.???.#?###.? 1,5 +?.??##??????.#??# 8,4 +?#????#??#. 2,5 +??#??#.#???#???# 1,2,2,1,1 +.#?..???.#??#?????#. 2,1,1,2,5,1 +?.???##??#?##.? 1,7,2,1 +?#??#?#.??.?.? 3,3,1 +??#???#???#????. 11,1 +.###?????????? 4,1,3 +??#??#????..###?.?? 6,2,3,1 +#???.##?.?? 4,2,1 +??.##??#??.????# 6,4 +????#?????. 1,7 +???.???????#???#? 2,1,10 +?????###??.?##?? 1,5,3 +???#.##?#???#?#??? 2,1,8,1,1 +?..?##?.?????#? 1,3,5 +????????????###???? 1,1,2,1,8 +#??.?#?#?.?????#?. 3,1,1,1,1,1 +?.?????.?#??##??# 2,1,3,5 +???##?#??????#.??? 10,1,1 +??.?###?#? 1,6 +???###??????? 6,4 +??????#.?.#??? 1,2,2 +.?.?.??#?????#???#?? 1,1,8,2 +?#..??????#.????. 2,1,2,1,2 +.??#?.????##??#.?.# 1,2,7,1,1 +??###????.???#???? 5,2,2,2,1 +?#???#?#..?? 2,1,1,2 +?.#?#??.???? 3,3 +.???#?.??????###???? 3,9 +????.##?????..#??.? 1,1,3,1,1,3 +????????.??????#? 1,3,6 +????.#.?#?#?????#??. 2,1,11 +#???????#? 1,1,1 +?????##?##? 3,6 +?#???.??##?#?????.# 1,2,7,1,1,1 +.?.#??.??.???##???#? 3,1,9 +#????????#???.??? 2,1,1,5,1 +????#??.?.?#??###?# 2,1,1,1,9 +?#.??..??###???? 1,1,1,5,1 +?.??????.?#.?.?#???. 6,2,1,2 +????#??????? 1,6,1 +#?.??.??#?.??#??? 1,2,1,1,5 +.?##?????#?##??.? 5,6 +?????..####????.? 4,8 +?#?????###???????. 9,5 +?????????#???..? 1,3 +??#?.???.?.?.?? 2,1,1 +???.??????#??#??#. 1,2,1,1,2,1 +.#???###??#???.?#??? 11,3 +?.???.??##?. 2,3 +???#???????##??#? 4,1,5 +.???#?.?????## 1,1,2,3 +?..????##????.??#.?. 7,2 +?#?#.??#???.????#.? 4,6,1,1 +.#??????##??##.?###? 2,2,4,2,3 +?????#???##???.##?. 1,4,3,1,3 +#..???##???##?.??#. 1,5,4,1 +.????#?#..????#??. 6,2 +??#??.???#?####. 2,1,8 +.???.??#?? 1,4 +?#?..????#??????? 2,9,1 +????#??#??#???.?. 6,4,1,1 +???????#.?..?? 5,1,1 +??##?????##.??.????? 11,1,1 +.#?##?????. 5,1 +#.??????#??????? 1,10,1 +??#??????.##???. 1,1,1,1,4 +?????#??#?. 1,1,2 +?.?#?#??.?.#???? 4,3 +#?#.?#.????? 1,1,1,1 +??????????#? 5,1 +??????#?.?##?#?????? 3,7 +.????????.?...? 1,2,1,1,1 +??##???##??#.???? 1,2,4,1,3 +???#?.#????? 1,1,1,2 +.??????.????.??#? 4,3,4 +??????.??????? 1,1,2,2 +????..#??#??? 1,2,1,1 +??#??????#?????.???. 10,2 +?..#???..??..?.? 4,1 +??#?#??#??.#.??#? 7,1,1 +..?#???.?.?????? 5,3 +?.????#???.?#?? 1,2,4,2 +.?#????#??.?? 5,3,1 +.#.????##??????????# 1,17 +???????.???.# 2,2,2,1 +??##?????.??. 7,1 +?.?.????.??? 1,1,1,1 +?.????????.# 1,4,1 +?????#??..#?#.? 1,1,3,3 +?#?.#?##??#?? 1,5,2 +?.??##??#??#???? 5,4 +?.?##?..???###??? 3,6 +??#.??#.??.. 2,1,1 +#?##?????????#???? 1,2,1,6,2 +?.?????.?????#??.#.? 1,1,1,8,1,1 +??.?#?.#?##????#.? 3,5,3 +..??.????????..? 1,2 +.???..????.???..? 3,2,2,1 +?.?.??.???. 1,1,1 +????#?#??????.??.??. 8,1 +??#????????? 4,1,3 +??#??????..#?#?? 2,1,2,3,1 +?#??#.???# 1,1,1 +??#?????#?? 2,4 +????#.?#???? 5,1,2 +??.???#?????.??#??#? 1,3,1,6 +?#.?#.????.#? 1,2,2,1 +#?#??#????.?.??????? 8,1,1,2 +??##???#.??. 4,2,1 +?##??#???.? 6,1,1 +?.#?#.?#??????? 3,5 +?????#???#..?. 1,8,1 +??#..#????.. 2,1,1 +?#????.?#?. 2,2,3 +?.#????#????###?? 8,3 +????...?.?? 2,1 +##.????#?????.?##?? 2,5,4 +.?#????.???? 6,1 +??.?????..???..??? 2,3,3,2 +???????#???#???????? 1,1,8,1,1,1 +.?.???.?#??.?.?? 1,1,3,1,1 +????#.????###.?? 1,1,1,7,1 +?.?##?#???.#???.? 5,4 +????#?...??.##?????? 2,2,1,5,1 +??.???###? 2,6 +??.??.?.?##???. 1,2,6 +???????#???#?.??#??? 10,5 +?.???????#?#??. 1,8,3 +..?#?##?.???? 4,2 +.?##?#?????. 5,1 +?#????#?.??. 1,2,1 +??#?#.???????. 3,3 +???.????#??.# 2,7,1 +????#?.#????????? 5,1,1,1,1 +????##???????? 5,1,1,1 +???#?????..?#? 2,5,3 +?..??##.?????#.?? 4,1,1 +.?#????.???#####?? 4,2,7 +???.?###.???.? 4,2 +?#?????.?#???? 6,4 +???#?.?..????? 5,1,1,2 +??.#??????####??? 1,1,2,1,7 +?.???????#? 2,3 +..#????????#? 8,1 +?#?##?##?##????. 12,1 +?#?.??.?.?# 2,1,1 +.?.?#???#?.????. 1,6,1 +?#???.??.???????? 5,1,1,2 +???????#?#??.?..?? 1,6,1,1,1 +???????#???.. 3,1,1 +..??#?????????? 1,2,6 +??????##?????#???## 8,1,2 +??????.#?####?#?#??# 1,1,1,6,2,1 +#?#????..#?.? 1,5,1,1 +?##???..?#? 2,1,1 +.????#.????.??#?#. 1,2,2,3,1 +?###??????#. 5,1,1 +.?##??#?????...???? 9,4 +?.???#?##?#??? 1,10 +???.?.?#???#???? 2,1,3,2,2 +?????##?##?#? 2,2,2,2 +????#???## 2,2 +??##..#.??.? 4,1,1,1 +??..#?#????? 4,1 +.??#??????#?????#?? 3,3,3,4 +#?#??#?#??#??. 6,1,4 +???##????.?#. 5,1,2 +??????##?????# 7,1,2 +#??##?#?#????#?????? 12,1,1,1 +?.?.???.???. 1,1,3,2 +???#.??#####??##?? 1,1,12 +??#?##.?#.?? 4,2 +?????????.???.??#?. 1,6,1,4 +?.#??.?#.???.?. 1,1,2,1,1 +?.#????#??.??? 6,2 +#?..?#?#???###??#?? 2,14 +????????##?????.?#. 3,5,1,2 +.#??#?..?????#????? 2,2,8,2 +#?????#?????.?.?# 1,1,4,1,2 +???#???????? 1,2,6 +???????.?..? 1,1,1,1 +..?#.?#???.#????#?. 1,3,1,1,1,2 +.??#?????##????##.?? 11,3 +.##?.##???##?#?#??? 3,11 +?##???#.????#??.#? 7,1,1,3,1 +???.??#??#?????????? 1,1,1,10,1 +?#.#??????#????#???# 2,4,3,7 +.#????#?##??#????#? 2,8,3 +????????#???.???#??? 1,5,1,2,1,1 +.#??????#? 2,4 +#?#?????????# 3,5,1 +..??#??.#?? 1,1,2 +??.?#????.??? 3,3 +???#?#?##?#?????? 11,1 +#?????#?##???#?.?.?? 4,8,1 +?#??.?#??. 2,4 +???????..?#?.??? 3,2 +?.##?#?.????##?. 5,4 +???.#???????????##? 1,7,2,3 +.??#?.??#??? 1,1,4 +??.??.?#??? 1,4 +..?????#.?.. 1,1 +??????.???#?#?##??# 1,1,1,1,6,2 +??##?#..#??##?#???.? 5,7,1 +???#?#????#?.#. 1,5,1,1 +.????????? 1,1,1 +?#??????????????? 6,1,1,1,1 +?.?##???..#. 1,2,1,1 +#?#????.#.?????##??. 3,1,1,7 +?????.?#?#??#??????? 1,1,1,6,5 +??..?.??.#????# 2,1,2,4,1 +?##??#??????????.? 9,2,1,1 +#?.#..?#?### 1,1,6 +???#???.???#??#.?? 1,1,1,3,1,1 +?#?????.?#????#???? 3,9 +.??????.??.? 4,2 +?#?#?.?#??#?#???? 4,2,2,1,1 +???.#??#?##??? 1,1,6 +??.?..??.?..#????. 1,1,2,1,2,1 +???#?????. 2,1,2 +.???#??#??? 3,4 +#????#?#???? 1,2,3,2 +??#??.#?... 2,2 +??.?.??#??#?. 2,1,5 +???#??????????? 8,1 +#????#?????# 2,7 +??????.#.##?? 4,1,2,1 +?.?.?#?###??#? 1,1,6,2 +??.?.?#???#???? 1,1,8,1 +?###?..??????.#?.??# 4,5,1,1,1 +??##?#???????##??# 6,8 +???.?????.????. 1,3,1 +????#?????????#??? 9,3 +??#???#??????#???? 10,3 +.??.##?#??#???#??.. 9,2 +..#?#?###???.? 7,1 +?????#??#???#??.?.? 2,6,2,1,1 +??????.???#????.? 1,8 +.?????.#.##? 2,1,2 +?.??#?#??????? 1,4,1,1 +??#???#.?.?#??#? 2,2,2,3 +.?#?..????# 2,1,2 +.??##??#?#??? 4,2,1,1 +?#???#?#?#..? 2,3,1 +.?????###????. 1,5,1 +??.???#???#?# 1,8 +?#?.#?#?#??.?#?. 2,6,2 +?.????#??#???.?? 1,7,1 +?.??...???? 1,2,1 +.#?.?#.?.???? 1,1,1 +.#?##??????? 5,3 +..#?#??.#????#?? 3,7 +?.?#??##?#?????##?? 1,1,7,2 +??????#..?#????? 6,2,2 +??#.??.?..?.???? 2,1,1,1,2 +???..???????? 3,7 +???????.#.??????. 1,1,1,1,6 +?#?.??.?#?#?#??????. 2,1,6,1,1 +.?#??????. 1,1,1 +.?##??#.????? 6,1 +?.?#?.?#.?? 3,1,1 +?????.????? 1,1,2 +??.???????#?? 1,5 +???????#?.???.????? 3,2,1,2,5 +??????????##?????? 2,2,6,1,1 +??#????.?????#?##?# 2,1,9,1 +??????#?#.?.?. 8,1 +?..#?.??##.?.?? 1,2,2,1,1 +??..#??##.?#????#? 1,1,2,1,4 +????#?#..?.#?????? 7,1,1,5 +.#??????????.???.? 6,1,1,2,1 +#?..???.?? 1,3,1 +..?????.??.??# 2,2,2,3 +.??????.???#??.?? 1,3 +..?#.?.????.#?????? 1,1,2,7 +??#?#?.???????.? 3,1,1,3 +????#??????????## 11,1,2 +##??????.???????. 5,1,1 +#????#????#?##? 1,10 +???##???##?#?????#?? 15,1 +????.??????#?#?? 2,7 +???#??..?#??##?? 3,8 +.##?.#.???? 2,1,2 +.?#?????????#?##?#?? 2,1,8,1,1 +???###..?#?..??###?? 5,3,1,5 +?#?.??#?#?. 1,3,1 +??????#?????#??.?.# 1,6,5,1,1 +??????#???.# 3,4,1 +??#?.??.?##.?#? 3,2,2 +????????..??.?.. 1,1 +#???#??.????# 7,1,2 +??????.?#??. 1,1,1,2 +?.#???#???????.? 1,6,1 +#.#.##????.#? 1,1,6,1 +?.?#?????.?#?#? 4,5 +??.?#????? 1,1,1 +.?##?????#.????.??? 7,1,1 +####.#???#.?.???#?#? 4,1,2,6 +???.##??#???##????? 1,1,14 +??????#??#??#?#???? 7,1,2,2 +.#.????##???????? 1,9,3 +#??#?.???##?? 2,2,2 +#??#?.??.? 5,2 +?#?##?#####????#.?# 12,1,1,1 +.?#?#??#??#?????.. 10,2 +?????.???#????? 3,7 +?.????#.??.??.???##? 1,2,1,1,1,4 +?????#??..?????##? 6,7 +?#??#?.???????##? 1,1,1,1,3 +?##?????.#.?#? 8,1,1 +.?#????.??? 4,2 +#????#???#????.?#?? 1,1,8,3 +?.??????#????#???# 1,1,1,7,2 +???????#?#?#?? 2,7 +??#?????.#? 3,2,1 +?#?.??.?..#????.??## 1,1,1,5,1,2 +?????..????????. 2,1,4 +.???#?#??#??#?.???? 13,1,1 +.#?..???????? 1,1,1,2 +??????#??????? 3,9 +??#?.??.?.????#???? 4,1,2,2 +??##??.????#? 3,1,1 +.???##?.?????###. 4,6 +?#.??#???#? 1,5,1 +????.???.????#. 2,1,5 +??.?.?#?????#?. 2,3,2 +?.???##??##.????? 9,1 +..?.??????????#?.??? 1,1,9,1,1 +#?.##??##??#???.? 1,7,1,1,1 +#???.#?.????????.# 3,2,3,1,1 +?#???#?.#?????## 1,2,2,1,2 +.#?##?##??#?# 1,10 +?#?##.????.#?????? 1,2,2,1,2,4 +#..??###????.#? 1,5,2 +.???#??????##???#?? 8,4,1 +?.????#???????????. 1,9,2 +?#??#????##?.??????? 11,1,2 +??#?#.???? 5,1 +#?????.???# 4,1,2 +???#??#???????????? 2,2,1,7,2 +????.???#?????? 1,1,1,1,3 +?????????.???#????? 9,1,3,1 +??????.??.??.???.??. 4,1,1,2,2,1 +???.?#???????#??? 2,4,2,2 +###???#???#??.??##?. 7,5,5 +.?????####???????? 6,1 +????.????? 1,1,1 +?????###??#???? 2,5,5 +.??.???#??.?.???. 2,5,1,3 +#.??#????.???### 1,3,3,1,3 +????????#.? 1,3 +????##??#.?????. 1,3,1,1,1 +..#????#..?#?????? 3,1,5 +??#??#?#???#?.? 2,2,2,3,1 +###??#?????.?. 4,5 +????.?#???.?? 3,2,1 +#??????..??#??????? 4,2,2,1,2 +???#?#?..#??.??? 4,3 +???#????..?? 8,1 +#?.?#?????? 2,2,3 +??#.????.????## 1,1,1,1,4 +???#.?.#?..? 1,1,1,1 +#??#????#??##?#? 6,2,5 +.????.#???. 1,2 +?????.?????.??##??.? 3,1,1,2,4,1 +.?#??#??.?. 2,4 +????????#???..? 2,6 +?#???##???#?.?..??# 8,2,2 +.??#?..#????.?? 1,5 +.??????#???.?#?#. 1,4,3 +????.?????#? 1,1,1,2 +.#.?.?.??? 1,1,1 +#??????.#? 3,2 +?????.#?##?.???#. 2,1,5,2,1 +???#?.?.???##??.?. 1,4 +?##???????? 4,5 +?#?#??#???#??.#?#?? 3,8,4 +##?????#??????? 2,5,1,1 +????.?#??..????##?? 1,1,1,1,6 +????.#???.????#??#? 1,3,1,3,2 +?..???#??###???? 1,1,8 +??#?#??#???? 3,4,2 +#????.??.#?? 5,1,1,1 +?.????##???#?????.? 1,11 +??????..?.?#?.??. 1,2,1,3,1 +?????.?#?? 1,1,1 +.?.????????# 1,2,5 +.?.?.#?#.????. 1,1,1,3 +.???.??#?????#?? 3,10 +???#?#????.?? 5,2,1 +??#.???.?? 2,1,1 +??###??????.?#?? 11,3 +??.?????????#?. 2,2 +.??#?#??##???????.# 12,1,1 +.##??????????.. 3,1,1,2 +?????##.?#??.#?.??# 1,1,2,1,1,3 +.??..???#??#??.# 2,1,1,2,1 +??#.?#.#?.?##?#?#??# 2,1,1,6,1 +?.??.??#??? 1,1 +#?##??.?##?#?#?#?? 5,10 +.??#?.#??#. 1,2,1 +?#??????#?.?? 1,5,1 +##???..???#?..?????? 2,2,4,3 +??.#????.#?? 1,1,2,1 +?.?#.???.???? 1,1,2,1 +.???##?..?..?#?.??? 3,1 +?????????.##????.??. 7,4 +##???#?.??.???. 3,1,2,1 +????????#..#?.?##?.? 9,2,3 +?.??.?..?#. 1,1,1 +???????.?##. 5,1,3 +#?#.#.???????????##? 3,1,6,6 +?.??#??????? 4,3 +??#?##???.##? 8,2 +???.???.#????.#?.? 2,3,3,1,2 +???????##???? 2,2,4,1 +????##?#???? 1,7,1 +##??..???? 2,1,1 +?#??..??.? 3,1 +?.??#??#..????.??? 3,2,1,1,3 +#?#..#?????#??.? 3,1,5 +?????.???????#??. 1,3,2 +?.?#????.?? 4,1 +?#???#????????#? 11,1 +#????#.#??#????#?#?. 1,3,6,3 +?????##?.##??.? 2,3 +??.??#??..??? 4,2 +?#?#???#??#??##????? 1,8,4,1 +?????.?.?. 2,1,1 +?.?#..???##??.?. 1,1,5,1 +??##???#???#?#.?.?. 1,8,1,1,1,1 +?#???#??????.???#??. 10,2,2 +#?.???#??#?.?????? 1,7,4 +???????##?#??? 1,1,8 +??.?????##?? 1,1,5 +??#??##??##?????##?? 2,2,9,1 +?.?#??#?##?.???????? 8,7 +???#?#?#?#??????.?? 2,1,11 +.????????..?? 1,1,2,1 +?#??#?.??.?#? 4,1,1 +???.?????#.?#??.. 6,3 +#..?#???????. 1,8 +?.???##?##?##??#?..? 14,1 +?.???..??.??##?? 2,2,2 +????#.#?.? 1,1,2 +??????#??????#??? 1,4,1,4 +?????.?.??. 4,2 +?##??..#?.? 3,2 +#?.??#.?##??#??#? 1,1,1,8 +??#??##?##?#?#?..? 2,10,1 +??????##??????.???? 1,10,1,1 +?????...?# 3,1 +?#?.?#?#?.?# 3,1,1,2 +.#?.?#.?#? 1,1,2 +??#????????#???.???? 13,1 +.????????###????#. 2,1,8 +???.?##??.??????# 1,5,1,2 +????????.?? 1,5,1 +??.?#?##???#?#.. 5,3 +.#????????#?#? 3,4,4 +???????.??#??? 4,4 +##..????##?.?.?. 2,2,4,1 +?.???????#?#.????#? 10,4 +.?????#??.????#??. 5,1 +?#.##??#?????. 1,7 +?????????.????? 6,1,2 +.?..????#? 1,4 +?.##???????#?? 1,3,5 +??#?????#??#?#?????. 2,11 +.?.???????## 1,6 +#???..?.?.?? 3,1,1,1 +##?????..??? 2,1,2 +#?.??.???????#.???? 1,1,1,2,2,4 +?#?#?????.?.?? 4,1,1,1 +?????#?#?.???.?.? 8,1,1,1 +??###?#??????.?.#?. 10,1 +?#????.?????.?? 1,1,4,1 +?#?#???##?#??#??#.# 1,1,7,4,1 +#.?????#????? 1,1,6 +?..#??.???. 1,2 +????#?#??.???. 1,5,1 +.???.?????#?##?..? 1,8 +#??#?#???????.#???#? 6,4,1,1,1,1 +.?.##?.?..??????? 3,5 +??.#???.#?#?.???#??? 1,4,3,1,1,2 +.##?.??.?## 2,1,2 +?##.??#.?#???#? 3,2,1,3 +.??????#?????? 8,2 +#..?????#????#.?#?#. 1,6,1,1,2,1 +.????.??.?#?? 2,2,3 +?#?#???????####.?#? 3,1,4,3 +?#.????#.?.??? 1,1,2,2 +???????.?#??#????? 1,1,9 +.#?##.????? 1,2,3 +???#??#?.??.?. 8,1 +..??#??.???#???.? 4,4 +???#??##???##??????? 13,1 +?????#.?????#???#? 1,1,1,1,4,1 +????##????.??.?##. 6,3 +#???.#?.?#? 1,1,2 +##???????#?#.???? 10,1,1,1 +??#??##??#??.??.?.? 10,1 +?#??#?#????#????? 1,9,1 +#???????.##?.?#???? 2,1,3,2,2 +?###??#?#?.#..??? 10,1,1,1 +??##??.?????.??. 3,1,1,1,1 +??#.?#???.?? 1,5 +?##?#????##??#? 10,1 +???#??????##?#???? 2,10 +???#?.??#??? 1,1,4 +???#?#????##?#?.?? 13,1 +?#??????????.??. 2,4,2 +?#.?##?#?? 1,4 +????????..? 1,1 +.??##?#???? 5,2 +?##???#????###? 3,5,4 +??..?.???????##?? 1,1,8 +??#?##?????##??. 8,3 +.##?..#?????##...? 3,8 +????????#??.???# 10,1,1 +.??????#?????. 1,1,5 +?????#???#. 1,3,1 +#.?#.??#.???##.. 1,1,2,4 +??#.?.??#? 3,1,1 +?.?.##??#??#.?.# 1,8,1,1 +??????##?#?#? 1,5,1,1 +??????#?????..????? 3,3,2,5 +?##?????#.? 5,1,1 +??????.??#.##???? 2,3,3,1 +####???????#??#?# 6,1,5,1 +.??#??#.?#.????##??# 1,1,1,2,1,6 +#?#?#?#????????.?? 9,4,1 +???###?##??.???. 1,8,1 +???##???#??#.??? 2,6,2,1 +???.??#????? 1,6 +..?????#?. 1,1,1 +#?#??.#????#? 5,6 +.????#?????#?? 5,1,1 +??.??.??#.??????.# 1,1,3,1,1,1 +???????..#?#??#. 5,6 +??.???###??##?????# 1,1,12,1 +???.??????#?##?. 2,10 +#????????.?..#.#.? 2,5,1,1,1,1 +.?#???##.???#????##. 7,1,1,2 +???????#???.##???# 2,1,1,6 +?#???#???.?.?? 2,4,1,1 +?.?.?.???.???.???? 1,1,2,1,2,1 +?#??????.?#????#???. 4,1,1,1,1,1 +??#?.##???? 1,1,5 +?????#???# 3,1,1 +.????.????#.. 3,1,2 +???????#?.??##.???.? 5,2,1 +?????????#?. 8,2 +????#?#?.?.??##????? 1,5,1,1,4,1 +?#.?#????####??#.? 2,12,1 +?.?##..????#? 3,5 +???#???#??? 1,6 +??###???#????#.????? 1,7,1,1,2,1 +??.????.?? 1,1,1 +????.?##?#?## 2,7 +.??????#???. 2,5 +???##????#?#..## 1,2,4,2 +.????.?##?? 1,1,4 +#.??#?#.????.?.. 1,5,3,1 +#.??.?#?.?#??. 1,1,1,3 +???#??????#.?#?? 6,1,1,2 +??.?.?#?.?? 1,3 +?#???????#.? 4,1,1 +?..????##????????.? 1,5,1,1,3,1 +#..?#.?#??#??#??##?? 1,2,2,9 +#??#?##??..??. 1,5,1,2 +?#?????#????#??#..?? 2,12,1 +??#.???#??????? 3,2,3,1,1 +.?????.#????#???? 3,9 +???.????#??????.?# 1,2,1,4,1 +??.???.?#?# 1,3,4 +#????????#??#..?#.? 4,8,2 +????#?#?#??????????? 9,6 +#???????#???#???.?.? 1,1,1,9,1,1 +?????#?#?#??????. 1,9,2 +?#????..?#????# 1,1,1,7 +#?????????### 5,1,3 +.#?????????? 1,3,1,1 +?.?##???????#??#..?# 7,1,4,1 +?#???????##??##?? 3,11 +.????.#??.?#?#?#?.#? 2,1,1,1,7,1 +???????#?.. 2,2 +??#?#.?###??##??#?? 1,1,1,8,1,1 +??##.?????????#?? 1,2,10 +.?????.?#?. 1,1,2 +#?????#??#???#..# 1,1,6,3,1 +?#????#???.?.#?. 1,1,4,1,2 +??#???##??.????#? 8,5 +.????.?????#??. 4,1,4 +????.??###?. 3,6 +????.#???.? 1,4,1 +??#???#????###?.. 3,8 +.?#???????###??.## 13,2 +????.#.????#??? 1,1,1,1,2 +#??#?????.? 4,1,1 +#?#.???????.? 3,3,1,1 +.????????.??. 3,2,1 +??.?.??.??##?????? 1,1,1,9 +?..?#??#?#??#.?? 1,7,1 +??.???##??#?.?? 2,5 +.????#.??????? 3,1,2,3 +??..??????#? 1,1,2,2 +??#??.????# 1,3 +??.???????? 1,6 +??.?.?????????.???. 1,6,3 +#???###???.#??? 1,6,2 +?.#???#??????# 2,4,1 +??##.???#??.????##. 2,5,6 +##.?.????????# 2,1,3,1 +#?##.??.#???? 4,4 +????#????..?#?? 5,3 +.??##?#???.?.???? 9,1,1 +???#?#?#?.?.?.. 8,1,1 +#????##???#???#???.# 4,2,7,1 +.?.???#.?##?????# 1,1,2,3 +#?????????#???..#. 5,8,1 +???#??????#?..??? 5,3,2 +???#?????? 4,1,1 +?.???#????####????#? 1,3,6,1 +????#?.#???.??#??? 1,1,2,1,5 +##????#?..???? 7,2 +?#??.?.??#?..???#? 4,1,1,2,5 +.##????????..?? 6,1,1,1 +????????.#????. 1,1,1,1,2 +??????????#??.?????? 3,4,2,1,1,1 +?#?.??????.? 1,2,2 +.?.?#????#???? 2,1 +?.?#????.?? 1,6,1 +.?##???.?????. 2,2,4 +#.????##?? 1,7 +??#???##?#?? 2,6 +.?.?#????. 3,2 +?#??#?#.?.?##???? 6,1,4,1 +?????.?.???#??????? 1,1,1,5,3 +.#???????.#? 5,1 +????..??#???#?. 3,8 +?#??#???.?#?##.?? 7,4 +?#????#..#????????? 2,1,1,4,1,1 +...?#??#?.#?? 3,2,2 +###???#?.???? 4,2,3 +???#??????..?##? 4,4,4 +????.????#? 2,1 +????##?#???.???? 7,1,1 +?????????.?..??. 3,2,1,2 +#?..#????? 1,1,2 +##???#????#?.???.. 7,2,1,1 +??#????????#?..?##?? 11,3 +.?#?..#??##???.. 3,8 +##?#????#.?#?.#???## 2,2,3,2,2,2 +???#???????????.? 1,3,5,1,1 +#.????????#? 1,1,1,3 +??.?.??.?.?.??????? 1,2 +???.#???????#??## 1,3,8 +..?#??.??##?????. 2,5 +????.?????????? 1,1,2,2 +#????.??#? 5,2 +?.#??#????#?????.?# 5,5,1 +?.?#??#??? 4,1 +????????.????.? 8,2 +????.?#?????. 1,1,3,2 +???#??.?#??# 5,5 +????#.?#?.##??????. 1,1,3,5,2 +.?#??.?#?#?????. 4,4,2 +??..?#?#????.#? 6,1 +.?.#.?#????#???#?##? 1,1,15 +#.?????##.? 1,5,1 +.##?????.????????? 5,2,4 +???.???.##???#.?#?? 1,1,1,6,4 +???#??#?????# 1,2,4,1 +#???.##??##????? 4,9 +???...#?...???# 1,2,3 +##?#?#????.# 6,1,1 +.??#??..????.?# 2,1,1,1,1 +##??#???????#.??# 5,1,1,1,2 +.#???#?????##.??#?. 1,3,1,1,2,3 +??????#???? 2,3 +?.???#??#? 1,2,2 +?#??.##?????#? 2,2,1,2 +??#.??#?.??.#?#??? 3,3,1,1,1,1 +?.?##?.?.???#?#.? 1,3,1,4 +??.???#?###?#.? 1,1,6,1,1 +.?.#?????#?.?????? 1,3,3,2,1 +?#?#?####..#.? 8,1 +???##?#????.??#??#?? 9,1,3,3 +??#????#?.??#?. 1,7,4 +???#..?.?? 1,1,1 +?????#????.??###? 6,1,6 +.#?????#?????.??# 1,5,1,1,2 +???#????..???#### 3,7 +???????##..? 1,4 +.??.??.???... 1,2,3 +.??.#?#?????#? 1,9 +???#???.?????? 1,4,2,1 +??.?#.?.??.?#???#?? 2,1,1,8 +##.##?????????#.?? 2,2,7 +???###??##?..??.??? 5,3,1,2 +#..????#?#???? 1,1,4,2 +???????????#? 2,2 +??.?##?#????????#??# 1,8,2,1 +????#?#???????????? 7,1,1,2,1 +.#??.?#??.# 1,2,1 +?#???????? 2,1,2 +?????#??#???.??? 1,4,1,2 +?.?????.?.????##? 3,7 +???..#???#???#??. 1,2,7 +???.#??#?#???#.?? 1,1,8,2 +###?##?.???#??# 7,5 +.???.?.??#? 1,1,2 +??#??.???# 2,4 +??###?????. 6,1 +???#?#????.?.?#.?.# 4,5,2,1,1 +.??????.?#...?#??. 2,1,3 +.??#.??#?? 1,2 +??#??.?#??#? 1,1,1,4 +.?#?#?..#?.??? 5,1,2 +#??#?##?#?..?#??? 1,7,1 +#???????## 2,3,2 +???.????##??#?#?.#?? 1,11,1 +#.?..????#?#??..?#? 1,1,9,1 +.?.?????#??? 1,4,1,1 +.???.???..#?##??? 3,1,5 +??..????.???.?# 1,4,1,2 +???????#?? 1,1,3 +.????????#??#.??. 1,9,1 +?.?.#????.#????????. 3,7 +????#??#??????.??.#? 8,3,2,1 +?????.#???..???#??? 2,1,4,4,1 +??#????..?.? 5,1 +???#?#?#????.?##???? 11,7 +?????????????#??? 4,10 +?.??#...?#???.?.. 1,3 +.????...???#...? 1,2 +??#?.#.?????##? 3,1,1,1,2 +????#.??#?.#. 1,1,3,1 +#.??????#??##??#???? 1,17 +###????.#?? 5,2 +??????????#.?.?...?. 10,1,1 +#.#.#.???##??#??.# 1,1,1,9,1 +.??.??.#?. 1,1,1 +?#???.???##??#?#? 1,1,1,8 +.??##?#??????#???## 9,3,3 +.???????#? 5,3 +.????###??#?.???? 9,2 +#?##??????????? 7,3,1 +???..#?#???#???.? 1,9 +??#.???#.# 2,1,1 +#.?#??.#?.?? 1,3,1,2 +.#??????#???????. 2,4,1,2 +..??????.??? 1,2 +?##???.??.??#???. 2,2,1,3,1 +.?.?##???#.?? 3,2,1 +..???????? 3,1 +??.??#????????#?#? 1,1,1,1,5 +#?.?#.###??##??????? 2,1,9,1 +.#?????.??. 2,1,1 +.??.??#????## 2,4,2 +?#?#?#?#?###?..?? 12,1 +??#??.??#???? 3,2 +#??#??????.?????? 1,1,4,1,1 +#??#???#??.????#??? 4,2,5,1 +???????.?#?? 5,1 +?.#??#??#????#???.? 8,6,1 +.?##?#?.?.?? 5,1 +??###??????##??. 5,4 +?#?#.???.??# 1,1,2,2 +.???????.?#?... 3,3 +??.?#?????.?.??##?#? 1,1,3,1,7 +?#.???????.???#? 2,1,1,1,2 +.??#?????. 4,1 +????.?#???.??????.. 2,3,1,5 +.??????.#? 1,2 +.??.???#?.? 2,3,1 +#?##?.?.???.## 5,1,1,2 +.##????.?? 4,1 +#??.?#???.?##?... 1,1,4,3 +?...##??.? 1,2,1 +?###..#??? 3,1,1 +?#?##?#??##??? 1,8,1 +.??###??.?#?.???###? 4,3,5 +?.??????#?##???? 1,1,9,1 +?????#??.?????? 7,2 +.???#????#???#? 6,2,3 +????.????.?#?..??? 3,4,3,2 +????.#?.???#?? 1,1,2,6 +?.#???#?#?.?#?? 1,5,2,1,1 +?#?.#.???????.?? 1,1,1,3,1 +?#?????????????? 3,1,2,1,3 +???.#??#?#???# 7,1 +??#????#???.##??#. 8,1,5 +???#?????#??.? 2,5 +.#????.???? 5,1 +??????#?????###?? 5,3 +?..??#???.????? 1,1,1,2,1 +#??.?...?.??. 2,1,2 +?#???#?????.#??? 2,1,1,2,1 +?#?????????.??????? 2,3,1,3,2 +.?#???????? 2,2,1 +?#??.?.?.?.? 3,1 +?##?..????#?? 4,1,4 +???#?..???????# 1,2,7 +??#??##?.??????.?? 2,2,1,1,1,1 +.#?#...#?????? 3,1 +.??..??##? 1,4 +?????##??.??##?.. 6,5 +#?..?#???.?? 1,3,1 +?##??.?.#?#????? 3,1,3,1,2 +.?#?????..#?#..?? 7,3,1 +????????##?##.?#.#?? 1,1,1,7,1,2 +???..????.?.???##?? 2,2,1,1,2,1 +??????.??????.# 3,1,2,1 +#.??#????.??#?? 1,3,1,1,1 +??#####??#?????#?# 10,3 +##???###?#????? 12,1 +????????????? 2,5 +??#??.????.?##? 2,4,2 +?.?#???.#?#.???#.. 1,4,3,2 +????#????#? 2,2,2 +???.#?.?#????? 1,1,2,6 +?.#???#?????#?????. 5,5 +??????#??#??? 1,7 +??.??????#???#?????# 1,1,7,3,1 +#?..?????? 1,3 +??#???..???.# 3,2,2,1 +?###?????? 6,1 +???##.#??#?? 1,3,1,3 +???##??..?#?#??? 3,4 +??????##??.?????#? 9,1,5 +?#???#?.?#???? 5,5 +??.?.???.?? 1,2 +??##??#?????# 2,7 +???????.???????##?#. 1,1,1,1,7 +#?#????#???.##.? 8,1,2,1 +???#.??###??..#?#?? 1,1,4,1,1,3 +.?.??###???#?.?????? 7,1 +?#?#?#???????.?#? 5,2,3,2 +???##???.??????#? 1,3,1,1,5 +???????#?#??.? 1,2,5 +????#?.??? 2,1,1 \ No newline at end of file diff --git a/day-12/src/bin/part1.rs b/day-12/src/bin/part1.rs new file mode 100644 index 0000000..eae1806 --- /dev/null +++ b/day-12/src/bin/part1.rs @@ -0,0 +1,116 @@ +use nom::{branch::alt, character::complete, combinator::value, multi::many1}; + +fn main() { + println!("{}", part1(include_str!("./input.txt"))); +} + +fn part1(input: &str) -> u32 { + input.lines().map(process_line).sum() +} + +fn process_line(line: &str) -> u32 { + let (springs_str, group_size_str) = line.split_once(' ').expect("Invalid input"); + + let group_size = group_size_str + .split(',') + .map(|s| s.parse::().expect("invalid char as number")) + .collect::>(); + + let springs = many1(alt(( + value( + SpringStatus::Operational, + complete::char::<&str, nom::error::Error<&str>>('.'), + ), + value( + SpringStatus::Damaged, + complete::char::<&str, nom::error::Error<&str>>('#'), + ), + value( + SpringStatus::Unknown, + complete::char::<&str, nom::error::Error<&str>>('?'), + ), + )))(springs_str) + .expect("Invalid input") + .1; + + let spring_amount = springs.len(); + let groups_amount = group_size.len(); + let mut dp = vec![vec![vec![0; spring_amount + 1]; groups_amount + 1]; spring_amount + 1]; + + dp[spring_amount][groups_amount][0] = 1; + dp[spring_amount][groups_amount - 1][group_size[groups_amount - 1]] = 1; + + for pos in (0..spring_amount).rev() { + for (group, &max_count) in group_size.iter().enumerate() { + // try iteratively all possible counts for the current group + for count in 0..=max_count { + // try both operational and damaged for each position + for &c in &[SpringStatus::Operational, SpringStatus::Damaged] { + // only proceed if the spring is of the chosen type or unknown + if springs[pos] == c || springs[pos] == SpringStatus::Unknown { + if c == SpringStatus::Operational && count == 0 { + // if operational and count is 0, then add the value from the next position because + // there is no new combination + dp[pos][group][count] += dp[pos + 1][group][0]; + } else if c == SpringStatus::Operational && group_size[group] == count { + // if operational and count is equal to the group size, then add the value from the + // next position and next group + dp[pos][group][count] += dp[pos + 1][group + 1][0]; + } else if c == SpringStatus::Damaged { + // if damaged, then add the value from the next position and next count + dp[pos][group][count] += dp[pos + 1][group][count + 1]; + } + } + } + } + } + if matches!( + springs[pos], + SpringStatus::Operational | SpringStatus::Unknown + ) { + dp[pos][groups_amount][0] += dp[pos + 1][groups_amount][0]; + } + } + + dp[0][0][0] +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +enum SpringStatus { + Operational, + Damaged, + Unknown, +} + +#[cfg(test)] +mod tests { + use super::*; + use indoc::indoc; + + #[test] + fn test_part1() { + assert_eq!( + part1(indoc!( + " + ???.### 1,1,3 + .??..??...?##. 1,1,3 + ?#?#?#?#?#?#?#? 1,3,1,6 + ????.#...#... 4,1,1 + ????.######..#####. 1,6,5 + ?###???????? 3,2,1 + " + )), + 21 + ); + } + + #[test] + fn test_process_line_part1() { + assert_eq!(process_line("???.### 1,1,3"), 1); + assert_eq!(process_line(".??..??...?##. 1,1,3"), 4); + assert_eq!(process_line("?#?#?#?#?#?#?#? 1,3,1,6"), 1); + assert_eq!(process_line("????.#...#... 4,1,1"), 1); + assert_eq!(process_line("????.######..#####. 1,6,5"), 4); + assert_eq!(process_line("?###???????? 3,2,1"), 10); + } +} diff --git a/day-12/src/bin/part2.rs b/day-12/src/bin/part2.rs new file mode 100644 index 0000000..bc8a837 --- /dev/null +++ b/day-12/src/bin/part2.rs @@ -0,0 +1,135 @@ +use std::vec; + +use nom::{branch::alt, character::complete, combinator::value, multi::many1}; + +fn main() { + println!("{}", part2(include_str!("./input.txt"))); +} + +fn part2(input: &str) -> u64 { + input.lines().map(process_line).sum() +} + +fn process_line(line: &str) -> u64 { + let (springs_str, group_size_str) = line.split_once(' ').expect("Invalid input"); + + let group_size = group_size_str + .split(',') + .map(|s| s.parse::().expect("invalid char as number")) + .collect::>() + .repeat(5); + + let springs = many1(alt(( + value( + SpringStatus::Operational, + complete::char::<&str, nom::error::Error<&str>>('.'), + ), + value( + SpringStatus::Damaged, + complete::char::<&str, nom::error::Error<&str>>('#'), + ), + value( + SpringStatus::Unknown, + complete::char::<&str, nom::error::Error<&str>>('?'), + ), + )))(springs_str) + .expect("Invalid input") + .1; + + let springs = vec![ + springs.clone(), + vec![SpringStatus::Unknown], + springs.clone(), + vec![SpringStatus::Unknown], + springs.clone(), + vec![SpringStatus::Unknown], + springs.clone(), + vec![SpringStatus::Unknown], + springs, + ] + .into_iter() + .flatten() + .collect::>(); + + let spring_amount = springs.len(); + let groups_amount = group_size.len(); + + let mut dp = vec![vec![vec![0; spring_amount + 1]; groups_amount + 1]; spring_amount + 1]; + + dp[spring_amount][groups_amount][0] = 1; + dp[spring_amount][groups_amount - 1][group_size[groups_amount - 1]] = 1; + + for pos in (0..spring_amount).rev() { + for (group, &max_count) in group_size.iter().enumerate() { + // try iteratively all possible counts for the current group + for count in 0..=max_count { + // try both operational and damaged for each position + for &c in &[SpringStatus::Operational, SpringStatus::Damaged] { + // only proceed if the spring is of the chosen type or unknown + if springs[pos] == c || springs[pos] == SpringStatus::Unknown { + if c == SpringStatus::Operational && count == 0 { + // if operational and count is 0, then add the value from the next position because + // there is no new combination + dp[pos][group][count] += dp[pos + 1][group][0]; + } else if c == SpringStatus::Operational && group_size[group] == count { + // if operational and count is equal to the group size, then add the value from the + // next position and next group + dp[pos][group][count] += dp[pos + 1][group + 1][0]; + } else if c == SpringStatus::Damaged { + // if damaged, then add the value from the next position and next count + dp[pos][group][count] += dp[pos + 1][group][count + 1]; + } + } + } + } + } + if matches!( + springs[pos], + SpringStatus::Operational | SpringStatus::Unknown + ) { + dp[pos][groups_amount][0] += dp[pos + 1][groups_amount][0]; + } + } + + dp[0][0][0] +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +enum SpringStatus { + Operational, + Damaged, + Unknown, +} + +#[cfg(test)] +mod tests { + use super::*; + use indoc::indoc; + + #[test] + fn test_part2() { + assert_eq!( + part2(indoc!( + " + ???.### 1,1,3 + .??..??...?##. 1,1,3 + ?#?#?#?#?#?#?#? 1,3,1,6 + ????.#...#... 4,1,1 + ????.######..#####. 1,6,5 + ?###???????? 3,2,1 + " + )), + 525152 + ); + } + + #[test] + fn test_process_line_part2() { + assert_eq!(process_line("???.### 1,1,3"), 1); + assert_eq!(process_line(".??..??...?##. 1,1,3"), 16384); + assert_eq!(process_line("?#?#?#?#?#?#?#? 1,3,1,6"), 1); + assert_eq!(process_line("????.#...#... 4,1,1"), 16); + assert_eq!(process_line("????.######..#####. 1,6,5"), 2500); + assert_eq!(process_line("?###???????? 3,2,1"), 506250); + } +}