From 529fd293d4a522800cb1383192cf7b7352dad3eb Mon Sep 17 00:00:00 2001 From: Anthony Oteri Date: Wed, 13 Dec 2023 11:56:50 -0500 Subject: [PATCH] Day-13 - Part 1 Signed-off-by: Anthony Oteri --- day-13/Cargo.toml | 29 + day-13/benches/benchmark.rs | 15 + day-13/input.txt | 1375 +++++++++++++++++++++++++++++++++++ day-13/src/bin/part1.rs | 21 + day-13/src/bin/part2.rs | 21 + day-13/src/error.rs | 9 + day-13/src/lib.rs | 4 + day-13/src/part1.rs | 157 ++++ day-13/src/part2.rs | 18 + day-13/test-input.txt | 15 + day-13/test-input2.txt | 31 + 11 files changed, 1695 insertions(+) create mode 100644 day-13/Cargo.toml create mode 100644 day-13/benches/benchmark.rs create mode 100644 day-13/input.txt create mode 100644 day-13/src/bin/part1.rs create mode 100644 day-13/src/bin/part2.rs create mode 100644 day-13/src/error.rs create mode 100644 day-13/src/lib.rs create mode 100644 day-13/src/part1.rs create mode 100644 day-13/src/part2.rs create mode 100644 day-13/test-input.txt create mode 100644 day-13/test-input2.txt diff --git a/day-13/Cargo.toml b/day-13/Cargo.toml new file mode 100644 index 0000000..66456f2 --- /dev/null +++ b/day-13/Cargo.toml @@ -0,0 +1,29 @@ +[package] +name = "day-13" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +itertools = { workspace = true } +nom = { workspace = true } +tracing = { workspace = true } +tracing-subscriber = { workspace = true } +miette = { workspace = true } +thiserror = { workspace = true } +dhat = { workspace = true } + +[dev-dependencies] +divan = { workspace = true } +env_logger = { workspace = true } +test-log = { workspace = true } +rstest = { workspace = true } + +[[bench]] +name = "day-00" +path = "benches/benchmark.rs" +harness = false + +[features] +dhat-heap = [] diff --git a/day-13/benches/benchmark.rs b/day-13/benches/benchmark.rs new file mode 100644 index 0000000..e984740 --- /dev/null +++ b/day-13/benches/benchmark.rs @@ -0,0 +1,15 @@ +use day_13::*; + +fn main() { + divan::main(); +} + +#[divan::bench] +fn part1() { + part1::process(divan::black_box(include_str!("../input.txt"))).unwrap(); +} + +#[divan::bench] +fn part2() { + part2::process(divan::black_box(include_str!("../input.txt"))).unwrap(); +} diff --git a/day-13/input.txt b/day-13/input.txt new file mode 100644 index 0000000..90d02d9 --- /dev/null +++ b/day-13/input.txt @@ -0,0 +1,1375 @@ +#.#.##.##.# +#..#..####. +#.#####..## +#.##..####. +##.#..####. +..#...#..#. +##...#....# +....##....# +##.##.#..#. +#..#.##..## +##.###.##.# +...#.#.##.# +.#.#.#.##.# +##.###.##.# +#..#.##..## + +##.#...#.#. +##.##..#.#. +.#.##...... +..######..# +..######..# +.#.##...... +##.##..#.#. +##.#...#.#. +...##...#.. +...#.###..# +#..#######. +..#.##.###. +..###.##.#. +..##.##.##. +..#.#..##.. + +#.....###..###. +.###....#..#... +.#..#.#..##..#. +##.##.#..##..#. +.#....#.#..#.#. +.###.#..#..#..# +#.###..######.. +#..#...######.. +..#.##.##..##.# +..#.##.##..##.# +#......######.. + +.##.####### +.##....##.# +#..#.#.#.## +#####....## +####.....## +#..#.#.#.## +.##....##.# +.##.####### +####.#.###. +#..#####..# +......##### +........### +#..###.###. + +#..##.. +#..#... +###.#.. +##..### +####.## +..#.#.. +..#.### +.##.##. +.##.##. +..#.### +..#.#.. + +##.##.##### +#.####.#.## +..#..#....# +#########.. +########.## +...##...#.# +.#....#...# +########..# +..####..... +#......###. +#......###. +..####..... +########..# +.#....#...# +...##...#.. +########.## +#########.. + +...#.#..#....#. +#.##..##.####.# +.##.#.##.#..#.# +#.#.#..##.##.## +..#.######..### +...#.###......# +.##...##......# +##..#.##.####.# +.###......##... +##..#..#..##..# +####.#..##..##. +.###..#.######. +#....##...##... +#.....#...##... +.###..#.######. + +#..#### +.##.#.. +.##.#.# +#..#### +#..#### +.##.#.# +.##.#.. +#..#### +.##...# +#..###. +..###.. + +##.##.....#.# +##.##.......# +......#.##... +##.#.#..#.#.. +##.#..####### +##...###..#.. +##.##.#..#.## +####.#...#..# +###.....####. +...##....#... +...##.#.##.## +....#..#...## +###...#...... + +#####.##..# +#.#..###### +.#.#...#### +.#.#...#### +#.#..###### +###.#.##..# +...#.###### +##.#...#### +....#..#### +.#.#....##. +.....#.#..# + +.#....#.#..## +.#....#....## +..#..#....... +.##..##...### +##########..# +.#.##.#.###.# +..#..#...#.## +#.####.##..#. +.#....#..##.. +#......###.## +.#.##.#..###. +...##...####. +.#....#...#.. +##.##.####.## +##....##...## +#.####.####.. +###..#####.## + +.##.####....# +.##.#..####.# +#####...#.### +.......#..... +####.#...##.. +.##....####.# +.##....###### +.##....###### +.##....####.# +####.#...##.. +.......#..... +#####...#.### +.##.#..####.# +.##.####....# +.###..#...##. + +#####.#.#.#..#.#. +#..###.###.##.### +....#...#..##..#. +#..####..#....#.. +#..###..#.####.#. +#####.#.#..##..#. +#####.#.#####.##. +.##..###..####..# +#..#.#..########. +#..##.#####..#### +####...#..#..#..# + +########..... +...##.....##. +.........#### +#.#..#.#####. +#.#######..#. +#.####.#.#..# +###..#####.#. +#.#..#.#..##. +..####....#.# +..####...#... +#.#..#.#.#..# +#.####.#..... +#.####.#..... + +###....#.##.#.# +..#....#....### +#####.#..#.#.## +##.#.#..##..#.. +..###..##...#.. +#####.##...#### +.....#...##.##. +###....####..## +##.###.##.###.. +..##.####.#...# +###.##...#..### +..##.#..##.#... +..##.##.##.#... +###.##...#..### +..##.####.#...# + +.#......#.####### +............####. +....##.....##..## +.###..###........ +....##....#..##.. +##.#..#.##.###### +...####...####### +..######..##.##.# +.########..#.##.. +###....#####.##.# +#...##...##...... +...#..#...#..##.. +##.####.##....... +.##....##.#..##.. +#.##..##.#.#.##.# +.#.#..#.#.##.##.# +.##.##.##.##.##.# + +....#.#.#..#.#. +####.#..#.#.#.. +#..##.#.#..#### +####..###...... +....###.#.###.. +....#.##....#.# +#..##.###.####. +....##.#...#... +#..#.#.#.#..### +####.#.#...##.. +####.#.#...##.. +#..#.#.#.#..#.# +....##.#...#... +#..##.###.####. +....#.##....#.# +....###.#.###.. +####..###...... + +..#####....#####. +##..##########..# +.##.#..####..#.## +..######..######. +..######..######. +.##.#..####..#.## +##..##########..# +..#####....#####. +.....########.... +..#..##....##..#. +.###.##.##.##.### +.#.#####..#####.# +..#....#..#.#..#. + +.###.##.### +.###.##.### +####....#.# +#...####... +####....### +.#...##...# +..###..###. +#..#.##.#.. +#....##.... + +#.#.#.#....#. +###..##.##.## +...##.#....#. +..##.#.####.# +##.#.#.#..#.# +.#..##..##..# +#.#...#....#. +...##.#....#. +...##.#....#. +#.#...#....#. +.#...#..##..# +##.#.#.#..#.# +..##.#.####.# + +#...... +.#.#### +#.##### +.##...# +..#.... +###.##. +##.#### +.#..##. +.##.... +##..##. +##..##. + +##..#.#..#..### +##.#......#.... +####....#.##### +.#.##.##....... +#..#.#..####..# +#..#.#..####..# +.#.##.##....... + +.#..###.# +..#...##. +#####.#.# +....#.### +#.###.#.# +###.####. +##..#..#. +.#.#..... +.#.#..... +##..#..#. +###.####. +#.###.#.# +....#.### +#####...# +..#...##. +.#..###.# +.#..###.# + +#.#.#.....###.##. +#.#.#.....###.##. +#...#.#.#####..#. +..#....#.####.... +#.#.##.##..###..# +#.#....#.#.#.#### +.##.#####........ +.#.#.##..#####..# +.#.......#.###### +..##.###.##.##### +####.#..##..#.... +##..#.#.##.##.##. +#.#..#.#....##..# + +#...#.####.#.#.## +#..#.#.#..##..#.# +.###...###..#.... +####..###.###..## +##..##...###.##.. +#.#.##.##..#.#..# +#.#.##.##.##.#..# +.##.#.##...###... +.##.#.##...###... +#.#.##.##.##.#..# +#.#.##.##..#.#..# + +..........##. +.##.#.#.####. +####..###.### +####.#..#..## +.##.##.#...#. +....####...#. +.##...###..## +####.###.#.## +....###.#...# +#..#.##....#. +#..#.##.#..#. + +.###...#..... +.###...#..... +..####.#####. +...#.##.##.## +#.###...###.. +####.#.#.###. +#.#.#.##...## +#.#.#.##...## +####.###.###. +#.###...###.. +...#.##.##.## +..####.#####. +.###...#..... + +.###..#.# +.#.#..#.# +.#.###..# +##....### +##....### +.#.###..# +.#.#..#.# +.###..#.# +..###.#.# +.##.#.#.# +####..### +###..##.# +#.#.###.# +###..#### +#.#.#.#.# +......#.. +#..#####. + +#...#..#..###..## +.#.##.#.##....... +.##.#.#...#...... +#..#.#...###.##.# +#.#...###.####### +####.##..#...##.. +##..#.####.###### +..#.#..#.####..## +...######.###..## +####....###...... +.#####.#.##..##.. +.##..#..#........ +####...###...##.. + +###.##..# +###.##..# +###.####. +##..##### +#....#..# +#..##.##. +..#..#### + +####......####### +####..##..####### +.##.#.##.#.##..## +###...##...###### +..##..##..##..... +.#..#....#..#..#. +####..##..####### +#..###..###..##.. +.####.##.####..## +.#..........#..#. +#..#..##..#..##.. + +....#..#..#..#..# +....#..#..#..#..# +#.#.#.###.#..#.## +..#..###...##...# +.#......##....##. +##..#...#.#..#.#. +#.#######.##.#.## +##...##.#..##..#. +####..#..######.. +.##..#..##.##.##. +...#...#..####..# +#####.###......## +.#####.#.######.# +.#.....#.#.##.#.# +.###.......##.... +.#...#....#..#... +.#.#..##..#..#..# + +###...### +..#.#.#.. +#.#...#.# +###.#.#.# +...##.#.. +####..#.# +..#..#..# +###....#. +....#.#.. +....#.#.. +###....#. + +..#..###.## +##.##.###.. +..##....##. +..##..##.## +##..##.#### +#####.#.... +##.#....### + +.#.#.##.#.. +#....#.##.. +#.#.###.### +#.##.###.## +.#.##...### +#.......... +...##....## +#...#...... +###.#.#.... +###.#.#.... +#...#...... +.#.##....## +#.......... +.#.##...### +#.##.###.## + +..####...#.####.# +#......##..#..#.. +#.#..#.#####..### +##....#####.##.## +..####......##..# +..#..#..#.#.##.#. +#..##..##.######. +..####..###....## +###..#######..### +.#.##.#.#..#..#.. +.######.##.#..#.# + +..##..###.. +..#####.... +...#.#.#.## +...#.#.#### +..#####.... +..##..###.. +##......##. +##.#..#.### +###..##.... +...##..##.# +##.###.#.#. +###.#.#.... +..##...#..# +####.###.## +....#..#.## + +#....##.... +..##....##. +#.##....##. +.#...##...# +#.......... +#####..#### +#....##.... + +..#..#. +##.#.## +#..#... +#...##. +...#... +...#... +#...##. +#..#... +##.#.## +..#..#. +....#.. +#.#..## +##....# +...#... +...##.. + +.####.####. +.###....... +..#####..## +...#.##..#. +#.#.###..## +.#..##....# +..#....##.. +#.#.#.#..#. +#.#.#.#..#. + +...#..####....#.. +....#.###...##... +###....#...#.#... +...###...##....## +##.##..#..###.#.. +#..#.#...#.#...## +..#....#...#..#.. +..#....#...#..#.. +#....#...#.#...## +##.##..#..###.#.. +...###...##....## +###....#...#.#... +....#.###...##... +...#..####....#.. +....#..###.#..### +#.....###....##.. +....####....##... + +#.#.#.#..#.#...#. +#..#.#.#..#...### +##.#..#.#.....#.# +###..#..#..#...## +##..#....#...#.#. +.##.#....#...##.. +.##......#...##.. +##..#....#...#.#. +###..#..#..#...## +##.#..#.#.....#.# +#..#.#.#..#...### +#.#.#.#..#.#...#. +.####...#..##.#.. +####.##.#####.##. +.####.#####..#.#. +#...###.......### +#...###.......### + +.#.##.#..##...# +###..###.##.#.. +#.####.##..#.## +#.#..#......#.. +#.#..#......#.. +#.####.##..#.## +###..###.##.#.. + +...#.##.##.##.#.. +##....######....# +##..#.##..##.#..# +###.#.######.#.## +..#...#.##.#...#. +...#.#.####.#.#.. +..###.##..##.###. +....#...##...#... +..#..##....##..#. +.#.#.#..##..#.#.# +##....#.##.#....# +##.#####..#####.# +##....#....#....# +...###.####.###.. +..#####....#####. +##...#.####.#...# +########..####### + +####.##...##.##.# +#..##.######..#.. +#..##.######.##.. +..#.#..########## +.#.#..#.###.##.## +#.#.#..##...###.# +#....#..#.#.#.... +#....#..#.#.#.... +#.#.#..##...###.# +.#.#..#.###.##.## +..#.#..########## +#..##.######.##.. +#..##.######..#.. + +##.#..# +######. +.#..#.. +.....## +.....## +.#..#.. +######. +##.#..# +.###.#. +..#.##. +....##. +.###.#. +##.#..# + +##....###.#..##.. +.#...##.##.##..## +#...##.###..#..#. +#..####...####### +#..####.#.####### +#...##.###..#..#. +.#...##.##.##..## +##....###.#..##.. +###.#.###..#....# +#...##.#..#..##.. +......#..#..#..#. +.#.....##...#..#. +..#...##.##...... + +.####.# +...#.## +#...### +#.##... +.##.... +....#.. +.#..#.. +.....## +.##..## +#...### +#...### + +##.#.## +#..#.#. +#.#..## +#.##.## +##..#.. +..#.### +.#.#### +.#..### +.#..### + +#.##..###.# +...####.##. +.#.###.###. +#.....###.. +..####.#### +....#..#.## +....#..#.## +..####.#### +#.....###.. +.#.###.###. +...####.##. +#.##..###.# +#.##..###.# +...####.##. +.#.###.###. +#......##.. +..####.#### + +.###...#...## +..##.#...#..# +....#.##.#### +..#....###.#. +..###.##.#### +..####..#...# +..##..#...#.. +.......##..## +...#.#.####.. +###.#.##.##.. +..#.###.#.#.# +####.....#.## +###...#####.# +...#####.#### +##.#.#####.## +##.#.#####.## +...#####.#### + +#..###.#..##. +.....#.....## +####....####. +....#.....### +#####.##.#.#. +.##..#####.## +.....##..#### +#######..#.## +.##..##..#... +#####.#..###. +#..#.###..### +#..#.###..### +#####.##.###. +.##..##..#... +#######..#.## + +..#..##.....# +###.#.##.###. +#.###.####### +####.#..#.... +####.#..#.... +#.###.####### +###.#.##.###. +..#..##.....# +#.######..##. +.#.##....##.. +##.#.....##.. +##.#.....##.. +.#.##....##.. +#.######..### +..#..##.....# + +........####...#. +........####...#. +#...######.##.##. +.####.##.#...##.# +##..###.#......## +#....##.###.#.... +#######....#.#.## +.####.#..#.###..# +.####.##...#.##.# + +#####.#.# +#..##.##. +.....#... +#####..#. +.##.....# +....#.### +....#.### +.##.....# +#####..#. +.....#... +#..##.##. +#####.#.# +.##..#... +..#...#.. +.##.##..# +....##... +####...#. + +###.... +#.###.# +###...# +.#.#.## +###.### +###.### +.#.#.## +###...# +#.###.# +###.... +.#####. +#..#... +.####.# +.####.# +#..#..# +.#####. +###.... + +......#..####..#. +##..##.#.####.#.# +..##...##....##.. +......#..####..#. +#.##.##..#..#..## +##..##.#.####.#.# +.........#####... +.#..#..###..###.. +#....###..##..### +#######........## +#########....#### +########..##..### +.......##.##.##.. +######.##....##.# +......###....###. +......#..####..#. +##..##.#..##..#.# + +..#..####..#. +..#..#..#..#. +##.##....##.# +...########.. +###..###...## +##.##....##.# +..#..####..#. +##....##....# +..##########. +##.########.# +...##.##.##.. + +..####..####..##. +.###.####.###.##. +##.##....##.##..# +.####....####.... +#.#.#.##.#.#..##. +#####.##.######## +#...#....#...#..# + +..##..#.##.##.##. +..##..########### +#....#..##.##.##. +.####.##.##..##.# +##..##..########. +######.##..##..## +#....##..#.##.#.. +#....###..#..#..# +.....##.##....##. + +.....#...##..#### +.....#...#...#### +.....#...#...#### +.....#...##..#### +.#..####...#...#. +#########.##..### +#####.##...##.#.# +..#.#.##.##...##. +....###..##.##.## +#.###..##.#..##.# +.....########..## +##..###.##..##### +.####.#.#..#...## +##..#..#.###.#.#. +#.##...##.#.....# +.#.#.....#....... +#...#####.######. + +#......###.##.# +.##.##.#.#.##.# +######...###### +.###.###..####. +###..####..##.. +###..####..##.. +.###.###..####. +######...###### +.##.##...#.##.# +#......###.##.# +#.#####..##..## + +#.##.#.#... +..##..##... +#.##.#.#### +.#####..... +#....##..## +..##....... +.####...... +#....#.#.## +##..###.#.. +.####.#.### +..##..#.... +#....#..... +.####.###.. + +#..###..#.####..# +#####.##..##..##. +####.#.##....##.# +####....#....#... +.##...#..####..#. +#####..#......#.. +#..#.....####.... + +#####.#..##.##. +........#...... +......##.###... +#####....#..#.# +.##....#######. +####.#..#.##... +.##.#.######.## +....#....###..# +#..#####....##. +.##.##..######. +......#.#.##.#. +####..#..####.. +.##...#..##..## +#..##.#.###.#.# +#..###..#..#.## +.##.#..##..#.#. +.##.#...#..#.#. + +.#.#..#.#.##.#. +##########..### +####..####..### +.###..###.##.## +##############. +#.##..##.####.# +##..##..######. + +##..#.####.#. +....#.#..#.#. +....#.#..#.#. +##..#.####.#. +.##.#..##..#. +.#.#.#.##.#.# +#.#.#......#. +#..######.### +..#.##....##. +.#.####..#### +....#..##..#. + +....#####.... +#######.####. +.#####.##..## +#.##.#..#..#. +#.###.#..#### +..#...##.#### +.#.#..#.##### +.#.#..#.##### +..#...##.#### +#.###....#### +#.##.#..#..#. +.#####.##..## +#######.####. +....#####.... +....#####.... + +.......## +.##..##.. +.##..##.. +.......## +#....##.# +#..##.#.# +#####...# +####..### +....#.#.. +#..#..#.# +........# +#####.### +....#..#. + +#...##. +#....## +#####.. +#.###.# +.###..# +.#.#..# +#.###.# +#####.. +#....## +#...##. +#...##. + +#.......####..### +.#.#..#.#........ +##.#..#.####..### +...####...#....#. +#.#....#.#......# +#...##...#..##..# +##.####.##......# +###....###..##..# +.########.##..##. + +#..#...#### +#..##.##### +#..##.#..#. +#..##.#..#. +#..##.##### +#..#...#### +##########. +.....#..#.. +#..#.#.#.## +#...#.#.##. +#..#.#.###. +....####... +.....#.#..# +.....##.##. +.##.##.##.# +.....###### +.##.#..##.. + +##.#....#..#.#. +####.##.#...##. +..##..###.##.## +........##..##. +...###.#..#...# +..#.##..#.##### +..####..##.#### +...#...##.#.#.. +..###.###.#.... +##...##.#.#.... +###..#.###...## +#####..####.##. +...#...##.#.### +..#....######## +...#.#######.## +..##.#.#...##.. +..##.#.#...#... + +..#.##.#... +.#.#....... +########.## +..#..###..# +....#..###. +....#..###. +..#..###..# +#######..## +.#.#....... +..#.##.#... +.#.#.#.#.## +.#...#.#.#. +#####.#.##. +##...###..# +##...###..# + +.#.....###.#..... +#.###...#.#...... +...#...##....#..# +...#...##....#..# +#.###...#.#...... +.#......##.#..... +#.#..########.... +..##.##.#####.##. +#...##.#.##.#.##. +#....#..#....#..# +.#.###.##...##### +...##..#......... +.#..####.##.#.... +......#.##...#### +#..#######..#.##. + +..##...#.#..#.# +...#.#.#......# +.##..###......# +.##..##.#....#. +#.#...##..##..# +#..#..###.##.## +#.##..#.##..##. +####..##......# +#.###.##.####.# +####.#..#....#. +.####..#......# +...##.##.####.# +#.####.#..##..# +.##.####..##..# +..#.####..##..# + +.##..###..#.....# +.##.###.....#.##. +.##.###.....#.##. +.##..#.#..#.....# +.##......###..#.# +#..#.###.###..##. +......####.####.# +#..#.####..#..#.. +.##.....##.##.### + +....##... +##.####.# +##..##..# +##.####.# +##......# +####..##. +###.##.## +##......# +###....## +...#..#.. +##..##..# +...#..#.. +..######. +###....## +##.####.# +...####.. +..######. + +.#....#.. +.##..##.. +.######.. +#.#..#.## +#...#..## +#.####.## +#..##..## +.##..##.. +.#.##.#.. +.#....#.. +.##..##.. +#.#..#.## +##....### +###..#### +.#....#.. + +.....#.##.# +........#.# +...###..### +#.#.###.#.# +...#.#....# +..#.##.###. +..###....#. +###.##.###. +###.##.###. + +..###.#..#.###... +..###......###... +#....#....#....## +.#..#.####.#..#.. +#.##..#..#..##.## +...#.######.#..## +....##....##..... +...##########.... +##..###..###..### +#.#.#......#.#.## +.#.##########.#.. +#..###....###..## +..#....##....#... + +.#....##.##..##.# +.#.####.#..##..#. +.#.##..#........# +..##.###..#..#..# +..#...###.#..#.## +#..#...##.####.## +#.#...#.###...##. +##.##..###.##.### +...#.##.......... +...#.##.......... +##.##..###.##.### + +.###.##.###.#..## +..#.#..#.#...##.# +....#..#.....#.#. +..##....##..#.#.# +..##....##..#.#.# +....#..#.....#.#. +..#.#..#.#...##.# +.###.##.###.##.## +#....##....#####. +.##......##.#.##. +.##......##..###. +.##.#..#.##.###.# +#####..#####.#### +...######...#.#.# +#..##..##..##.### + +.....#..... +..#.#.##### +###..##.... +##..#.#.##. +..######..# +...#..#.... +#####..#### +##.#....##. +...#..##..# +####.#..##. +##..##..... +...##.##..# +..###..#..# +#####...##. +......##..# +###...##..# +....#.##.## + +..####.#.#....### +##...##..###.##.# +###.##.#..#....#. +###.##.#..#....#. +##...##..###.##.# +..####.#.#..#.### +.......######.#.. +..#.##.....#..... +..#.##.##.#.####. + +##..#.#.###.# +##..#.#.###.# +#.#..###..##. +...#.#...##.# +###.#.##..#.# +.#..####.#.## +.#####.##..#. +.######.##### +#..........#. +#..........#. +.######.##### +.#####.##..#. +.#..####.#.## +###.#.##.##.# +...#.#...##.# + +...##.##.#.#### +.#.#...#..#.##. +.#..#..####.... +#####.#.#.##..# +.....###.#.#..# +......##.#.#..# +#####.#.#.##..# + +#....######..## +#####.....#.#.. +..##..#.#..#.## +..##..##..###.. +..##.....##.### +#....###.###.## +##..##.#.#.##.. + +##..##.###..### +##..##.###..### +......#.####..# +..##.###.#.#### +#....###...#.## +##..####..#.#.. +.####.#.....#.# + +###.####.##.# +........#..#. +..####....... +##....##....# +#.#..#.#.##.# +##.##.###..## +##.##.##....# +#.####.#....# +.#.##.#..##.. + +####....#.#..###. +.##.#..##...#..## +.##.#..##...#..## +####....#.#..###. +#####.#.#.##..... +..#.....#....###. +#..#.#...##.#..## +#..#####.##...#.# +######.#...#.#... + +.#..#.#...#.. +##..##.####.. +..##...#..#.. +.#..#...#..#. +.####....##.. +#....##..#### +.####.#.###.. +......#.##.## +..##..#...#.. +#....#.####.. +..##....##### + +#...#.#..##.# +#...#.#..##.# +###..##...... +#########...# +.#..#.#.##... +#..####...#.. +#..##..#..##. +#....##.....# +#....##.....# +#..##..#..##. +#..####...#.. +.#..#...##... +#########...# +###..##...... +#...#.#..##.# + +#.#...# +...#### +#.#.#.. +####... +#....## +##.##.. +##.##.. + +.##......##.. +#..#.....##.. +....##...##.. +#..#.#...##.. +#..##.#.####. +#..#.#...##.. +.##...#..##.. +#..###.##..## +....##.#....# +....#.....#.. +#..#.##.####. +######...##.. +####..#.#..#. +....#####..## +.##.#.##....# +#..##.#...... +#..#.###....# + +...#....#....#. +#.#......#.#..# +.#...##...#.#.. +#.###..###.#... +#.#......#.##.# +##...##...#..#. +.#..#..#..#.... +#..........#.#. +.#.#.##.#.#.#.. +.#.#.##.#.#.#.. +#..........#.#. + +##.####.##.####.# +..#.##.####.##.#. +.#.####.##.####.# +#..#..#....#..#.. +#..#..#....#..#.. +##.#..#.##.#..#.# +#.##..##..##..##. +####..######..### +###.##.####.##.## +....##......##... +#.##..##..##..##. +#...##......##... +#.##..##..##..##. + +#....#####.##..## +......#..##.##### +########.#.###.## +#.##.###.......## +..##...##..##.... +......####.#.#### +.......#..###.... +####.#.#.###...## +###########..#... +.........#...#.## +......#.#....#... + +.##.##.#. +#.#.##.## +#.......# +#.......# +#.#.##.## +.##.##.#. +.###..### +#..#..##. +.#.####.. +####..... +#....#... +....#...# +.####.### +.#...#... +#.####### +#...#..## +#.#.#..## + +.##.#####.. +..#.#.##... +###.###.#.. +###.##...#. +.#.#...##.. +...#...#.#. +##.###....# +##.##.....# +...#...#.#. +.#.#...##.. +###.##...#. +###.###.#.. +..#.#.##... +.##.#####.. +..#......## +##.#.#####. +##.#.#####. \ No newline at end of file diff --git a/day-13/src/bin/part1.rs b/day-13/src/bin/part1.rs new file mode 100644 index 0000000..fe36893 --- /dev/null +++ b/day-13/src/bin/part1.rs @@ -0,0 +1,21 @@ +use day_13::part1::process; +use miette::Context; + +#[cfg(feature = "dhat-heap")] +#[global_allocator] +static ALLOC: dhat::Alloc = dhat::Alloc; + +#[tracing::instrument] +fn main() -> miette::Result<()> { + #[cfg(feature = "dhat-heap")] + let _profiler = dhat::Profiler::new_heap(); + + #[cfg(not(feature = "dhat-heap"))] + tracing_subscriber::fmt::init(); + + let file = include_str!("../../input.txt"); + let result = process(file).context("process part 1")?; + + println!("{}", result); + Ok(()) +} diff --git a/day-13/src/bin/part2.rs b/day-13/src/bin/part2.rs new file mode 100644 index 0000000..4d7a7a6 --- /dev/null +++ b/day-13/src/bin/part2.rs @@ -0,0 +1,21 @@ +use day_13::part2::process; +use miette::Context; + +#[cfg(feature = "dhat-heap")] +#[global_allocator] +static ALLOC: dhat::Alloc = dhat::Alloc; + +#[tracing::instrument] +fn main() -> miette::Result<()> { + #[cfg(feature = "dhat-heap")] + let _profiler = dhat::Profiler::new_heap(); + + #[cfg(not(feature = "dhat-heap"))] + tracing_subscriber::fmt::init(); + + let file = include_str!("../../input.txt"); + let result = process(file).context("process part 1")?; + + println!("{}", result); + Ok(()) +} diff --git a/day-13/src/error.rs b/day-13/src/error.rs new file mode 100644 index 0000000..e08a17f --- /dev/null +++ b/day-13/src/error.rs @@ -0,0 +1,9 @@ +use miette::Diagnostic; +use thiserror::Error; + +#[derive(Error, Diagnostic, Debug)] +pub enum AocError { + #[error(transparent)] + #[diagnostic(code(aoc::io_error))] + IoError(#[from] std::io::Error), +} diff --git a/day-13/src/lib.rs b/day-13/src/lib.rs new file mode 100644 index 0000000..681e54b --- /dev/null +++ b/day-13/src/lib.rs @@ -0,0 +1,4 @@ +pub mod error; + +pub mod part1; +pub mod part2; diff --git a/day-13/src/part1.rs b/day-13/src/part1.rs new file mode 100644 index 0000000..7030f78 --- /dev/null +++ b/day-13/src/part1.rs @@ -0,0 +1,157 @@ +use crate::error::AocError; +use itertools::Itertools as _; +use nom::character::complete::line_ending; +use nom::{ + bytes::complete::is_a, character::complete::newline, multi::separated_list1, sequence::pair, + IResult, +}; +use tracing::instrument; + +#[instrument(skip(input))] +fn block(input: &str) -> IResult<&str, Vec<&str>> { + separated_list1(line_ending, is_a(".#"))(input) +} + +#[instrument(skip(input))] +fn parse(input: &str) -> IResult<&str, Vec<(Vec, Vec)>> { + let (input, blocks) = separated_list1(pair(newline, newline), block)(input)?; + + let blocks = blocks + .into_iter() + .map(|block| { + let rows: Vec = block.iter().map(|&s| s.to_string()).collect_vec(); + let cols: Vec = (0..block[0].len()) + .map(|i| { + rows.iter() + .map(|row| row.chars().nth(i).unwrap().to_string()) + .collect::() + }) + .collect_vec(); + + (rows, cols) + }) + .collect_vec(); + + Ok((input, blocks)) +} + +#[instrument] +fn partition(input: Vec) -> (Vec, Vec) { + for i in 0..input.len() - 1 { + if input[i] == input[i + 1] { + let a = input[..=i].to_vec(); + let b = input[i + 1..].to_vec(); + if is_mirror(&a, &b) { + return (a, b); + } + } + } + (input, vec![]) +} + +#[instrument] +fn is_mirror(a: &[T], b: &[T]) -> bool { + if a.is_empty() || b.is_empty() { + return false; + } + + let a: Vec = a.iter().rev().cloned().collect(); + let b: Vec = b.to_vec(); + if a.len() >= b.len() { + return a.starts_with(&b); + } else { + return b.starts_with(&a); + } +} + +#[instrument] +fn smaller_len(a: &Vec, b: &Vec) -> usize { + a.len().min(b.len()) +} + +#[tracing::instrument(skip(input))] +pub fn process(input: &str) -> miette::Result { + let (input, parsed) = parse(input).unwrap(); + + debug_assert!(input.is_empty()); + + Ok(parsed + .iter() + .map(|block| { + let rows = block.0.clone(); + let cols = block.1.clone(); + + let (row_lhs, row_rhs) = partition(rows); + let (col_lhs, col_rhs) = partition(cols); + + let mut sum: usize = 0; + if is_mirror(&col_lhs, &col_rhs) { + sum += col_lhs.len(); + } + if is_mirror(&row_lhs, &row_rhs) { + sum += 100 * row_lhs.len(); + } + + sum + }) + .sum::() as u64) +} + +#[cfg(test)] +mod tests { + use super::*; + + use rstest::rstest; + + #[test_log::test] + fn tets_partition() { + let a = vec!['a', 'b', 'c', 'd', 'd', 'c', 'b', 'a']; + assert_eq!( + partition(a), + (vec!['a', 'b', 'c', 'd'], vec!['d', 'c', 'b', 'a']) + ); + } + + #[test_log::test] + fn tets_partition_double_col() { + let a = vec!['a', 'b', 'b', 'c', 'd', 'd', 'c', 'b', 'b']; + assert_eq!( + partition(a), + (vec!['a', 'b', 'b', 'c', 'd'], vec!['d', 'c', 'b', 'b']) + ); + } + + #[test_log::test] + fn test_is_mirror() { + let a = vec!['a', 'b', 'c', 'd']; + let b = vec!['d', 'c', 'b']; + assert!(is_mirror(&a, &b)); + } + + #[test_log::test] + fn test_is_mirror_empty() { + let a = vec!['a', 'b', 'c', 'd']; + let b = vec![]; + assert!(!is_mirror(&a, &b)); + } + + #[test_log::test] + fn test_smaller_len() { + let a = vec!['a', 'b', 'c', 'd']; + let b = vec!['d', 'c', 'b']; + assert_eq!(smaller_len(&a, &b), 3); + assert_eq!(smaller_len(&b, &a), 3); + } + + #[test_log::test(rstest)] + #[case("test-input.txt", 405)] + #[case("test-input2.txt", 408)] + fn test_process(#[case] filename: &str, #[case] output: u64) -> miette::Result<()> { + let input = + String::from_utf8_lossy(&std::fs::read(std::path::Path::new(filename)).unwrap()) + .parse::() + .unwrap(); + assert_eq!(output, process(&input)?); + Ok(()) + } +} diff --git a/day-13/src/part2.rs b/day-13/src/part2.rs new file mode 100644 index 0000000..1ff3976 --- /dev/null +++ b/day-13/src/part2.rs @@ -0,0 +1,18 @@ +use crate::error::AocError; + +#[tracing::instrument] +pub fn process(input: &str) -> miette::Result { + Ok(0) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test_log::test] + fn test_process() -> miette::Result<()> { + let input = include_str!("../test-input.txt"); + assert_eq!(0, process(input)?); + Ok(()) + } +} diff --git a/day-13/test-input.txt b/day-13/test-input.txt new file mode 100644 index 0000000..f226414 --- /dev/null +++ b/day-13/test-input.txt @@ -0,0 +1,15 @@ +#.##..##. +..#.##.#. +##......# +##......# +..#.##.#. +..##..##. +#.#.##.#. + +#...##..# +#....#..# +..##..### +#####.##. +#####.##. +..##..### +#....#..# \ No newline at end of file diff --git a/day-13/test-input2.txt b/day-13/test-input2.txt new file mode 100644 index 0000000..8dab2be --- /dev/null +++ b/day-13/test-input2.txt @@ -0,0 +1,31 @@ +#.#.##.##.# +#..#..####. +#.#####..## +#.##..####. +##.#..####. +..#...#..#. +##...#....# +....##....# +##.##.#..#. +#..#.##..## +##.###.##.# +...#.#.##.# +.#.#.#.##.# +##.###.##.# +#..#.##..## + +##.#...#.#. +##.##..#.#. +.#.##...... +..######..# +..######..# +.#.##...... +##.##..#.#. +##.#...#.#. +...##...#.. +...#.###..# +#..#######. +..#.##.###. +..###.##.#. +..##.##.##. +..#.#..##.. \ No newline at end of file