From eb168b1532e13fc5d525dccc34e780bdda94f99b Mon Sep 17 00:00:00 2001 From: ThetaDev Date: Fri, 2 Dec 2022 20:20:28 +0100 Subject: [PATCH 01/10] day 2 --- Cargo.lock | 81 ++ Cargo.toml | 5 +- input/day2.txt | 2500 ++++++++++++++++++++++++++++++++++++++++++++++++ src/day2.rs | 134 +++ src/main.rs | 4 +- 5 files changed, 2721 insertions(+), 3 deletions(-) create mode 100644 input/day2.txt create mode 100644 src/day2.rs diff --git a/Cargo.lock b/Cargo.lock index e5329e4..49e94b6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7,10 +7,91 @@ name = "adventofcode22" version = "0.1.0" dependencies = [ "path_macro", + "rstest", ] +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + [[package]] name = "path_macro" version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a6e819bbd49d5939f682638fa54826bf1650abddcd65d000923de8ad63cc7d15" + +[[package]] +name = "proc-macro2" +version = "1.0.47" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5ea3d908b0e36316caf9e9e2c4625cdde190a7e6f440d794667ed17a1855e725" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbe448f377a7d6961e30f5955f9b8d106c3f5e449d493ee1b125c1d43c2b5179" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "rstest" +version = "0.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b07f2d176c472198ec1e6551dc7da28f1c089652f66a7b722676c2238ebc0edf" +dependencies = [ + "rstest_macros", + "rustc_version", +] + +[[package]] +name = "rstest_macros" +version = "0.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7229b505ae0706e64f37ffc54a9c163e11022a6636d58fe1f3f52018257ff9f7" +dependencies = [ + "cfg-if", + "proc-macro2", + "quote", + "rustc_version", + "syn", + "unicode-ident", +] + +[[package]] +name = "rustc_version" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bfa0f585226d2e68097d4f95d113b15b83a82e819ab25717ec0590d9584ef366" +dependencies = [ + "semver", +] + +[[package]] +name = "semver" +version = "1.0.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e25dfac463d778e353db5be2449d1cce89bd6fd23c9f1ea21310ce6e5a1b29c4" + +[[package]] +name = "syn" +version = "1.0.105" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "60b9b43d45702de4c839cb9b51d9f529c5dd26a4aff255b42b1ebc03e88ee908" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "unicode-ident" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ceab39d59e4c9499d4e5a8ee0e2735b891bb7308ac83dfb4e80cad195c9f6f3" diff --git a/Cargo.toml b/Cargo.toml index 2a33956..aeb858b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,7 +3,8 @@ name = "adventofcode22" version = "0.1.0" edition = "2021" -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - [dependencies] path_macro = "1.0.0" + +[dev-dependencies] +rstest = { version = "0.16.0", default-features = false } diff --git a/input/day2.txt b/input/day2.txt new file mode 100644 index 0000000..4e0cd35 --- /dev/null +++ b/input/day2.txt @@ -0,0 +1,2500 @@ +C Y +C Y +B Y +A Z +B Z +A X +A Y +A Y +A X +A Y +B Y +A Y +B Y +B Y +B Z +B Z +B Z +B Z +A Y +B Z +A Y +B X +B Y +B X +A X +A X +B Z +A X +A X +B Z +B Z +B Y +B Z +B Z +B Z +B Y +A X +A X +B Z +A X +B X +B X +C Y +B Z +C X +A X +A Y +B Y +A Y +B X +A X +B Y +B Z +B Z +B Y +B Z +C Z +B X +B X +B Z +B Z +B Z +B Z +A Y +B X +A X +C X +B Y +B Z +A Y +B Z +B Z +B Y +B Y +B Z +B Y +B Z +A X +B X +B X +A X +A X +B Z +B Z +B Z +B Z +B Y +B X +B Z +A X +A Y +B Z +A Y +B Z +B Y +B Z +A X +B Y +A Y +B Z +A Z +A Y +A Y +C Y +B Z +B X +A Z +B Z +A X +B Z +A Z +B Z +A X +B Y +A X +B Y +B Z +B X +B X +B Z +B Z +B Z +A X +B Z +A X +B X +B Z +A X +C Z +B Z +B Z +B Y +B Y +B Y +B Z +B Z +A Y +B Z +B Z +C Y +C Z +A X +B Z +B X +B Z +B Z +B Y +A X +B Z +B Y +A Z +B Z +A X +A X +B Y +A Y +B Z +B Z +B X +B Y +A X +A Y +B X +C Z +A Y +B Z +B Z +B Z +A Z +C Y +B Y +B Y +B Z +C Y +B Y +B X +B X +B Z +C Z +A X +B Z +B Z +B Z +B Z +B X +B X +A X +A Z +A Z +A X +C Y +B X +A X +A Y +A X +B X +A Y +B Z +B Z +A Y +A X +B Y +B Z +B Z +A X +A Z +B Z +B X +A X +B Z +B Y +A Y +A Z +B X +A Y +B Z +A Z +B Z +A Y +B Z +B X +B Y +A Y +B Z +B Z +A X +A X +B Y +B Z +A X +B Z +B Z +B Z +B Z +B Z +A Z +B Z +B Z +B X +A Y +C X +B Z +B Y +B Y +B Z +B Z +B Z +B Y +B X +B Y +C X +B Z +A Z +A Y +C X +A X +B X +A X +B X +A Y +B Z +A Y +A Y +B Z +B X +B Z +A Y +B Z +B X +C Z +C X +C Z +B Y +B X +B Z +B Z +B Y +B Z +B Y +B Z +A X +B X +B Z +A X +B Z +B Y +B Z +A X +B Y +C Y +A Z +B Z +C Z +A Y +B Z +A Y +A Z +B Y +A X +A X +B Z +B Z +B Z +A Y +B Z +A Z +B Z +A Y +A Z +B Y +C Z +B Z +A Z +B X +B Z +B Y +B Z +A Z +A Z +B Z +B X +C Z +B X +B Z +B Y +A X +B Z +A X +B X +B Z +B Z +A X +B X +C X +C X +B X +B Y +B Z +B X +B Y +B Y +B Z +A Z +B Z +C X +A Y +C X +B Z +A Y +B Z +B Y +B Z +B X +A X +B X +B Z +A Y +A Y +B Z +B Z +B Z +A Y +B Z +B X +C X +B Z +B Z +C Y +B Z +C Y +B Z +C Y +C X +B Z +C Y +A Y +A Y +C Z +B Z +B X +B Z +B X +C Z +B Z +A Z +B Z +C Y +B Z +A X +A Y +B Y +B Y +B Z +A Y +B X +B Z +B Z +A Z +B X +A Y +A Y +B Z +B Z +B Z +C X +A Z +B X +C Z +B Y +B Z +B X +A Y +B Z +A X +B X +B Y +A Y +B Z +B Z +B Y +A Z +B Z +A X +B Y +A Y +A X +A Y +A X +A X +B X +B Y +B X +B Y +B Z +B Y +B X +A X +B Z +A Z +A X +B Z +B Y +A X +B Y +A X +B X +C Y +B X +B X +B X +C Y +B X +B Y +B Y +B Y +B Y +B X +A Y +C Y +B Z +B Z +B X +B Z +C Y +B Y +B Z +B Z +B Y +B Z +A X +B X +B Y +A X +C Y +B Z +A Y +B Z +B Z +B Z +A X +B Y +B Z +B Y +B Z +A X +B Z +A X +B Y +B Y +B X +B X +A Y +B Z +C Y +A X +A X +B X +B Z +B X +B X +B Z +B Z +A X +B X +B Z +A Z +B Y +A X +B Z +C Y +B Z +B X +B Z +B Z +A Y +A Y +B Z +B X +B Y +B Z +A Y +A Y +B X +B X +C X +B Z +C Z +B X +A Y +B Z +A Z +B Y +A Y +B Z +B X +B X +A Y +B Z +B Y +A X +B Y +B Y +B Y +B X +B Y +B Z +B Y +B Y +A X +C Z +B Z +B Z +A Z +B Z +B Z +A X +B X +A Y +A X +B X +C X +B X +B Z +B Y +A Z +A Y +B Y +B Z +B X +B X +B Z +B Z +B Y +A X +B Z +A Y +A X +B Y +B Z +B Z +B X +A X +A Y +C X +A Y +B Z +B Y +B Z +B Z +C Z +B Z +B X +A X +C Y +B X +B Z +A Z +A X +A Y +B Z +B Z +A Y +A Y +B X +A Y +A Y +B Z +A Y +B Y +B Z +A Y +A Z +B X +B X +B Y +B Z +B Z +A X +B X +C X +B Y +A Y +A X +A X +A X +B Y +A X +A Z +A Y +B X +A Y +B Z +C X +B X +B X +B Z +B X +B X +B Z +A X +B Z +B X +B Z +A Y +B Y +B Z +B Y +B Z +B Z +A X +B Z +A Y +C Z +A Y +B Z +A Y +B X +B Z +B X +C Y +A X +B Z +B Y +A X +A X +B X +B Z +A Z +B Z +B X +B X +B X +B Z +B Z +B Y +B X +B X +A X +B Z +B X +A X +A X +A X +B X +B Z +A Z +B X +B Y +B Y +B Z +B Z +C Z +A Y +A X +B Y +B X +B Z +B X +A X +B Z +B Z +B Y +B Z +B X +B Z +B Z +B X +B X +A X +A X +B X +B Z +B Z +C X +C X +B Z +B Z +B X +B Z +B Y +B X +A X +B Y +A Y +A X +B Z +B Z +C X +B Y +B Z +B Z +B X +B Z +B Z +C Z +B Z +B Y +B Z +B X +A X +B X +B Z +B Z +A Y +B Z +B Z +B Z +B Z +A X +B Z +B Z +B X +B Y +B Y +B Z +A X +B Z +B Y +B X +A X +B X +B Z +B Z +B Z +B X +B Z +B Z +B Z +A Y +B Z +C X +B Y +B Y +B Y +A Y +B Z +B Z +A X +C Z +B Z +B Z +B Z +B X +B Z +A Z +B Z +B Z +B Z +A Z +B Z +C Y +B X +A X +A Y +B Z +B Z +A Z +B Z +B X +A Z +B Y +B Z +B X +B Z +B X +B Y +B Z +A X +B Z +B Z +A Y +B X +B X +B X +A Z +C X +A Z +B Z +B Z +B Z +B X +A Y +C X +A Z +A Y +B X +B Z +B X +B Z +B Y +A Y +B X +C X +A Y +C Z +A X +B Z +B Z +A Z +B X +B Y +B Z +A Y +B Y +A X +A X +C Y +A Y +B X +A X +B Y +B X +B Y +A Y +A X +C Y +B Y +B Y +B Z +B Y +B X +B Z +B Z +B X +A X +B Z +B Z +B Z +B X +B Z +B X +B Z +B Z +B Y +B Y +B X +A X +B Z +B Y +A Y +B Z +B X +B Z +B Z +A Z +B Z +A Y +B Z +A X +B Z +B Z +A Y +B Z +A X +B Z +A Y +A Y +A Z +B X +B Z +B Y +A Z +C Z +B Z +A X +A X +B X +A Z +B X +B X +B Z +C X +B Z +B Z +B Z +B Z +B X +A Z +A Y +B Z +B Y +C Z +B Y +B Z +A Y +B X +B X +B Z +A X +A Y +B Z +B X +B Y +A Y +C Y +C Y +B Z +A Y +B Y +A Y +B Z +B Z +A X +B X +A X +B X +A Y +A X +B Z +A X +B Z +B X +B Z +B X +B X +A X +A Y +B Z +B X +B X +A X +A Y +A X +B Y +B Z +B Z +B Z +B Z +B Z +B X +B Y +A Y +B X +B Z +A Y +B X +A X +B Z +C X +B Y +A Y +A X +A X +B X +B X +B Z +B Z +B Z +B X +B Y +B X +B Z +B X +B Y +B X +B Z +B X +B Y +B X +B Z +B Z +B Z +A X +C X +C Y +A Y +B X +B X +A Y +B Z +B X +B Z +B X +B X +C X +B Z +B Z +B Y +A Z +A Z +C X +B X +C Y +B Z +C X +B Z +A Y +C Z +B X +B Y +A Y +B Y +B X +B Z +A Y +A Z +C X +B Z +A X +B X +B Z +C Z +A Y +B Z +B Z +A X +A X +B Z +B Y +C Z +B Z +B X +B Z +A X +A X +B Y +A X +B X +A Y +B Z +A Y +B Z +A Y +B Z +A X +B Y +B Z +B Z +B Z +A X +A X +B X +B Z +A X +B Z +A X +A X +B Z +B Z +B Z +B X +B Y +B Z +B Z +B X +B Z +B Y +B Z +C Y +B X +C Z +B Z +B Z +A Y +B Z +B X +A Y +B Z +B X +B Z +B Z +B Y +B Z +A Z +A Y +B Z +B Z +B Y +A Y +B Z +A X +B X +A Z +A X +B X +B Z +B X +B Z +B Z +B Y +B Y +B Z +B Y +B Z +B Z +A X +B X +B Y +B Y +C Z +A X +B Y +A Y +B Z +B Z +B Z +C X +B X +A Z +B Y +A X +C X +B Z +B Z +B Z +B Y +A Y +A Y +B Y +B Z +B Z +B Z +C X +A X +B Z +A Y +B X +B Z +B Z +B Z +B Z +B X +B Y +B Z +B Z +A Y +C X +A X +B Z +B Z +A Y +A Y +B Z +A Y +B Y +A X +B Z +B Z +A Y +B Z +B Z +A X +A X +B X +B Z +B Z +A X +B Z +B Z +C Y +B Z +A X +B Z +A X +B Z +A X +B X +A X +A Y +B Z +B X +A X +B Z +A X +A Z +B Z +B Z +B Z +A Y +B X +A X +B Y +A Y +B Z +B Z +B X +B Z +B X +B X +A X +B Z +A Y +A X +B X +A Z +B X +B Y +B Z +B X +B X +B Z +C X +A X +B Z +B Y +C Z +B Z +A Z +A Z +A X +A Y +B Z +B Z +B X +A Z +B Z +B Z +B X +B Y +B X +B Z +B X +B Z +A Y +A X +B Z +B X +B Y +B Z +B Z +B Z +B Z +C X +C X +B Z +B X +B Z +B Z +B Z +B X +B Z +B Z +B Z +B X +B Z +B Z +B Z +C Y +B Z +B Y +B Z +A Z +A Y +B Z +A Y +C X +A X +B X +A Y +B Z +A Y +B Z +B Y +B Z +B Z +C Y +B Y +B Z +B Z +B X +B Y +B Z +B Y +B Z +B X +B Y +B Z +B Z +B Z +B Y +B Y +B Z +B Z +C X +B Z +A Y +B Z +B Z +B Y +B Y +B Z +B Z +A Y +B Z +A Z +C X +A Y +A Y +A X +B Z +A X +C Y +A Z +C Y +C X +B Z +A X +A Y +B Z +B Z +B Z +B Z +B Y +A Z +A Y +B Y +A Y +A Z +B X +B Z +B Z +A X +C Y +B Z +B X +C X +A Z +B Z +B X +B Y +A X +A X +B Z +C X +B Z +B Z +B X +B Z +B Z +B Z +A X +B Z +B Y +B Z +B Z +B Y +B X +A X +B Z +A X +A X +B Y +B Z +B X +A Y +C X +B Y +A X +A X +A X +C Y +B X +C Z +A X +B Z +A Z +B Z +A X +A Y +B Z +A X +B X +A Y +A X +A Y +B X +B Y +B Z +B Y +C X +C Y +B Z +B Y +B Z +A X +C Z +B Z +A X +B X +A X +B X +B Z +B X +B Z +B Z +B Z +A X +A X +B Z +B X +B Z +B Z +B Z +A X +B Z +B X +A X +C Z +A Y +B Z +A Y +B Z +B Z +B Z +C Z +B Z +B Z +A X +B X +A X +B X +A X +B Z +B Z +B X +B Z +A X +A Y +A Y +A Y +B Z +B Z +B Z +B Z +A X +B Z +B Z +B Y +B Z +A Z +B Z +A Y +B Y +B Y +C Y +B X +B Z +B Y +B Z +B Y +B Z +B Z +A Y +B X +A Z +B Z +A X +B Y +A X +B Z +B Y +A X +B X +B Z +B Z +B Z +A Y +A Y +A X +A Y +A Y +B X +B Z +B Y +B Y +B X +B Y +B X +B X +B X +B Z +B Z +B Z +A Y +A Z +B Y +C X +B X +B Z +C Y +B Z +C Y +B Z +B Z +B Z +B X +A X +B Z +B Z +A X +B Z +B Z +B Z +B Z +B Z +B Z +B X +A X +A Y +B Y +B Z +B Y +B X +A Z +A X +B X +B Y +B Z +B Z +B Z +B X +C X +B X +A Z +A X +A Y +B Z +B Z +A X +B Z +A Y +B X +B Z +B Y +B Y +A X +B X +B Z +B Z +B Z +B Z +B Y +A Z +A Y +B X +A X +B Y +B Z +C Y +B Z +B Z +B X +A Z +A Y +B Y +B Z +A Y +B Y +A Y +A X +A Y +B Y +C X +C Z +B X +A Z +A X +B Y +B X +A X +B Z +A Y +A Z +B Z +B Z +B Z +A X +B Z +B Y +B Z +A X +A Y +B Z +B X +C X +B Z +A Y +B Z +B Z +B X +B Z +B Z +A Y +A X +B Z +C Y +A Y +B Y +B Z +A X +B Z +B Z +B Z +B Z +C X +B Y +B Z +B X +B Z +B Z +B Z +B Z +B Z +B Y +A X +B Y +A Y +A X +A X +B Z +B Z +B Z +C X +B X +B Z +A X +A X +B Y +A X +B Z +B Z +B X +B Y +B Y +B Y +A Y +A Y +A X +A X +B Z +B Y +B Y +B X +B Y +B Z +A Z +B Z +A Z +B X +B Y +C Y +B X +B X +A X +A X +A X +A X +B Z +B Z +A Z +B X +B X +B X +B X +B X +B Z +B Z +B Z +B Z +B Z +B Y +B Z +B Z +A Y +A Z +C Y +B Y +A X +B Z +B X +A Y +B Y +A Y +C Z +A X +A Y +B X +B X +C Z +B Y +A Y +A Y +A X +B X +A X +B X +A Y +B Z +B Z +A Z +B X +B Z +B Z +B Y +A Y +B Z +B Y +B Z +B Z +B Z +C X +B Y +A Y +B Z +A Y +A Y +A X +B X +B Y +C Y +C X +B Z +B Z +B Z +A Y +A Z +B Y +B X +B X +B Y +B Z +B Z +A Y +B Y +B Z +B Z +B Z +A X +C Y +B Z +A Z +C Z +B X +B Y +B Z +A Z +A X +B X +A X +C X +B Z +A X +B Y +C Y +B Y +B Z +B X +A Y +B Z +B Z +B Z +A X +B Z +B Z +B Z +B Z +A X +B Z +B X +B X +B Z +C X +A Y +B Z +B X +B Z +B Y +B Z +B Y +C Y +A X +B Y +B Z +B Z +B X +B Z +A X +B Z +A X +B Y +B Z +A X +B Z +B X +A Z +B Z +C X +B Z +B Y +B Y +B Y +B Z +B X +B X +A X +A X +B Z +B Z +A X +A X +A Z +A Y +C Z +B Z +B Y +C X +B X +B Y +B Y +A Y +B Z +B Z +B Y +C Y +B Z +A Y +B Z +B Z +B Z +B Z +A Y +B Z +B Z +B Z +A X +B X +A Y +B Y +B Z +B Z +B Z +A Y +B Z +B Z +B Z +B Y +B Y +B X +B X +B Y +C Z +B X +B Y +C Y +A Y +B Z +A Z +B Z +A Y +B X +B Z +B X +C Z +A Y +B X +B Z +A X +A X +B Z +B Z +B Y +B Z +C Y +C X +B Z +B Z +A X +B X +A Y +B Y +B Y +C Z +A X +B X +B Z +A Y +B Z +C X +B Z +A Z +C X +A Y +A Z +B Z +A Y +A X +B Z +B Y +B Z +B X +B Z +A X +B Z +A Z +B Z +B Z +B Y +A X +B Z +B X +B Y +B Y +A Y +A X +C Z +B Y +C Z +A X +B X +B Z +B X +C X +B Z +B Z +A X +A Y +A Z +A Z +B Z +B Z +A X +B Z +B Y +A X +A Y +B Z +B X +A Y +B Z +B Y +B X +B Z +A Z +B Y +A Y +A X +B Z +B Z +B Z +B Z +A X +B Y +B Z +B Z +B Z +A Z +B Y +A X +C Y +B Z +B Z +B Y +B X +B Z +A Y +A X +B Z +B Y +B Z +A Y +C X +B Y +B Z +B Z +A Y +B X +B Z +B Z +A X +B X +B Z +A Z +B Y +B Z +B Z +B Z +B Z +B Z +A Z +B X +A Y +C Y +B Z +A Y +B Z +B Z +A Y +B Z +B Z +A Y +A Y +B X +B Y +A X +B Z +A Y +B Z +A Z +B Z +B X +B Z +B Z +B Y +B Y +B Z +A X +B Y +B Z +B Z +B Z +B Y +A Y +B Z +B Y +B Z +B Z +B X +A X +B Y +A X +A X +B Z +A X +B Z +C X +B Y +B Z +B Z +B Y +A X +B Z +B Z +B Y +B X +B Z +B X +B Z +C Y +B Y +B Z +B Z +A Z +B Z +B Z +A Y +B Z +B Y +A X +B Z +B Z +B Z +B X +B Z +B Y +B Z +B X +C Z +B Y +B Z +B Z +B Y +A X +A X +B Y +A Y +B Z +B X +B Y +B Z +B Y +B Z +A X +C X +B Z +A Z +C Y +A X +B Y +B Y +A X +A X +B Y +B Z +C Y +B Z +B Y +A X +B Y +B Y +B Z +B X +B Z +C X +C Y +B X +A Y +B Z +B Z +B Z +A X +B Z +B Z +B X +B X +B X +B X +B Y +A Y +B Z +B Z +B Z +A Z +B X +B X +B X +B X +B Z +A Y +B Y +B Z +A X +A X +A X +B Y +B Z +B X +B Z +A Z +B Z +B Z +A Y +C Z +A X +B Z +C X +B Z +B Z +B Z +B Y +B Y +B Z +C X +B Z +A Y +B X +C X +A Y +C X +B Z +B X +A Z +B Z +C Y +B Y +B X +B Z +B Y +A Y +B X +A Z +B Z +B Z +C Z +B Z +B Y +B Z +A Y +C Y +B Z +B X +A Y +B Z +B Z +B Y +B Y +B Z +A X +A Y +A X +A Z +B Z +B Z +B Y +A Y +B Y +B Z +B Y +A Y +B X +A X +B Y +B Y +B X +B Z +B Y +C X +B Z +B Z +B Z +A X +B Z +B X +A Y +A Y +B Z +A Y +A Y +A Y +B Y +B Z +A X +C X +B Y +A X +C X +B Z +B Y +A Y +B X +B Z +B Y +B Y +B Z +B Z +A Y +B X +B X +A X +B Z +B X +B Z +B Z +C Z +C Y +A X +B X +C X +B X +A Z +A X +B Y +C X +B Y +A X +A X +A X +B X +B Z +B X +B Y +B Z +B Z +B Z +B Z +A X +A X +B X +B Z +A Z +A X +B Z +C Y +B Z +A X +B Z +A Z +B Z +B Z +A Y +B Y +B Z +B X +B Z +B Z +C Y +B Z +B X +A X +B Y +B Z +A Y +A X +B Z +B Y +B Z +B X +A Y +C Z +B X +B Z +A X +B X +B Z +B Z +A X +B Y +B X +B Z +B Z +B Y +B X +B X +C X +B Z +C X +B Z +C Y +A Y +C X +B X +B Y +B Z +B Y +B Z +A X +B Z +B Z +B Y +B Y +A X +A X +A Y +B Z +B Z +A Y +B Z +A X +B Z +B X +B X +B X +A Y +B Z +B Z +A Y +B Z +A Z +B Y +B X +A Z +A Z +B Z +A Y +B Z +B Y +B Y +B X +A Y +B X +B Z +B Z +B Y +B X +B Z +B Z +B Z +B Z +A X +B X +B X +B Z +A X +B X +B Z +B Z +B Y +A Y +B Z +B Y +B Y +A X +B X +B Y +C X +C X +B Y +A Y +A Z +B X +B Z +B Z +B Z +B Z +A X +A Y +A Y +B Y +C X +B Z +A Y +B X +C Z +B X +B Y +A Y +B X +B Y +B Z +B Y +B X +B Y +A Z +B Z +A X +B X +B X +B Z +B Y +B Y +B Y +A X +B X +B Y +B Z +A Y +A X +B X +B Z +B Z +C X +A Z +B Z +A X +A Y +C X +B Z +B Z +A X +B X +A Y +A Y +B Z +B Z +B X +A Z +B Z +B Y +A Y +B Z +B Y +A Y +B Y +B Z +A X +B Y +A X +A Y +A X +A X +B Y +A Z +B Z +A Y +B Z +A Z +A Y +B Z +B Y +B Z +B X diff --git a/src/day2.rs b/src/day2.rs new file mode 100644 index 0000000..71fcaf5 --- /dev/null +++ b/src/day2.rs @@ -0,0 +1,134 @@ +//! Day 2: Rock Paper Scissors + +use std::{ + cmp::Ordering, + fs::File, + io::{BufRead, BufReader, Lines}, +}; + +use path_macro::path; + +fn read_input() -> std::iter::Flatten>> { + let f = File::open(path!("input" / "day2.txt")).unwrap(); + BufReader::new(f).lines().flatten() +} + +fn read_rounds() -> Vec<(Sign, Sign)> { + read_input() + .filter_map(|line| { + line.split_once(' ') + .map(|(a, b)| (Sign::from_str(b), Sign::from_str(a))) + }) + .collect() +} + +fn read_r2() -> Vec<(Sign, Ordering)> { + read_input() + .filter_map(|line| { + line.split_once(' ').map(|(a, b)| { + ( + Sign::from_str(a), + match b { + "X" => Ordering::Less, + "Y" => Ordering::Equal, + "Z" => Ordering::Greater, + _ => panic!("could not parse sign `{}`", b), + }, + ) + }) + }) + .collect() +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +enum Sign { + Rock = 1, + Paper = 2, + Scissors = 3, +} + +impl Sign { + fn from_str(s: &str) -> Self { + match s { + "A" | "X" => Self::Rock, + "B" | "Y" => Self::Paper, + "C" | "Z" => Self::Scissors, + _ => panic!("could not parse sign: `{}`", s), + } + } +} + +fn round_result(you: Sign, opp: Sign) -> Ordering { + if you == opp { + Ordering::Equal + } else { + let won = match you { + Sign::Rock => opp == Sign::Scissors, + Sign::Paper => opp == Sign::Rock, + Sign::Scissors => opp == Sign::Paper, + }; + if won { + Ordering::Greater + } else { + Ordering::Less + } + } +} + +fn round_score(you: Sign, opp: Sign) -> i64 { + let score_shape = you as i64; + let score_result = match round_result(you, opp) { + Ordering::Less => 0, + Ordering::Equal => 3, + Ordering::Greater => 6, + }; + score_shape + score_result +} + +fn your_sign(opp: Sign, result: Ordering) -> Sign { + match result { + Ordering::Less => match opp { + Sign::Rock => Sign::Scissors, + Sign::Paper => Sign::Rock, + Sign::Scissors => Sign::Paper, + }, + Ordering::Equal => opp, + Ordering::Greater => match opp { + Sign::Rock => Sign::Paper, + Sign::Paper => Sign::Scissors, + Sign::Scissors => Sign::Rock, + }, + } +} + +pub fn part1() -> i64 { + let rounds = read_rounds(); + rounds + .into_iter() + .map(|(you, opp)| round_score(you, opp)) + .sum() +} + +pub fn part2() -> i64 { + let rounds = read_r2(); + rounds + .into_iter() + .map(|(opp, result)| round_score(your_sign(opp, result), opp)) + .sum() +} + +#[cfg(test)] +mod tests { + use rstest::rstest; + + use super::*; + + #[rstest] + #[case(Sign::Paper, Sign::Rock, 8)] + #[case(Sign::Rock, Sign::Paper, 1)] + #[case(Sign::Scissors, Sign::Scissors, 6)] + fn t_round_score(#[case] you: Sign, #[case] opp: Sign, #[case] expect: i64) { + let score = round_score(you, opp); + assert_eq!(score, expect); + } +} diff --git a/src/main.rs b/src/main.rs index 2b6dff1..ffc8138 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,6 @@ mod day0; mod day1; +mod day2; macro_rules! days { ( $($n:expr, $module:ident),* ) => { @@ -19,7 +20,8 @@ macro_rules! days { days! { 0, day0, - 1, day1 + 1, day1, + 2, day2 } fn main() { From 346c556ee955b467cbef86ddf124ebe61ae907a2 Mon Sep 17 00:00:00 2001 From: ThetaDev Date: Sat, 3 Dec 2022 13:21:58 +0100 Subject: [PATCH 02/10] refactor: use shared read_input funtion --- input/day0.txt | 2 ++ src/day0.rs | 23 +++++++++++++++++++---- src/day1.rs | 39 ++++++++++++++++++++------------------- src/day2.rs | 41 +++++++++++++++++++++-------------------- src/main.rs | 25 +++++++++++++++++++++++-- 5 files changed, 85 insertions(+), 45 deletions(-) create mode 100644 input/day0.txt diff --git a/input/day0.txt b/input/day0.txt new file mode 100644 index 0000000..ce8c77d --- /dev/null +++ b/input/day0.txt @@ -0,0 +1,2 @@ +123 +456 diff --git a/src/day0.rs b/src/day0.rs index ab5e325..04178fd 100644 --- a/src/day0.rs +++ b/src/day0.rs @@ -1,9 +1,24 @@ //! Dummy challenge for testing -pub fn part1() -> i64 { - 123 +pub fn part1(input: Vec) -> i64 { + input[0].parse().unwrap() } -pub fn part2() -> i64 { - 456 +pub fn part2(input: Vec) -> i64 { + input[1].parse().unwrap() +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn t_part1() { + assert_eq!(part1(crate::read_input(0)), 123); + } + + #[test] + fn t_part2() { + assert_eq!(part2(crate::read_input(0)), 456); + } } diff --git a/src/day1.rs b/src/day1.rs index c5a3824..15dbd58 100644 --- a/src/day1.rs +++ b/src/day1.rs @@ -1,20 +1,6 @@ //! Day 1: Calorie counting -use std::{ - fs::File, - io::{BufRead, BufReader, Lines}, -}; - -use path_macro::path; - -fn read_input() -> std::iter::Flatten>> { - let f = File::open(path!("input" / "day1.txt")).unwrap(); - BufReader::new(f).lines().flatten() -} - -fn get_elves() -> Vec { - let input = read_input(); - +fn get_elves(input: Vec) -> Vec { let mut elves = Vec::new(); let mut buf = 0; @@ -31,13 +17,13 @@ fn get_elves() -> Vec { elves } -pub fn part1() -> i64 { - let elves = get_elves(); +pub fn part1(input: Vec) -> i64 { + let elves = get_elves(input); *elves.iter().max().unwrap() as i64 } -pub fn part2() -> i64 { - let mut elves = get_elves(); +pub fn part2(input: Vec) -> i64 { + let mut elves = get_elves(input); elves.sort(); let mut acc = 0; @@ -46,3 +32,18 @@ pub fn part2() -> i64 { } acc as i64 } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn t_part1() { + assert_eq!(part1(crate::read_input(1)), 69177); + } + + #[test] + fn t_part2() { + assert_eq!(part2(crate::read_input(1)), 207456); + } +} diff --git a/src/day2.rs b/src/day2.rs index 71fcaf5..b73642d 100644 --- a/src/day2.rs +++ b/src/day2.rs @@ -1,20 +1,10 @@ //! Day 2: Rock Paper Scissors -use std::{ - cmp::Ordering, - fs::File, - io::{BufRead, BufReader, Lines}, -}; +use std::cmp::Ordering; -use path_macro::path; - -fn read_input() -> std::iter::Flatten>> { - let f = File::open(path!("input" / "day2.txt")).unwrap(); - BufReader::new(f).lines().flatten() -} - -fn read_rounds() -> Vec<(Sign, Sign)> { - read_input() +fn get_rounds(input: Vec) -> Vec<(Sign, Sign)> { + input + .into_iter() .filter_map(|line| { line.split_once(' ') .map(|(a, b)| (Sign::from_str(b), Sign::from_str(a))) @@ -22,8 +12,9 @@ fn read_rounds() -> Vec<(Sign, Sign)> { .collect() } -fn read_r2() -> Vec<(Sign, Ordering)> { - read_input() +fn get_rounds2(input: Vec) -> Vec<(Sign, Ordering)> { + input + .into_iter() .filter_map(|line| { line.split_once(' ').map(|(a, b)| { ( @@ -101,16 +92,16 @@ fn your_sign(opp: Sign, result: Ordering) -> Sign { } } -pub fn part1() -> i64 { - let rounds = read_rounds(); +pub fn part1(input: Vec) -> i64 { + let rounds = get_rounds(input); rounds .into_iter() .map(|(you, opp)| round_score(you, opp)) .sum() } -pub fn part2() -> i64 { - let rounds = read_r2(); +pub fn part2(input: Vec) -> i64 { + let rounds = get_rounds2(input); rounds .into_iter() .map(|(opp, result)| round_score(your_sign(opp, result), opp)) @@ -131,4 +122,14 @@ mod tests { let score = round_score(you, opp); assert_eq!(score, expect); } + + #[test] + fn t_part1() { + assert_eq!(part1(crate::read_input(2)), 14827); + } + + #[test] + fn t_part2() { + assert_eq!(part2(crate::read_input(2)), 13889); + } } diff --git a/src/main.rs b/src/main.rs index ffc8138..6905631 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,15 +1,36 @@ +use std::{ + fs::File, + io::{BufRead, BufReader}, +}; + +use path_macro::path; + mod day0; mod day1; mod day2; +pub(crate) fn read_input(day: u8) -> Vec { + let f = + File::open(path!("input" / format!("day{}.txt", day))).expect("could not read input file"); + let mut input = BufReader::new(f).lines().flatten().collect::>(); + if let Some(last) = input.last() { + if last.is_empty() { + input.pop(); + } + } + input +} + macro_rules! days { ( $($n:expr, $module:ident),* ) => { fn get_result(day: u8, part: u8) -> i64 { + let input = read_input(day); + match day { $( $n => match part { - 2 => $module::part2(), - _ => $module::part1(), + 2 => $module::part2(input), + _ => $module::part1(input), } )* _ => panic!("day {} missing", day), From e26bd81389c757c7da6eaca886e8c66393eccaf7 Mon Sep 17 00:00:00 2001 From: ThetaDev Date: Sat, 3 Dec 2022 14:14:00 +0100 Subject: [PATCH 03/10] day 3 --- Cargo.lock | 16 +++ Cargo.toml | 1 + input/day3.txt | 300 +++++++++++++++++++++++++++++++++++++++++++++++++ src/day3.rs | 109 ++++++++++++++++++ src/main.rs | 4 +- 5 files changed, 429 insertions(+), 1 deletion(-) create mode 100644 input/day3.txt create mode 100644 src/day3.rs diff --git a/Cargo.lock b/Cargo.lock index 49e94b6..9f7aead 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6,6 +6,7 @@ version = 3 name = "adventofcode22" version = "0.1.0" dependencies = [ + "itertools", "path_macro", "rstest", ] @@ -16,6 +17,21 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "either" +version = "1.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "90e5c1c8368803113bf0c9584fc495a58b86dc8a29edbf8fe877d21d9507e797" + +[[package]] +name = "itertools" +version = "0.10.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b0fd2260e829bddf4cb6ea802289de2f86d6a7a690192fbe91b3f46e0f2c8473" +dependencies = [ + "either", +] + [[package]] name = "path_macro" version = "1.0.0" diff --git a/Cargo.toml b/Cargo.toml index aeb858b..571b646 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,6 +4,7 @@ version = "0.1.0" edition = "2021" [dependencies] +itertools = "0.10.5" path_macro = "1.0.0" [dev-dependencies] diff --git a/input/day3.txt b/input/day3.txt new file mode 100644 index 0000000..fca63f0 --- /dev/null +++ b/input/day3.txt @@ -0,0 +1,300 @@ +qFdBBvtHHfvRlfvsqldvqjPpQmnQmjnjjjTRTLGRNG +ZCWhhCsJCzSJzSbzgsmPTGNNPPNGjgLTLjgn +WJZsbJMwJcszJcScwhVltFwBFBlqddvFdHDfqq +crtTsGTtqFThGQGCrsjTwdNJwpRdnJJwffRClpSf +PWVBPVHLvHHVgvZWBzmPpnfRSJJRQnSRflRPSNSl +gmzBzDgzmZQWLDLLgVmDrqGhsscrqDMGhcqtqcFr +HsbcdVrsbVbcLfPqqQsqqtPj +mMBFzZRnmFMRBDnDFBGZDGdDqLjtdQtPtgfPfttg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gFRGRwmRJvwbBbJDwjvTbjrwhJvJ +WHClslcNNWcqNWlCZdcHsVrThBwBjbhDTDBhrvDZJTwm +NWVqqcHHNpsNcNVdVlhCMlHQQMQQzLfzQPttFGPMLSLgtF diff --git a/src/day3.rs b/src/day3.rs new file mode 100644 index 0000000..eee49b1 --- /dev/null +++ b/src/day3.rs @@ -0,0 +1,109 @@ +//! Day 3: Rucksack Reorganization + +use itertools::Itertools; + +fn priority(item: char) -> i64 { + match item { + 'a'..='z' => item as i64 - 'a' as i64 + 1, + 'A'..='Z' => item as i64 - 'A' as i64 + 27, + _ => panic!("priority undefined for item `{}`", item), + } +} + +fn get_rucksack_compartments(rucksack: &str) -> [String; 2] { + [ + rucksack[..rucksack.len() / 2].to_owned(), + rucksack[rucksack.len() / 2..].to_owned(), + ] +} + +fn get_common_item(compartments: &[String]) -> char { + compartments[0] + .chars() + .find(|item| { + compartments[1..] + .iter() + .all(|comp| comp.chars().any(|it| &it == item)) + }) + .unwrap() +} + +pub fn part1(input: Vec) -> i64 { + input + .into_iter() + .map(|rucksack| { + let compartments = get_rucksack_compartments(&rucksack); + let common_item = get_common_item(&compartments); + priority(common_item) + }) + .sum() +} + +pub fn part2(input: Vec) -> i64 { + input + .into_iter() + .tuples::<(String, String, String)>() + .map(|group| { + let common_item = get_common_item(&[group.0, group.1, group.2]); + priority(common_item) + }) + .sum() +} + +#[cfg(test)] +mod tests { + use super::*; + + use rstest::rstest; + + #[rstest] + #[case('p', 16)] + #[case('L', 38)] + #[case('P', 42)] + #[case('v', 22)] + #[case('t', 20)] + #[case('s', 19)] + fn t_priority(#[case] item: char, #[case] expect: i64) { + assert_eq!(priority(item), expect) + } + + #[test] + fn t_compartments() { + let compartments = get_rucksack_compartments("vJrwpWtwJgWrhcsFMMfFFhFp"); + + assert_eq!(compartments[0], "vJrwpWtwJgWr"); + assert_eq!(compartments[1], "hcsFMMfFFhFp"); + + assert_eq!(get_common_item(&compartments), 'p'); + } + + #[test] + fn t_common_items3() { + assert_eq!( + get_common_item(&[ + "vJrwpWtwJgWrhcsFMMfFFhFp".to_owned(), + "jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL".to_owned(), + "PmmdzqPrVvPwwTWBwg".to_owned() + ]), + 'r' + ); + assert_eq!( + get_common_item(&[ + "wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn".to_owned(), + "ttgJtRGJQctTZtZT".to_owned(), + "CrZsJsPPZsGzwwsLwLmpwMDw".to_owned() + ]), + 'Z' + ); + } + + #[test] + fn t_part1() { + assert_eq!(part1(crate::read_input(3)), 8039); + } + + #[test] + fn t_part2() { + assert_eq!(part2(crate::read_input(3)), 2510); + } +} diff --git a/src/main.rs b/src/main.rs index 6905631..037225e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,6 +8,7 @@ use path_macro::path; mod day0; mod day1; mod day2; +mod day3; pub(crate) fn read_input(day: u8) -> Vec { let f = @@ -42,7 +43,8 @@ macro_rules! days { days! { 0, day0, 1, day1, - 2, day2 + 2, day2, + 3, day3 } fn main() { From 7f9ce3e21febd663954572d0f8335180ea2ded2b Mon Sep 17 00:00:00 2001 From: ThetaDev Date: Sun, 4 Dec 2022 15:03:15 +0100 Subject: [PATCH 04/10] day 4 --- input/day4.txt | 1000 ++++++++++++++++++++++++++++++++++++++++++++++++ src/day4.rs | 78 ++++ src/main.rs | 4 +- 3 files changed, 1081 insertions(+), 1 deletion(-) create mode 100644 input/day4.txt create mode 100644 src/day4.rs diff --git a/input/day4.txt b/input/day4.txt new file mode 100644 index 0000000..f1311b8 --- /dev/null +++ b/input/day4.txt @@ -0,0 +1,1000 @@ +31-31,32-40 +26-92,13-91 +9-90,29-91 +72-72,25-73 +28-79,79-79 +52-77,53-53 +17-54,17-54 +11-47,4-10 +1-96,1-97 +38-99,39-63 +19-54,18-53 +1-73,1-73 +17-34,18-34 +93-93,25-94 +96-98,7-95 +58-97,73-98 +7-64,8-64 +87-91,67-90 +42-78,77-85 +24-96,24-97 +71-78,72-97 +5-28,27-29 +19-86,86-90 +19-85,20-20 +64-65,1-64 +28-78,27-81 +79-91,80-95 +32-89,32-82 +3-96,2-4 +70-83,15-70 +5-90,8-91 +16-60,16-61 +38-97,39-90 +25-56,6-55 +12-14,13-13 +11-95,11-96 +13-16,12-22 +15-29,14-72 +68-88,67-69 +23-95,94-97 +63-77,62-64 +19-69,20-85 +23-63,61-62 +7-47,6-47 +39-86,39-86 +98-99,4-98 +4-6,5-88 +41-89,88-89 +14-95,96-98 +6-62,3-62 +1-68,6-68 +25-90,40-90 +11-81,10-12 +2-91,1-91 +35-62,27-61 +14-28,14-28 +13-78,14-79 +51-52,46-52 +6-77,77-78 +98-99,50-98 +7-23,24-85 +38-56,38-62 +96-99,1-97 +32-32,31-31 +5-89,4-6 +22-84,21-85 +14-40,39-89 +40-91,40-91 +3-6,4-60 +48-59,60-60 +2-92,2-92 +34-35,7-34 +32-64,33-64 +36-56,36-55 +95-96,44-96 +91-99,37-92 +10-98,11-96 +24-68,69-94 +98-99,37-98 +99-99,6-87 +11-90,11-12 +76-87,36-77 +19-97,10-99 +47-77,78-93 +96-96,21-96 +50-97,99-99 +3-50,2-81 +18-69,19-69 +17-92,16-16 +18-72,19-72 +1-76,5-77 +14-97,98-99 +28-90,19-89 +19-47,47-48 +10-97,9-97 +11-96,11-85 +20-22,20-97 +1-94,1-95 +49-52,48-53 +3-3,3-34 +12-97,85-96 +49-84,48-84 +3-89,89-90 +68-96,41-96 +69-80,2-65 +16-81,16-81 +1-96,95-99 +49-99,49-94 +28-92,91-95 +2-95,1-99 +12-30,8-31 +21-68,26-69 +6-31,2-31 +49-66,49-65 +55-67,55-68 +21-50,20-44 +14-42,15-43 +70-89,70-89 +3-99,1-3 +7-86,6-37 +58-71,68-71 +55-83,55-82 +61-99,11-99 +83-83,3-84 +25-82,25-81 +14-48,15-54 +97-98,13-96 +25-63,63-63 +4-81,37-82 +12-98,98-98 +31-34,33-56 +3-83,1-82 +33-99,99-99 +96-99,39-97 +82-84,43-77 +95-99,1-96 +83-84,6-83 +58-60,58-58 +51-92,92-93 +93-93,33-93 +1-93,92-97 +16-16,16-31 +10-55,54-55 +56-64,63-70 +18-88,10-19 +55-95,5-95 +62-77,48-61 +3-65,56-65 +54-55,55-56 +22-70,23-70 +18-91,18-18 +91-98,2-99 +9-56,8-56 +84-92,17-83 +98-98,25-99 +22-63,66-84 +4-50,2-5 +12-13,12-49 +30-85,31-79 +57-83,57-87 +13-79,13-79 +30-38,17-87 +64-65,5-64 +99-99,29-99 +28-75,7-27 +3-79,3-67 +10-90,24-89 +10-53,83-91 +39-74,35-39 +52-78,53-78 +2-87,3-95 +58-68,57-68 +6-32,31-51 +18-36,18-35 +3-99,3-99 +8-81,7-80 +50-53,51-52 +91-92,84-92 +19-91,5-18 +40-68,68-69 +30-90,52-91 +20-27,19-26 +98-99,99-99 +27-58,28-58 +61-61,45-62 +67-75,60-77 +69-70,68-72 +1-96,4-93 +2-95,38-96 +56-57,11-56 +54-84,6-53 +65-85,5-66 +22-84,21-84 +50-64,51-53 +7-75,74-81 +34-69,34-70 +35-35,6-35 +8-96,69-96 +13-98,13-97 +80-80,55-81 +16-62,6-98 +2-87,63-87 +84-91,7-92 +3-3,4-98 +3-80,52-80 +80-94,11-79 +2-95,1-71 +31-96,95-95 +9-65,64-65 +61-69,60-97 +2-99,2-98 +18-73,19-21 +80-80,80-82 +47-70,47-71 +13-30,23-30 +16-41,17-41 +3-13,12-79 +72-75,73-87 +13-13,13-81 +29-74,30-73 +24-43,7-42 +1-93,92-93 +4-47,4-93 +8-10,9-96 +2-87,5-88 +51-61,50-64 +60-64,60-63 +26-66,27-73 +13-85,14-86 +42-43,42-92 +45-72,44-44 +83-83,84-99 +3-5,6-97 +10-92,10-10 +43-69,44-69 +49-50,15-50 +4-91,4-91 +51-52,20-51 +7-84,1-96 +6-76,5-76 +15-19,14-14 +57-68,46-69 +80-80,3-81 +4-93,7-92 +37-40,38-41 +8-49,16-49 +6-86,86-87 +71-85,71-85 +35-68,69-96 +57-96,2-56 +13-33,5-32 +14-97,14-97 +83-98,60-99 +4-24,10-25 +1-84,2-84 +75-86,70-74 +22-79,1-78 +94-94,10-93 +6-70,7-70 +25-56,16-20 +2-87,1-88 +50-52,51-92 +2-68,3-42 +3-93,92-92 +8-97,96-96 +90-93,77-94 +61-98,6-99 +30-47,16-47 +27-92,26-26 +18-32,19-32 +23-76,19-21 +8-43,23-44 +9-93,10-93 +64-85,84-86 +2-88,88-89 +51-77,32-78 +53-83,25-52 +74-97,74-99 +99-99,7-98 +3-96,3-96 +67-67,68-69 +3-96,2-99 +18-95,17-19 +41-43,42-59 +3-5,5-5 +4-98,3-98 +41-69,70-88 +21-90,20-89 +76-78,77-81 +12-47,46-46 +33-85,34-84 +12-14,13-91 +46-59,45-59 +73-79,72-94 +7-18,6-8 +35-94,94-95 +15-18,15-18 +22-94,23-68 +15-16,15-81 +71-71,39-72 +61-61,11-62 +38-69,84-98 +76-80,76-79 +8-71,71-72 +27-94,28-71 +5-34,5-69 +48-62,35-61 +30-30,31-95 +45-96,95-99 +27-86,28-87 +72-72,44-72 +3-90,2-96 +31-98,30-71 +77-77,8-78 +19-99,18-20 +96-96,88-97 +10-30,7-8 +46-94,46-94 +9-34,33-96 +34-79,7-78 +31-77,32-76 +25-50,26-49 +37-70,36-38 +31-58,32-58 +12-97,12-98 +85-91,7-86 +57-77,4-78 +18-64,64-64 +73-74,72-74 +1-42,1-2 +30-99,37-98 +35-97,31-97 +17-57,57-58 +65-86,19-85 +27-95,50-97 +16-75,3-74 +44-87,86-94 +4-52,3-8 +31-96,95-95 +13-39,1-40 +29-95,71-95 +8-93,6-6 +1-97,2-98 +2-9,10-90 +22-24,19-23 +12-95,31-87 +1-77,7-78 +10-83,3-9 +60-88,60-87 +46-76,41-75 +21-57,20-57 +70-86,70-99 +46-56,55-72 +4-80,14-20 +14-55,13-71 +4-21,3-22 +2-91,2-92 +10-54,9-55 +1-3,2-98 +2-94,1-97 +3-89,3-4 +3-91,10-90 +11-13,10-68 +70-79,69-71 +42-77,42-77 +16-97,1-97 +18-82,82-86 +8-51,8-60 +21-70,20-70 +4-93,5-92 +46-46,6-45 +9-88,8-88 +26-92,15-91 +16-17,16-66 +58-65,7-65 +19-21,20-54 +48-93,48-93 +42-86,43-85 +28-44,28-44 +13-13,14-97 +10-72,73-73 +94-94,15-95 +44-67,26-66 +16-97,15-94 +74-74,9-75 +8-43,1-51 +33-58,34-57 +25-29,54-79 +21-77,29-76 +30-31,30-93 +88-96,42-87 +11-92,10-93 +25-72,2-73 +1-99,4-99 +33-56,33-80 +16-93,15-94 +36-39,34-38 +31-68,31-31 +20-89,88-90 +87-87,43-87 +12-14,11-14 +5-27,5-28 +28-70,71-71 +16-98,25-97 +13-89,12-12 +27-33,10-34 +97-99,54-95 +12-84,85-89 +39-58,40-57 +43-44,43-54 +92-94,93-95 +47-48,47-87 +10-31,9-30 +5-96,4-97 +67-70,67-71 +20-52,53-72 +44-65,38-66 +34-55,34-94 +1-98,6-89 +4-83,57-82 +94-95,4-93 +57-58,17-58 +8-38,5-9 +5-88,87-87 +17-76,75-77 +5-9,5-10 +31-36,28-31 +50-50,44-51 +19-89,19-89 +34-54,35-96 +54-60,32-64 +56-78,56-77 +39-39,39-39 +21-22,20-21 +52-76,51-53 +62-89,63-71 +40-85,40-70 +5-15,16-90 +42-83,41-46 +40-98,4-98 +29-94,28-54 +62-92,91-98 +8-94,9-93 +83-88,51-88 +4-60,7-60 +41-72,41-49 +31-57,51-56 +49-99,49-94 +79-79,10-80 +21-86,57-87 +42-44,23-43 +7-94,8-94 +24-32,24-33 +32-51,39-52 +12-28,27-82 +50-63,50-97 +58-64,64-64 +72-72,73-77 +51-65,51-51 +41-98,42-98 +97-98,1-97 +5-76,5-75 +83-93,42-92 +87-87,2-88 +26-41,26-27 +68-71,67-70 +61-62,36-62 +6-66,1-31 +57-73,15-72 +11-97,96-97 +4-99,4-98 +22-54,22-54 +18-43,43-43 +2-39,3-7 +24-91,99-99 +98-98,48-97 +3-70,3-71 +71-85,81-84 +93-98,2-92 +79-86,9-80 +30-61,60-83 +31-37,23-36 +25-95,26-95 +43-56,42-69 +18-28,5-27 +11-87,12-86 +58-99,59-97 +3-3,3-70 +31-41,17-30 +14-27,14-15 +2-70,3-94 +2-93,1-93 +38-90,39-79 +57-99,1-79 +70-95,70-78 +24-89,23-88 +4-84,19-83 +46-70,5-47 +10-70,71-73 +13-95,14-95 +48-52,49-52 +20-22,21-21 +88-88,1-88 +39-84,45-83 +28-75,28-75 +17-17,17-95 +3-98,97-97 +9-21,9-97 +28-37,27-34 +32-47,32-46 +13-28,27-72 +43-65,5-43 +22-48,47-48 +8-90,91-95 +51-53,10-52 +34-65,30-65 +1-98,1-2 +65-79,13-79 +74-74,13-75 +31-33,32-99 +4-95,38-94 +10-90,90-91 +62-62,4-61 +14-24,24-46 +4-85,10-84 +15-83,14-83 +1-85,3-86 +12-12,13-13 +15-89,14-90 +72-94,23-73 +11-69,8-68 +20-90,16-17 +5-95,32-96 +49-71,49-91 +22-89,22-89 +4-43,5-5 +51-98,15-97 +85-85,28-85 +18-61,18-81 +88-89,56-88 +71-79,72-79 +3-84,19-85 +18-18,19-85 +15-15,4-15 +46-71,44-71 +56-88,7-89 +37-50,36-51 +84-84,55-83 +14-27,15-26 +74-76,22-75 +77-99,77-95 +13-44,20-45 +6-95,12-96 +11-41,10-12 +10-76,17-77 +8-84,9-85 +14-64,13-15 +5-82,3-3 +43-53,42-44 +14-74,2-75 +12-97,12-98 +59-93,93-94 +17-81,15-82 +87-91,24-92 +8-84,9-84 +20-94,93-99 +46-97,97-98 +87-97,52-97 +2-90,3-91 +36-74,38-73 +55-59,55-55 +49-69,48-59 +54-62,54-96 +49-74,10-48 +27-78,26-26 +31-31,29-30 +2-76,1-90 +10-98,7-97 +14-15,14-93 +24-95,95-98 +13-23,22-23 +6-66,5-5 +85-86,66-86 +3-97,96-99 +1-93,1-90 +27-77,27-28 +51-64,51-51 +13-83,6-83 +40-54,53-54 +3-94,1-4 +15-29,15-28 +6-83,84-95 +62-62,8-63 +12-76,13-76 +3-7,5-7 +10-93,11-98 +28-28,28-63 +11-34,10-98 +6-8,7-49 +8-22,6-34 +29-47,29-49 +97-97,53-80 +20-99,21-75 +3-56,7-57 +43-63,43-64 +3-99,2-99 +24-99,24-25 +40-41,40-59 +60-63,60-60 +13-87,3-14 +70-88,25-88 +39-96,96-97 +11-53,12-52 +35-48,48-49 +11-53,9-25 +65-75,74-91 +74-80,80-80 +10-12,12-13 +46-73,74-84 +4-80,81-99 +4-84,83-91 +6-86,40-79 +46-82,22-81 +15-40,16-39 +5-86,45-93 +4-31,3-5 +40-84,31-83 +6-88,5-88 +8-79,12-80 +10-94,9-95 +81-81,23-81 +9-28,1-9 +47-99,46-83 +24-41,24-85 +10-67,9-35 +28-61,16-61 +6-6,7-71 +42-87,10-99 +97-97,2-97 +38-43,37-44 +15-86,15-87 +3-93,3-4 +85-86,84-87 +3-94,1-3 +32-47,47-47 +47-47,28-48 +4-97,4-98 +11-18,37-61 +31-38,30-38 +1-37,3-37 +23-60,61-99 +93-98,37-94 +8-97,9-97 +59-95,59-59 +81-87,13-80 +6-97,4-16 +15-27,27-73 +12-95,4-96 +54-96,22-55 +72-76,71-81 +5-94,6-92 +24-78,25-78 +55-64,52-63 +51-65,59-64 +32-87,32-58 +7-58,34-58 +29-83,29-82 +3-94,3-95 +84-84,71-85 +4-98,3-5 +17-36,18-37 +38-49,37-50 +14-61,94-99 +61-82,96-96 +25-98,98-99 +63-78,64-64 +43-49,42-50 +43-47,43-47 +14-62,23-63 +7-97,9-64 +49-82,49-90 +2-59,2-60 +53-85,53-86 +1-99,56-99 +26-92,26-60 +70-83,2-52 +54-55,54-85 +37-71,70-77 +87-91,15-92 +11-61,12-61 +9-9,7-10 +22-53,20-54 +88-89,1-88 +9-80,79-91 +43-85,43-59 +34-88,35-87 +9-98,24-97 +80-85,7-79 +9-38,38-39 +56-56,56-67 +79-90,79-89 +29-61,60-61 +1-95,1-96 +60-66,60-66 +41-75,74-74 +18-55,54-77 +4-98,3-97 +5-14,2-13 +9-51,9-55 +26-62,35-63 +85-97,84-96 +2-99,98-98 +67-94,67-94 +10-18,17-96 +12-99,13-13 +8-99,10-99 +20-28,21-27 +77-87,58-78 +38-51,38-59 +19-90,19-59 +24-98,37-74 +49-88,28-89 +18-85,84-85 +36-48,37-48 +70-92,91-93 +45-50,45-91 +82-93,44-50 +1-97,97-99 +45-58,44-59 +4-21,20-78 +56-83,57-57 +92-92,25-92 +33-36,35-35 +17-86,22-74 +12-86,86-87 +36-41,2-40 +34-34,35-35 +12-95,94-98 +88-89,32-99 +13-90,12-31 +3-90,1-1 +9-96,9-99 +8-60,13-61 +91-96,97-97 +19-58,18-20 +4-4,4-76 +2-57,2-75 +97-99,18-93 +15-37,38-86 +55-94,54-87 +15-35,16-94 +15-57,59-97 +14-90,14-91 +25-53,54-54 +87-97,86-97 +8-13,48-75 +5-80,3-3 +65-79,65-78 +1-73,72-73 +67-69,46-67 +50-67,49-67 +23-59,22-59 +2-27,1-96 +14-33,1-32 +30-82,16-30 +8-18,14-19 +30-30,27-30 +99-99,11-56 +25-26,25-38 +61-66,65-65 +44-77,93-97 +36-36,37-93 +11-45,10-10 +32-57,31-56 +98-99,41-96 +98-99,11-99 +6-8,7-79 +18-87,45-87 +22-84,22-92 +3-91,9-81 +5-78,6-78 +1-83,82-95 +54-61,54-61 +67-86,66-94 +17-85,17-86 +41-41,41-66 +57-68,14-67 +6-7,7-96 +10-96,72-98 +21-73,20-74 +35-98,9-97 +98-98,12-99 +6-48,5-96 +18-96,97-98 +19-21,20-52 +10-71,1-72 +2-35,2-2 +13-85,85-86 +29-81,81-82 +5-77,78-96 +40-41,28-41 +2-92,1-91 +7-81,28-82 +38-72,37-39 +58-85,25-85 +3-43,2-4 +30-58,31-79 +73-76,22-74 +43-73,44-74 +47-83,46-82 +64-92,63-69 +49-90,90-90 +11-98,11-97 +45-83,44-64 +18-65,38-65 +7-97,6-98 +9-67,11-77 +10-99,9-98 +11-41,10-58 +91-91,20-44 +2-99,2-98 +69-77,70-81 +45-73,46-55 +16-17,16-17 +13-76,75-81 +9-38,9-18 +42-64,3-41 +2-8,7-54 +53-58,28-59 +86-87,2-86 +93-93,13-94 +99-99,17-98 +55-63,60-60 +9-54,8-54 +89-95,94-95 +30-87,86-86 +14-16,15-96 +9-90,8-8 +41-95,40-94 +75-91,88-91 +9-13,8-10 +5-93,93-93 +19-41,19-41 +58-65,57-59 +56-99,55-70 +76-76,3-68 +98-98,46-92 +73-89,74-76 +78-93,26-78 +26-26,27-77 +57-76,75-75 +64-82,64-98 +64-91,21-80 +30-65,64-64 +43-43,43-87 +29-93,28-92 +94-95,13-95 +23-95,22-86 +18-85,84-84 +23-34,22-99 +82-84,77-84 +11-30,11-31 +6-34,33-34 +19-59,19-60 +4-66,65-72 +57-75,24-58 +26-55,34-54 +23-62,17-61 +1-89,3-92 +20-38,20-37 +10-98,10-11 +5-93,58-87 +89-90,62-90 +69-70,38-69 +36-63,62-63 +38-49,39-92 +86-87,22-87 +99-99,52-78 +16-99,16-92 +62-64,63-64 +87-88,69-87 +55-78,69-78 +5-15,15-72 +52-60,16-61 +4-97,1-96 +2-85,19-85 +5-10,1-22 +37-81,31-82 +21-91,12-90 +70-71,37-70 +64-64,65-92 +40-93,94-98 +38-84,83-85 +44-97,44-98 +31-31,10-31 +28-96,27-27 +32-79,35-78 +72-93,8-88 +7-77,7-58 +8-93,2-8 +5-91,28-90 +2-96,1-96 +52-56,51-54 +8-85,6-85 +4-84,6-49 +9-98,2-8 +61-97,46-60 +95-96,66-94 +98-98,86-99 +27-28,27-97 +54-85,55-86 +91-91,35-90 +4-89,10-88 +6-97,5-80 +34-57,35-37 +6-98,6-98 +12-67,31-68 +35-93,34-36 +11-60,39-61 +66-81,40-96 +57-57,8-56 +15-79,10-80 +78-79,27-79 +45-62,46-63 +14-35,35-71 +12-49,11-13 +51-67,67-67 +67-68,54-68 +55-93,13-41 +22-91,21-23 +92-95,88-96 +82-94,82-94 +99-99,2-97 +6-46,6-46 +39-75,1-40 +8-54,54-55 +78-93,77-93 +67-95,95-95 +2-4,3-98 +17-95,94-94 +36-51,42-50 +1-95,1-94 +67-96,67-95 +7-78,3-77 +18-48,17-47 +76-96,5-95 +45-45,21-46 +48-76,47-76 +36-38,33-38 +17-33,13-70 +78-93,78-78 +3-50,2-50 +8-8,8-84 +33-97,33-96 +51-53,52-87 +21-42,22-42 +98-99,2-98 +8-99,8-97 +41-51,41-51 +20-75,68-74 +2-98,98-99 +46-95,68-88 +16-69,68-97 +41-96,95-97 +1-98,1-98 +35-93,35-97 +54-66,1-97 +29-98,29-97 +44-44,44-67 +7-69,69-69 +3-74,73-73 +8-78,9-75 +43-51,51-52 +13-49,48-48 +42-76,3-76 +16-50,9-55 +62-71,62-71 +79-98,7-66 +43-58,58-58 +25-30,25-54 +2-17,16-27 +88-99,15-87 +33-94,94-94 +9-11,10-43 +38-97,39-95 +17-58,18-57 +25-97,17-62 +9-19,10-26 +87-88,86-87 +23-58,22-59 +49-96,49-49 +64-81,31-70 +3-85,2-86 +10-10,9-28 +9-51,5-9 +49-57,48-57 +1-65,3-66 +54-70,70-71 +2-61,1-60 +90-98,57-91 diff --git a/src/day4.rs b/src/day4.rs new file mode 100644 index 0000000..e2b38a1 --- /dev/null +++ b/src/day4.rs @@ -0,0 +1,78 @@ +//! Day 4: Camp Cleanup + +use std::ops::RangeInclusive; + +fn get_ranges(input: Vec) -> Vec<(RangeInclusive, RangeInclusive)> { + // 31-31,32-40 + input + .into_iter() + .map(|line| { + let (r1, r2) = line.split_once(',').unwrap(); + (parse_range(r1), parse_range(r2)) + }) + .collect() +} + +fn parse_range(s: &str) -> RangeInclusive { + let (a, b) = s.split_once('-').unwrap(); + RangeInclusive::new(a.parse().unwrap(), b.parse().unwrap()) +} + +pub fn part1(input: Vec) -> i64 { + let ranges = get_ranges(input); + + ranges + .iter() + .filter(|(a, b)| { + (b.start() >= a.start() && b.end() <= a.end()) + || (a.start() >= b.start() && a.end() <= b.end()) + }) + .count() + .try_into() + .unwrap() +} + +pub fn part2(input: Vec) -> i64 { + let ranges = get_ranges(input); + + ranges + .iter() + .filter(|(a, b)| { + a.contains(b.start()) + || a.contains(b.end()) + || b.contains(a.start()) + || b.contains(a.end()) + }) + .count() + .try_into() + .unwrap() +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn t_example() { + let input = vec![ + "2-4,6-8".to_owned(), + "2-3,4-5".to_owned(), + "5-7,7-9".to_owned(), + "2-8,3-7".to_owned(), + "6-6,4-6".to_owned(), + "2-6,4-8".to_owned(), + ]; + assert_eq!(part1(input.clone()), 2); + assert_eq!(part2(input), 4); + } + + #[test] + fn t_part1() { + assert_eq!(part1(crate::read_input(4)), 556); + } + + #[test] + fn t_part2() { + assert_eq!(part2(crate::read_input(4)), 876); + } +} diff --git a/src/main.rs b/src/main.rs index 037225e..2adf37d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,6 +9,7 @@ mod day0; mod day1; mod day2; mod day3; +mod day4; pub(crate) fn read_input(day: u8) -> Vec { let f = @@ -44,7 +45,8 @@ days! { 0, day0, 1, day1, 2, day2, - 3, day3 + 3, day3, + 4, day4 } fn main() { From 80a64349562f66d24317b1cd2876a72872564ea0 Mon Sep 17 00:00:00 2001 From: ThetaDev Date: Mon, 5 Dec 2022 23:04:06 +0100 Subject: [PATCH 05/10] refactor: use string as output type --- src/day0.rs | 12 ++++++------ src/day1.rs | 12 ++++++------ src/day2.rs | 14 ++++++++------ src/day3.rs | 14 ++++++++------ src/day4.rs | 18 ++++++++---------- src/main.rs | 2 +- 6 files changed, 37 insertions(+), 35 deletions(-) diff --git a/src/day0.rs b/src/day0.rs index 04178fd..924db48 100644 --- a/src/day0.rs +++ b/src/day0.rs @@ -1,11 +1,11 @@ //! Dummy challenge for testing -pub fn part1(input: Vec) -> i64 { - input[0].parse().unwrap() +pub fn part1(input: Vec) -> String { + input[0].to_owned() } -pub fn part2(input: Vec) -> i64 { - input[1].parse().unwrap() +pub fn part2(input: Vec) -> String { + input[1].to_owned() } #[cfg(test)] @@ -14,11 +14,11 @@ mod tests { #[test] fn t_part1() { - assert_eq!(part1(crate::read_input(0)), 123); + assert_eq!(part1(crate::read_input(0)), "123"); } #[test] fn t_part2() { - assert_eq!(part2(crate::read_input(0)), 456); + assert_eq!(part2(crate::read_input(0)), "456"); } } diff --git a/src/day1.rs b/src/day1.rs index 15dbd58..b193226 100644 --- a/src/day1.rs +++ b/src/day1.rs @@ -17,12 +17,12 @@ fn get_elves(input: Vec) -> Vec { elves } -pub fn part1(input: Vec) -> i64 { +pub fn part1(input: Vec) -> String { let elves = get_elves(input); - *elves.iter().max().unwrap() as i64 + elves.iter().max().unwrap().to_string() } -pub fn part2(input: Vec) -> i64 { +pub fn part2(input: Vec) -> String { let mut elves = get_elves(input); elves.sort(); @@ -30,7 +30,7 @@ pub fn part2(input: Vec) -> i64 { for i in 1..=3 { acc += elves[elves.len() - i]; } - acc as i64 + acc.to_string() } #[cfg(test)] @@ -39,11 +39,11 @@ mod tests { #[test] fn t_part1() { - assert_eq!(part1(crate::read_input(1)), 69177); + assert_eq!(part1(crate::read_input(1)), "69177"); } #[test] fn t_part2() { - assert_eq!(part2(crate::read_input(1)), 207456); + assert_eq!(part2(crate::read_input(1)), "207456"); } } diff --git a/src/day2.rs b/src/day2.rs index b73642d..31d9672 100644 --- a/src/day2.rs +++ b/src/day2.rs @@ -92,20 +92,22 @@ fn your_sign(opp: Sign, result: Ordering) -> Sign { } } -pub fn part1(input: Vec) -> i64 { +pub fn part1(input: Vec) -> String { let rounds = get_rounds(input); rounds .into_iter() .map(|(you, opp)| round_score(you, opp)) - .sum() + .sum::() + .to_string() } -pub fn part2(input: Vec) -> i64 { +pub fn part2(input: Vec) -> String { let rounds = get_rounds2(input); rounds .into_iter() .map(|(opp, result)| round_score(your_sign(opp, result), opp)) - .sum() + .sum::() + .to_string() } #[cfg(test)] @@ -125,11 +127,11 @@ mod tests { #[test] fn t_part1() { - assert_eq!(part1(crate::read_input(2)), 14827); + assert_eq!(part1(crate::read_input(2)), "14827"); } #[test] fn t_part2() { - assert_eq!(part2(crate::read_input(2)), 13889); + assert_eq!(part2(crate::read_input(2)), "13889"); } } diff --git a/src/day3.rs b/src/day3.rs index eee49b1..27e47d6 100644 --- a/src/day3.rs +++ b/src/day3.rs @@ -28,7 +28,7 @@ fn get_common_item(compartments: &[String]) -> char { .unwrap() } -pub fn part1(input: Vec) -> i64 { +pub fn part1(input: Vec) -> String { input .into_iter() .map(|rucksack| { @@ -36,10 +36,11 @@ pub fn part1(input: Vec) -> i64 { let common_item = get_common_item(&compartments); priority(common_item) }) - .sum() + .sum::() + .to_string() } -pub fn part2(input: Vec) -> i64 { +pub fn part2(input: Vec) -> String { input .into_iter() .tuples::<(String, String, String)>() @@ -47,7 +48,8 @@ pub fn part2(input: Vec) -> i64 { let common_item = get_common_item(&[group.0, group.1, group.2]); priority(common_item) }) - .sum() + .sum::() + .to_string() } #[cfg(test)] @@ -99,11 +101,11 @@ mod tests { #[test] fn t_part1() { - assert_eq!(part1(crate::read_input(3)), 8039); + assert_eq!(part1(crate::read_input(3)), "8039"); } #[test] fn t_part2() { - assert_eq!(part2(crate::read_input(3)), 2510); + assert_eq!(part2(crate::read_input(3)), "2510"); } } diff --git a/src/day4.rs b/src/day4.rs index e2b38a1..438250f 100644 --- a/src/day4.rs +++ b/src/day4.rs @@ -18,7 +18,7 @@ fn parse_range(s: &str) -> RangeInclusive { RangeInclusive::new(a.parse().unwrap(), b.parse().unwrap()) } -pub fn part1(input: Vec) -> i64 { +pub fn part1(input: Vec) -> String { let ranges = get_ranges(input); ranges @@ -28,11 +28,10 @@ pub fn part1(input: Vec) -> i64 { || (a.start() >= b.start() && a.end() <= b.end()) }) .count() - .try_into() - .unwrap() + .to_string() } -pub fn part2(input: Vec) -> i64 { +pub fn part2(input: Vec) -> String { let ranges = get_ranges(input); ranges @@ -44,8 +43,7 @@ pub fn part2(input: Vec) -> i64 { || b.contains(a.end()) }) .count() - .try_into() - .unwrap() + .to_string() } #[cfg(test)] @@ -62,17 +60,17 @@ mod tests { "6-6,4-6".to_owned(), "2-6,4-8".to_owned(), ]; - assert_eq!(part1(input.clone()), 2); - assert_eq!(part2(input), 4); + assert_eq!(part1(input.clone()), "2"); + assert_eq!(part2(input), "4"); } #[test] fn t_part1() { - assert_eq!(part1(crate::read_input(4)), 556); + assert_eq!(part1(crate::read_input(4)), "556"); } #[test] fn t_part2() { - assert_eq!(part2(crate::read_input(4)), 876); + assert_eq!(part2(crate::read_input(4)), "876"); } } diff --git a/src/main.rs b/src/main.rs index 2adf37d..0d32c6f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -25,7 +25,7 @@ pub(crate) fn read_input(day: u8) -> Vec { macro_rules! days { ( $($n:expr, $module:ident),* ) => { - fn get_result(day: u8, part: u8) -> i64 { + fn get_result(day: u8, part: u8) -> String { let input = read_input(day); match day { From a6193bfff232fa3889ef6bb95f3443dfdd0a0496 Mon Sep 17 00:00:00 2001 From: ThetaDev Date: Mon, 5 Dec 2022 23:12:35 +0100 Subject: [PATCH 06/10] day 5 --- Cargo.lock | 40 ++++ Cargo.toml | 2 + input/day5.txt | 511 +++++++++++++++++++++++++++++++++++++++++++++++++ src/day5.rs | 134 +++++++++++++ src/main.rs | 4 +- 5 files changed, 690 insertions(+), 1 deletion(-) create mode 100644 input/day5.txt create mode 100644 src/day5.rs diff --git a/Cargo.lock b/Cargo.lock index 9f7aead..c432058 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7,10 +7,21 @@ name = "adventofcode22" version = "0.1.0" dependencies = [ "itertools", + "once_cell", "path_macro", + "regex", "rstest", ] +[[package]] +name = "aho-corasick" +version = "0.7.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cc936419f96fa211c1b9166887b38e5e40b19958e5b895be7c1f93adec7071ac" +dependencies = [ + "memchr", +] + [[package]] name = "cfg-if" version = "1.0.0" @@ -32,6 +43,18 @@ dependencies = [ "either", ] +[[package]] +name = "memchr" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d" + +[[package]] +name = "once_cell" +version = "1.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "86f0b0d4bf799edbc74508c1e8bf170ff5f41238e5f8225603ca7caaae2b7860" + [[package]] name = "path_macro" version = "1.0.0" @@ -56,6 +79,23 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "regex" +version = "1.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e076559ef8e241f2ae3479e36f97bd5741c0330689e217ad51ce2c76808b868a" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.6.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "456c603be3e8d448b072f410900c09faf164fbce2d480456f50eea6e25f9c848" + [[package]] name = "rstest" version = "0.16.0" diff --git a/Cargo.toml b/Cargo.toml index 571b646..7e1f818 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,7 +5,9 @@ edition = "2021" [dependencies] itertools = "0.10.5" +once_cell = "1.16.0" path_macro = "1.0.0" +regex = "1.7.0" [dev-dependencies] rstest = { version = "0.16.0", default-features = false } diff --git a/input/day5.txt b/input/day5.txt new file mode 100644 index 0000000..0d3bcf4 --- /dev/null +++ b/input/day5.txt @@ -0,0 +1,511 @@ + [J] [B] [T] + [M] [L] [Q] [L] [R] + [G] [Q] [W] [S] [B] [L] +[D] [D] [T] [M] [G] [V] [P] +[T] [N] [N] [N] [D] [J] [G] [N] +[W] [H] [H] [S] [C] [N] [R] [W] [D] +[N] [P] [P] [W] [H] [H] [B] [N] [G] +[L] [C] [W] [C] [P] [T] [M] [Z] [W] + 1 2 3 4 5 6 7 8 9 + +move 6 from 6 to 5 +move 2 from 5 to 9 +move 8 from 9 to 1 +move 3 from 5 to 4 +move 9 from 1 to 8 +move 2 from 1 to 5 +move 1 from 1 to 8 +move 14 from 8 to 2 +move 1 from 1 to 2 +move 2 from 6 to 8 +move 2 from 5 to 7 +move 6 from 8 to 6 +move 4 from 4 to 2 +move 2 from 4 to 9 +move 5 from 7 to 4 +move 2 from 7 to 5 +move 6 from 2 to 4 +move 2 from 4 to 7 +move 4 from 5 to 8 +move 1 from 5 to 2 +move 3 from 3 to 5 +move 3 from 8 to 3 +move 4 from 3 to 7 +move 2 from 9 to 8 +move 1 from 3 to 7 +move 1 from 6 to 8 +move 5 from 7 to 1 +move 3 from 7 to 2 +move 1 from 6 to 3 +move 2 from 5 to 9 +move 5 from 4 to 2 +move 3 from 5 to 9 +move 5 from 9 to 6 +move 2 from 1 to 3 +move 4 from 4 to 1 +move 2 from 8 to 1 +move 18 from 2 to 5 +move 3 from 4 to 1 +move 1 from 1 to 2 +move 1 from 6 to 8 +move 1 from 7 to 1 +move 10 from 1 to 5 +move 1 from 1 to 5 +move 3 from 8 to 1 +move 2 from 1 to 5 +move 3 from 6 to 5 +move 8 from 2 to 9 +move 2 from 9 to 7 +move 3 from 3 to 8 +move 1 from 4 to 8 +move 3 from 5 to 3 +move 15 from 5 to 8 +move 4 from 6 to 1 +move 2 from 7 to 4 +move 9 from 5 to 7 +move 1 from 6 to 8 +move 5 from 3 to 5 +move 5 from 7 to 5 +move 3 from 1 to 5 +move 2 from 4 to 8 +move 3 from 1 to 6 +move 20 from 5 to 4 +move 1 from 7 to 6 +move 21 from 8 to 2 +move 1 from 3 to 7 +move 2 from 4 to 2 +move 1 from 7 to 1 +move 18 from 2 to 8 +move 3 from 9 to 2 +move 1 from 6 to 4 +move 1 from 1 to 9 +move 8 from 8 to 6 +move 4 from 8 to 2 +move 1 from 2 to 6 +move 7 from 8 to 5 +move 2 from 5 to 3 +move 1 from 9 to 5 +move 5 from 2 to 4 +move 1 from 3 to 7 +move 2 from 5 to 7 +move 4 from 4 to 9 +move 2 from 5 to 9 +move 6 from 2 to 8 +move 3 from 7 to 3 +move 2 from 5 to 4 +move 4 from 8 to 2 +move 2 from 7 to 4 +move 7 from 6 to 4 +move 1 from 8 to 4 +move 3 from 6 to 7 +move 2 from 7 to 2 +move 7 from 9 to 7 +move 1 from 9 to 2 +move 3 from 3 to 6 +move 3 from 7 to 4 +move 2 from 7 to 9 +move 6 from 4 to 1 +move 3 from 7 to 9 +move 1 from 8 to 5 +move 1 from 3 to 6 +move 3 from 9 to 4 +move 2 from 6 to 4 +move 3 from 9 to 1 +move 4 from 2 to 8 +move 1 from 8 to 5 +move 9 from 1 to 2 +move 1 from 6 to 5 +move 1 from 7 to 2 +move 1 from 8 to 1 +move 2 from 8 to 9 +move 1 from 9 to 8 +move 1 from 5 to 7 +move 1 from 7 to 6 +move 1 from 9 to 8 +move 1 from 6 to 3 +move 26 from 4 to 3 +move 1 from 5 to 8 +move 3 from 6 to 3 +move 7 from 4 to 3 +move 1 from 1 to 3 +move 1 from 4 to 8 +move 13 from 3 to 1 +move 1 from 3 to 4 +move 12 from 2 to 5 +move 20 from 3 to 2 +move 1 from 4 to 1 +move 4 from 5 to 7 +move 1 from 7 to 8 +move 9 from 5 to 2 +move 5 from 1 to 5 +move 21 from 2 to 8 +move 5 from 8 to 4 +move 4 from 5 to 2 +move 6 from 1 to 7 +move 1 from 5 to 4 +move 4 from 3 to 1 +move 6 from 1 to 3 +move 1 from 1 to 9 +move 6 from 8 to 7 +move 4 from 8 to 2 +move 4 from 2 to 7 +move 5 from 4 to 1 +move 8 from 8 to 4 +move 1 from 9 to 6 +move 18 from 7 to 6 +move 15 from 6 to 5 +move 2 from 6 to 8 +move 2 from 6 to 3 +move 8 from 3 to 7 +move 15 from 5 to 7 +move 3 from 4 to 9 +move 12 from 2 to 3 +move 3 from 9 to 4 +move 6 from 7 to 9 +move 9 from 4 to 5 +move 10 from 3 to 5 +move 9 from 5 to 2 +move 14 from 7 to 8 +move 14 from 8 to 5 +move 4 from 2 to 4 +move 1 from 4 to 6 +move 2 from 8 to 4 +move 3 from 8 to 9 +move 18 from 5 to 1 +move 1 from 5 to 9 +move 1 from 7 to 4 +move 5 from 5 to 9 +move 3 from 2 to 4 +move 13 from 9 to 2 +move 13 from 2 to 6 +move 1 from 7 to 3 +move 3 from 3 to 1 +move 9 from 6 to 5 +move 1 from 7 to 8 +move 20 from 1 to 8 +move 2 from 2 to 8 +move 5 from 6 to 9 +move 15 from 8 to 7 +move 3 from 5 to 3 +move 5 from 1 to 3 +move 2 from 3 to 4 +move 3 from 9 to 5 +move 4 from 5 to 2 +move 4 from 5 to 7 +move 3 from 4 to 9 +move 10 from 7 to 8 +move 2 from 9 to 4 +move 1 from 5 to 6 +move 8 from 7 to 9 +move 1 from 6 to 7 +move 6 from 3 to 4 +move 12 from 9 to 8 +move 1 from 1 to 5 +move 2 from 7 to 8 +move 1 from 7 to 5 +move 1 from 9 to 5 +move 2 from 2 to 9 +move 11 from 8 to 1 +move 7 from 1 to 5 +move 3 from 1 to 6 +move 5 from 8 to 9 +move 8 from 4 to 3 +move 4 from 4 to 6 +move 5 from 9 to 3 +move 4 from 4 to 5 +move 2 from 6 to 7 +move 1 from 9 to 5 +move 2 from 7 to 4 +move 12 from 5 to 2 +move 8 from 8 to 9 +move 8 from 8 to 6 +move 9 from 6 to 2 +move 4 from 9 to 2 +move 1 from 5 to 1 +move 5 from 2 to 1 +move 2 from 5 to 4 +move 5 from 2 to 5 +move 5 from 5 to 6 +move 3 from 4 to 7 +move 11 from 2 to 7 +move 2 from 2 to 1 +move 4 from 3 to 7 +move 2 from 2 to 4 +move 6 from 1 to 4 +move 1 from 2 to 8 +move 2 from 9 to 5 +move 4 from 4 to 3 +move 5 from 4 to 1 +move 2 from 2 to 1 +move 1 from 8 to 5 +move 14 from 7 to 6 +move 3 from 9 to 2 +move 15 from 6 to 8 +move 4 from 1 to 3 +move 2 from 2 to 3 +move 1 from 1 to 7 +move 2 from 3 to 5 +move 4 from 5 to 4 +move 1 from 3 to 5 +move 5 from 1 to 6 +move 12 from 6 to 7 +move 7 from 8 to 4 +move 12 from 7 to 9 +move 4 from 7 to 9 +move 1 from 2 to 8 +move 12 from 9 to 4 +move 23 from 4 to 3 +move 1 from 6 to 5 +move 3 from 9 to 3 +move 1 from 7 to 9 +move 1 from 9 to 1 +move 1 from 9 to 7 +move 42 from 3 to 1 +move 3 from 5 to 4 +move 5 from 1 to 3 +move 3 from 4 to 7 +move 1 from 1 to 9 +move 4 from 3 to 8 +move 1 from 3 to 7 +move 1 from 9 to 1 +move 2 from 7 to 8 +move 8 from 1 to 6 +move 2 from 7 to 5 +move 9 from 1 to 2 +move 5 from 2 to 3 +move 3 from 2 to 4 +move 20 from 1 to 2 +move 1 from 1 to 5 +move 1 from 6 to 7 +move 3 from 4 to 7 +move 2 from 3 to 6 +move 3 from 6 to 1 +move 1 from 6 to 4 +move 2 from 1 to 6 +move 3 from 5 to 9 +move 1 from 4 to 3 +move 2 from 7 to 4 +move 6 from 8 to 4 +move 1 from 1 to 9 +move 1 from 2 to 9 +move 2 from 8 to 7 +move 3 from 6 to 2 +move 5 from 7 to 5 +move 4 from 2 to 5 +move 4 from 4 to 6 +move 3 from 9 to 6 +move 4 from 3 to 1 +move 1 from 9 to 2 +move 7 from 8 to 9 +move 4 from 2 to 4 +move 2 from 1 to 7 +move 3 from 4 to 5 +move 4 from 2 to 4 +move 1 from 7 to 4 +move 4 from 2 to 9 +move 7 from 4 to 3 +move 1 from 7 to 3 +move 6 from 2 to 3 +move 2 from 1 to 5 +move 10 from 3 to 6 +move 2 from 6 to 1 +move 2 from 2 to 7 +move 2 from 3 to 1 +move 1 from 7 to 8 +move 11 from 5 to 3 +move 2 from 3 to 1 +move 4 from 6 to 1 +move 1 from 4 to 6 +move 8 from 3 to 4 +move 2 from 5 to 6 +move 3 from 3 to 5 +move 1 from 8 to 4 +move 1 from 4 to 9 +move 2 from 6 to 1 +move 1 from 5 to 1 +move 9 from 4 to 3 +move 5 from 6 to 9 +move 5 from 6 to 7 +move 13 from 9 to 3 +move 5 from 1 to 8 +move 4 from 8 to 4 +move 10 from 3 to 2 +move 3 from 6 to 1 +move 2 from 7 to 9 +move 1 from 8 to 3 +move 1 from 7 to 3 +move 1 from 9 to 5 +move 1 from 6 to 3 +move 7 from 2 to 4 +move 3 from 5 to 2 +move 8 from 3 to 5 +move 7 from 4 to 3 +move 5 from 9 to 7 +move 1 from 7 to 1 +move 9 from 1 to 8 +move 9 from 5 to 8 +move 2 from 7 to 8 +move 3 from 8 to 1 +move 10 from 3 to 6 +move 1 from 1 to 6 +move 5 from 1 to 7 +move 3 from 2 to 8 +move 7 from 8 to 6 +move 7 from 8 to 6 +move 1 from 3 to 5 +move 5 from 7 to 9 +move 4 from 8 to 4 +move 3 from 2 to 8 +move 1 from 7 to 8 +move 3 from 3 to 9 +move 3 from 7 to 4 +move 1 from 7 to 2 +move 9 from 9 to 1 +move 5 from 1 to 9 +move 4 from 8 to 6 +move 1 from 2 to 7 +move 1 from 5 to 3 +move 1 from 3 to 7 +move 1 from 1 to 9 +move 1 from 1 to 7 +move 5 from 9 to 6 +move 2 from 7 to 6 +move 10 from 4 to 8 +move 1 from 4 to 2 +move 1 from 4 to 1 +move 1 from 9 to 2 +move 3 from 1 to 2 +move 1 from 7 to 3 +move 1 from 2 to 1 +move 16 from 6 to 3 +move 9 from 6 to 1 +move 6 from 6 to 1 +move 5 from 6 to 1 +move 3 from 8 to 1 +move 11 from 3 to 4 +move 1 from 6 to 2 +move 3 from 8 to 2 +move 4 from 1 to 6 +move 5 from 3 to 2 +move 1 from 2 to 5 +move 1 from 8 to 5 +move 5 from 8 to 3 +move 4 from 6 to 9 +move 2 from 9 to 6 +move 3 from 3 to 9 +move 1 from 5 to 7 +move 5 from 1 to 6 +move 3 from 6 to 4 +move 2 from 2 to 9 +move 8 from 4 to 2 +move 9 from 1 to 7 +move 3 from 3 to 5 +move 3 from 5 to 7 +move 12 from 7 to 1 +move 5 from 4 to 6 +move 1 from 4 to 5 +move 7 from 1 to 8 +move 5 from 9 to 3 +move 1 from 7 to 4 +move 10 from 1 to 8 +move 1 from 4 to 8 +move 4 from 6 to 8 +move 1 from 6 to 9 +move 2 from 5 to 1 +move 4 from 3 to 4 +move 1 from 1 to 8 +move 4 from 4 to 7 +move 2 from 1 to 8 +move 4 from 6 to 1 +move 3 from 9 to 5 +move 1 from 6 to 5 +move 1 from 3 to 7 +move 24 from 8 to 6 +move 3 from 6 to 5 +move 4 from 6 to 7 +move 1 from 1 to 7 +move 7 from 7 to 6 +move 7 from 5 to 3 +move 13 from 6 to 8 +move 3 from 1 to 2 +move 7 from 6 to 3 +move 12 from 2 to 4 +move 4 from 6 to 9 +move 6 from 3 to 1 +move 1 from 2 to 4 +move 2 from 8 to 7 +move 2 from 2 to 9 +move 6 from 3 to 4 +move 12 from 8 to 2 +move 18 from 2 to 5 +move 10 from 4 to 3 +move 4 from 7 to 3 +move 5 from 4 to 7 +move 3 from 5 to 2 +move 4 from 7 to 9 +move 1 from 5 to 4 +move 3 from 2 to 1 +move 4 from 3 to 6 +move 7 from 5 to 6 +move 2 from 5 to 7 +move 5 from 1 to 7 +move 9 from 7 to 6 +move 8 from 9 to 8 +move 1 from 1 to 3 +move 1 from 3 to 1 +move 10 from 3 to 9 +move 8 from 8 to 4 +move 1 from 3 to 8 +move 1 from 1 to 3 +move 6 from 9 to 1 +move 5 from 5 to 3 +move 5 from 3 to 6 +move 1 from 8 to 9 +move 19 from 6 to 2 +move 13 from 4 to 1 +move 4 from 1 to 5 +move 6 from 2 to 1 +move 2 from 9 to 4 +move 1 from 3 to 1 +move 9 from 2 to 3 +move 4 from 5 to 1 +move 5 from 9 to 6 +move 4 from 3 to 4 +move 3 from 2 to 7 +move 2 from 4 to 8 +move 6 from 1 to 9 +move 1 from 8 to 6 +move 4 from 1 to 5 +move 3 from 4 to 5 +move 1 from 7 to 2 +move 11 from 1 to 6 +move 1 from 2 to 7 +move 5 from 3 to 7 +move 1 from 3 to 4 +move 1 from 4 to 8 +move 3 from 5 to 6 +move 8 from 1 to 7 +move 1 from 8 to 9 +move 1 from 6 to 9 +move 1 from 8 to 5 +move 11 from 6 to 5 +move 12 from 5 to 2 +move 1 from 5 to 2 +move 8 from 7 to 3 +move 1 from 5 to 6 +move 2 from 5 to 6 +move 3 from 7 to 1 +move 6 from 2 to 6 +move 1 from 3 to 1 +move 1 from 4 to 1 +move 4 from 6 to 2 +move 5 from 1 to 5 +move 10 from 2 to 3 +move 2 from 9 to 4 +move 4 from 5 to 8 +move 2 from 2 to 7 +move 12 from 6 to 7 +move 1 from 8 to 2 +move 10 from 3 to 4 +move 2 from 3 to 5 +move 1 from 3 to 1 diff --git a/src/day5.rs b/src/day5.rs new file mode 100644 index 0000000..90d76d1 --- /dev/null +++ b/src/day5.rs @@ -0,0 +1,134 @@ +//! Day 5: Supply Stacks + +use once_cell::sync::Lazy; +use regex::Regex; + +type Stacks = Vec>; + +fn split_input(input: &[String]) -> (&[String], &[String]) { + let mut parts = input.split(|i| i.is_empty()); + (parts.next().unwrap(), parts.next().unwrap()) +} + +fn parse_stacks(stacks: &[String]) -> Stacks { + let mut lines = stacks.iter().rev(); + let index_line = lines.next().unwrap(); + let n_stacks = index_line.trim().split_ascii_whitespace().count(); + + let mut stacks = (0..n_stacks).map(|_| Vec::new()).collect::>(); + + for line in lines { + for (i, stack) in stacks.iter_mut().enumerate() { + let char_i = i * 4 + 1; + + if let Some(char_str) = line.get(char_i..char_i + 1) { + let c = char_str.chars().next().unwrap(); + if c != ' ' { + if !c.is_ascii_uppercase() { + panic!("invalid crate char: `{}`", c) + } + stack.push(c); + } + } + } + } + stacks +} + +struct Instruction { + n: usize, + from: usize, + to: usize, +} + +fn parse_instruction(instr: &str) -> Instruction { + static INSTR_REGEX: Lazy = + Lazy::new(|| Regex::new("move (\\d+) from (\\d+) to (\\d+)").unwrap()); + let cap = INSTR_REGEX.captures(instr).unwrap(); + + let n = cap.get(1).unwrap().as_str().parse::().unwrap(); + let from = cap.get(2).unwrap().as_str().parse::().unwrap() - 1; + let to = cap.get(3).unwrap().as_str().parse::().unwrap() - 1; + Instruction { n, from, to } +} + +fn apply_instruction(stacks: &mut Stacks, instr: &str) { + let instr = parse_instruction(instr); + + for _ in 0..instr.n { + let c = stacks[instr.from].pop().unwrap(); + stacks[instr.to].push(c); + } +} + +fn apply_instruction2(stacks: &mut Stacks, instr: &str) { + let instr = parse_instruction(instr); + + let si = stacks[instr.from].len() - instr.n; + let mut crates = stacks[instr.from].split_off(si); + stacks[instr.to].append(&mut crates); +} + +pub fn part1(input: Vec) -> String { + let (stacks, instr) = split_input(&input); + let mut stacks = parse_stacks(stacks); + + instr.iter().for_each(|i| apply_instruction(&mut stacks, i)); + + stacks + .iter() + .map(|stack| stack.last().copied().unwrap_or(' ')) + .collect() +} + +pub fn part2(input: Vec) -> String { + let (stacks, instr) = split_input(&input); + let mut stacks = parse_stacks(stacks); + + instr + .iter() + .for_each(|i| apply_instruction2(&mut stacks, i)); + + stacks + .iter() + .map(|stack| stack.last().copied().unwrap_or(' ')) + .collect() +} + +#[cfg(test)] +mod tests { + use super::*; + + fn example_input() -> Vec { + const EXAMPLE: &str = r#" [D] +[N] [C] +[Z] [M] [P] + 1 2 3 + +move 1 from 2 to 1 +move 3 from 1 to 3 +move 2 from 2 to 1 +move 1 from 1 to 2"#; + EXAMPLE.lines().map(str::to_owned).collect() + } + + #[test] + fn t_example_1() { + assert_eq!(part1(example_input()), "CMZ"); + } + + #[test] + fn t_example_2() { + assert_eq!(part2(example_input()), "MCD"); + } + + #[test] + fn t_part1() { + assert_eq!(part1(crate::read_input(5)), "TWSGQHNHL"); + } + + #[test] + fn t_part2() { + assert_eq!(part2(crate::read_input(5)), "JNRSCDWPP"); + } +} diff --git a/src/main.rs b/src/main.rs index 0d32c6f..6bc9146 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,6 +10,7 @@ mod day1; mod day2; mod day3; mod day4; +mod day5; pub(crate) fn read_input(day: u8) -> Vec { let f = @@ -46,7 +47,8 @@ days! { 1, day1, 2, day2, 3, day3, - 4, day4 + 4, day4, + 5, day5 } fn main() { From fa85d1e0932c4afc9005b6b7d008bff27beb4c70 Mon Sep 17 00:00:00 2001 From: ThetaDev Date: Tue, 6 Dec 2022 10:22:39 +0100 Subject: [PATCH 07/10] day 6 --- input/day6.txt | 1 + src/day6.rs | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 4 +++- 3 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 input/day6.txt create mode 100644 src/day6.rs diff --git a/input/day6.txt b/input/day6.txt new file mode 100644 index 0000000..0365fef --- /dev/null +++ b/input/day6.txt @@ -0,0 +1 @@ +jfnjjwbbqttplpvllqgllmdllfmllscssqmqzmmwzznqnwqnwnqnjjbdbpbtbdbzzzljljzjjpccrmmppzfpzfpfnfccfbbcqcrcffblfbftbfbtbwwwmgwmgmnngnllnfllhghcghhjppchcfcnfffllmmqbmmpwwwwlqwwqgqcqsqjqpqzqqdzdtztltslsljjfqfcqqgbqqqghqgqvgvrggqwggrgjgmmnrmmzgmzgzpzjjctcmtcmcnndppcvpvrrwvrvhrvhrhjhnjnvjnjrjggccvffnqqvfqvvnmvmqmfmfqqzfzbfzzzgpzpllrwwnpwpnwnwgwhhrrdnrrdjjzjszsjjbddcdbbvmbmqbqnbqbsqbsqqwbwhwggssdnnmttvnnvmnmhmfhhjchcttzdzdqqszzcwwhhwzhwhphqhcqqsggddfmmvzmzwmwfwzwrrbmrrnwnfnwnlwnwrwfwnnmtnnzwnwdnnbhhrphrhlhwllpmmbcbtbffmqffddjnjwwzpzfpptbbqqwbwzbzjbjmjljblbtlblqqhqbqggrngrgllbmbccmhmqmqwwqcqssqzzfjzjrjnnqrqssfnsnvvtgvvmsvsqqljjbsbrrjllvfvzfzmzhzzhthjhshlslfljfjqjpqpvvmpmhpmhmqqmmdwmddppjlplhlsstlssgnggrbblggffcdfdzzwqqtztqtwqtwtzzsjsbszsbsvbvwwjqjnnpdpccwssvdsdzzqbqbtbtqtmtltltvlvddzwzzfpzpjpgphprpgpqqwppdwpdplddvffcdffvpvqqgvqgvvrfvrvqrrcjcpjjpttftqqvjqvqsvqsvqssdpdbbbmcmscsddbhhgttwhhjlltqllnqntqtsscnntwwhswswlwggldltlttsjszsnznsznzccbtbblplnnmfmqmrrvjvhjhzhnzzgnnhrrdrllblpbllfdfjjssvnssvlsllnqqhwqhhhsgstsjstthrrhrghrhhfmhmwhwrwwsrwrfwwdnntqnnsvnvmnnfvnntztqzqhqnqjnnjfflbfllrsllqhqdqccgvgnvvcwcfccmssqnqhhqrrfrtrvvnjnpjnjjpplmlppvmpphjhppvhvdvssjcjrrtrdrrsvvbbjzzrtztgzghzhccwmccshhzbhhdwdwsdswwlcwllpblpphrppfhfnffrbbcgcmggnvnzzmvvcrrftrftrffcscvcsslbljlglzgzbzczszmsmbmnbbhdhvvsqvqhvvfrfddbpwgvztwwqcpzhhwnhphnrwldjmztsptbbgsqbqqccwbdqzvhfjlfldgphzbfprclgpfztbrgvsvfpghmdchscbdqjqgzvmrtdrfzbhgdvgznjcsmglcfwhdtpsljnvvzjcbbrczwtgpdmgpzhctvbbmvsjzthffsjqhfsdrclpqslbhnmpczwvggpzbjcchfjzjhhgtrmlgnzlndfvzrccgggrpmprbmjbfjjhzrhrtwgqdbgdlqghssrnmtmpvttcqwnwdzhgfnddgbqcsdvzvwqdnmmpwrwhfbqtcpqhvwbczrmjqzsntvdrncwjsmvvwcngrtlwtjmnctwrrtvphbjhlqmgzfsfsrblzzvmzlbhzjhwbdfpncdrfchmrqhspdszcjrnvwtmjzmsmzcdphsdzjgqswwrpdvlpvrdnhplnlmswvcrzlcmbtqtscjfwrnrctrvdqcqzwcvgvpdgrndrgsrvzftwpqjjgjhzwhvrjlqntdtcjdrqzhqlqqdffcgvttlhvwgggnwmdlvghfgjpsmntbvbjbbttrwsljwsrvtmznvqdptpwtdcwtcsfdjlmdqthqggjcptrqhbsbjzqqmvvjmgmppqmjmnjdqvspzlbgzjsjshpslmszqnzghsszpsmpzfcrqqjdwvtbnzstvvjzvtzgpptcmvm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wdpbvngsgzlwvwcqhqjsndznbbdcqqhmjjpqjbsnvwztgmqwdcbbjvcndmhsbvbjnzlbscmgnjcrrwrfdljtcsgmwtffgcjflpzzdcnzvmrbnrjbbmhzqqjtgsrwqmmrhpndwlbnrtrhhpqlmdrcrtdmzsslrmffpftdjvfcpvvhzhjhqtrrsclvtbsccgmmqrjbqgbmpnbzlsncssdhmjppjptvddfgbbnjzjjldjlqjzhhttsclrmsgzctwjqqvtjlfzwgtffgrdjzwdcnrprlcswffghngrqcgsbzqhhvbfjtwcjlrrmbtqjdrgpnbftnmzqnndnqwgrqndlwmjnnspbhjlnzrnptnrmcjhpbfcqpvbchvdwthjlcrfpssgtfbsgfrftcrwttrspbsvzpvcczmdqslcdgfljvtjsdpjnwmdvfzfllrdrbgvpltzlqcrlwbncswhfvrdthspmhfhfdlvpbcqlmjfznhnqblffftgzqrtswnmtnvjprqqhhhvrscvbbzgmnlnprghfdjqbgjppjzjrnclfdssbmgspwcscnlcrrqmtlljrmcwgdgcqwvvjzvsjdjvsspszlcthwzrwqtzdgmqvnlvvzrvrpqqwswzcchncrpnjdmflvmhhwvrrstpvnszfrmvpdtpqpbdmwvvbbpjnwmtststtlcvqdnvqqphzlhhzbbbjssgdcnhlmwrzwvwmcmgrcngqzcnffqzfnvldpdjmsspgpbrzhnszfnljfcrgsjvqjjbstvghlcslhqlzhltpglwffrzfgjghssfgrptbnpbhqnhhfbjsnmsvltqpthdmzzrhrhhmzlplvrtdqfrfrppdpqnllblcfjqpdwznsbrhcncdpmztcrjrfnlwtznrmpbzqsbrqrbnthgfpshrdhnwjmrnsmsfqwdjsmsvhfrbdpjrwcvmdvvmdtfqjgmdsrqtctsdmznngbsrfjvhllgwt diff --git a/src/day6.rs b/src/day6.rs new file mode 100644 index 0000000..e9e7fd7 --- /dev/null +++ b/src/day6.rs @@ -0,0 +1,57 @@ +//! Day 6: Tuning Trouble + +use itertools::Itertools; + +fn get_input(input: &[String]) -> Vec { + input.iter().next().unwrap().chars().collect() +} + +fn find_header(input: &[char], n_unique: usize) -> usize { + let (i_start, _) = input + .windows(n_unique) + .enumerate() + .find(|(_, window)| window.iter().all_unique()) + .unwrap(); + i_start + n_unique +} + +pub fn part1(input: Vec) -> String { + let input = get_input(&input); + find_header(&input, 4).to_string() +} + +pub fn part2(input: Vec) -> String { + let input = get_input(&input); + find_header(&input, 14).to_string() +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn t_example1() { + assert_eq!( + part1(vec!["mjqjpqmgbljsphdztnvjfqwrcgsmlb".to_owned()]), + "7" + ); + } + + #[test] + fn t_example2() { + assert_eq!( + part2(vec!["mjqjpqmgbljsphdztnvjfqwrcgsmlb".to_owned()]), + "19" + ); + } + + #[test] + fn t_part1() { + assert_eq!(part1(crate::read_input(6)), "1300"); + } + + #[test] + fn t_part2() { + assert_eq!(part2(crate::read_input(6)), "3986"); + } +} diff --git a/src/main.rs b/src/main.rs index 6bc9146..9dc4c86 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,6 +11,7 @@ mod day2; mod day3; mod day4; mod day5; +mod day6; pub(crate) fn read_input(day: u8) -> Vec { let f = @@ -48,7 +49,8 @@ days! { 2, day2, 3, day3, 4, day4, - 5, day5 + 5, day5, + 6, day6 } fn main() { From 5b62118782dfec969b5c0e0758b8c97b2ca5a7cb Mon Sep 17 00:00:00 2001 From: ThetaDev Date: Thu, 8 Dec 2022 12:57:35 +0100 Subject: [PATCH 08/10] day 7 --- example/day7.txt | 23 + input/day7.txt | 1101 ++++++++++++++++++++++++++++++++++++++++++++++ src/day7.rs | 143 ++++++ src/main.rs | 17 +- 4 files changed, 1281 insertions(+), 3 deletions(-) create mode 100644 example/day7.txt create mode 100644 input/day7.txt create mode 100644 src/day7.rs diff --git a/example/day7.txt b/example/day7.txt new file mode 100644 index 0000000..09a921e --- /dev/null +++ b/example/day7.txt @@ -0,0 +1,23 @@ +$ cd / +$ ls +dir a +14848514 b.txt +8504156 c.dat +dir d +$ cd a +$ ls +dir e +29116 f +2557 g +62596 h.lst +$ cd e +$ ls +584 i +$ cd .. +$ cd .. +$ cd d +$ ls +4060174 j +8033020 d.log +5626152 d.ext +7214296 k diff --git a/input/day7.txt b/input/day7.txt new file mode 100644 index 0000000..b9cccb9 --- /dev/null +++ b/input/day7.txt @@ -0,0 +1,1101 @@ +$ cd / +$ ls +dir grdd +270251 hjlvwtph.jzv +230026 jzmgcj.gmd +dir nns +dir rrfflbql +$ cd grdd +$ ls +233044 mqbz.fcp +dir nnch +82939 rgtvsqsh.psq +150253 srvg.dth +$ cd nnch +$ ls +4014 mqbz.fcp +$ cd .. +$ cd .. +$ cd nns +$ ls +dir cgbdghtd +dir dnh +dir gjhp +dir jwjm +dir mrpfzd +dir ncvv +dir pfnglqgw +dir tlh +dir vnrhpc +$ cd cgbdghtd +$ ls +276941 jrrcdgz.szm +$ cd .. +$ cd dnh +$ ls +269539 nnch +220637 sjzmpwwb +$ cd .. +$ cd gjhp +$ ls +dir czclvmwc +dir jgtzfsm +$ cd czclvmwc +$ ls +179729 fzqvvlg +67916 pmsdthr.prv +$ cd .. +$ cd jgtzfsm +$ ls +151591 rcggj.nwm +$ cd .. +$ cd .. +$ cd jwjm +$ ls +203559 nnch +$ cd .. +$ cd mrpfzd +$ ls +dir pfnglqgw +204978 qscs.vpq +16184 tbfwpmp.hvl +$ cd pfnglqgw +$ ls +dir wvzw +dir ztl +$ cd wvzw +$ ls +195912 jpn.ndh +143238 nnch.djz +2239 pmsdthr.prv +dir sfqq +$ cd sfqq +$ ls +134913 pthmqd +dir vcdhz +$ cd vcdhz +$ ls +13800 ffhv.jnq +$ cd .. +$ cd .. +$ cd .. +$ cd ztl +$ ls +181007 mqbz.fcp +266517 zbpjz.gbr +$ cd .. +$ cd .. +$ cd .. +$ cd ncvv +$ ls +296494 ccmvjm.bjb +20801 hjr +32494 mqbz.fcp +dir nnch +dir pfnglqgw +dir qpq +dir rftzzmq +dir wgblcl +294511 wrdmgdb.fmh +$ cd nnch +$ ls +dir hjzpfvm +dir ncvv +dir pffr +dir pfnglqgw +dir ssdffgsq +$ cd hjzpfvm +$ ls +304078 mqbz.fcp +dir nnch +146425 nnch.lrw +dir vcfglm +dir zhscvh +$ cd nnch +$ ls +311625 gqmpvplj.vjg +248744 ndfcj +$ cd .. +$ cd vcfglm +$ ls +dir btptvs +dir ghpvzvzp +146354 nnch +$ cd btptvs +$ ls +127157 nqnnwq.rtz +16115 pmsdthr.prv +$ cd .. +$ cd ghpvzvzp +$ ls +dir ncvv +$ cd ncvv +$ ls +236869 pmsdthr.prv +$ cd .. +$ cd .. +$ cd .. +$ cd zhscvh +$ ls +225337 glpz +$ cd .. +$ cd .. +$ cd ncvv +$ ls +dir nljtcssr +dir svjhsvjh +$ cd nljtcssr +$ ls +258085 gqmpvplj.vjg +dir ncvv +dir qmzfgcr +249049 wznr.gbs +dir zvvpqlmq +$ cd ncvv +$ ls +147686 ndfcj.sfr +$ cd .. +$ cd qmzfgcr +$ ls +162245 ndfcj.nlj +$ cd .. +$ cd zvvpqlmq +$ ls +160481 ggnc +$ cd .. +$ cd .. +$ cd svjhsvjh +$ ls +dir bjqbmt +58138 gqmpvplj.vjg +dir nsvf +154398 rrhjs.gch +dir wmvhmlr +$ cd bjqbmt +$ ls +252614 ndfcj.wzg +153886 nnch +214625 zhmdvb +$ cd .. +$ cd nsvf +$ ls +274932 nnch.jfg +$ cd .. +$ cd wmvhmlr +$ ls +137205 nnch +$ cd .. +$ cd .. +$ cd .. +$ cd pffr +$ ls +89895 cwpnzngf.swg +197833 jgv +184768 jjhzddp.fbb +31033 tpngfdsg.brv +$ cd .. +$ cd pfnglqgw +$ ls +228015 ccmvjm.bjb +$ cd .. +$ cd ssdffgsq +$ ls +165887 gqmpvplj.vjg +dir lfq +dir nnch +170828 qjb.mnp +dir ttj +$ cd lfq +$ ls +257411 gqmpvplj.vjg +137375 jzmgcj.gmd +267329 nsbsgd.zvq +$ cd .. +$ cd nnch +$ ls +dir mrfwfq +171186 ndhqthf.jlp +$ cd mrfwfq +$ ls +100447 lsfsh.mvr +$ cd .. +$ cd .. +$ cd ttj +$ ls +49634 ndfcj +dir tqvld +$ cd tqvld +$ ls +dir ndfcj +$ cd ndfcj +$ ls +dir gnzj +80690 pmlnbvj +12843 zbrfmfgj.lzr +$ cd gnzj +$ ls +dir wtpzrpn +$ cd wtpzrpn +$ ls +33840 gqmpvplj.vjg +18122 rrfflbql.vws +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd pfnglqgw +$ ls +289092 fdjfvb +$ cd .. +$ cd qpq +$ ls +126281 ccmvjm.bjb +dir crtzrd +231988 jzmgcj.gmd +203061 mgmmp +272098 mqbz.fcp +dir pfnglqgw +dir qzvcsn +105003 rrfflbql.mcm +$ cd crtzrd +$ ls +162336 fsps +100215 mctd.wsz +67983 mqbz.fcp +281538 mqq.cgz +dir ndfcj +dir rrfflbql +dir wbmtvr +$ cd ndfcj +$ ls +290647 pfjczr.wjc +$ cd .. +$ cd rrfflbql +$ ls +dir mpr +dir ncvv +dir rrfflbql +301818 zml.qfj +$ cd mpr +$ ls +237300 gqmpvplj.vjg +dir pfnglqgw +$ cd pfnglqgw +$ ls +dir brnwdjtg +dir dvqlmzw +248787 pmsdthr.prv +$ cd brnwdjtg +$ ls +256129 ncvv +dir ptztp +$ cd ptztp +$ ls +104775 nnch.wlc +$ cd .. +$ cd .. +$ cd dvqlmzw +$ ls +25407 twgqbrtl +$ cd .. +$ cd .. +$ cd .. +$ cd ncvv +$ ls +dir jvwvsm +62293 jzmgcj.gmd +261836 mqbz.fcp +dir vvvrf +$ cd jvwvsm +$ ls +222978 ccmvjm.bjb +207799 jzmgcj.gmd +dir pcvsvcn +248569 pmsdthr.prv +dir wmd +$ cd pcvsvcn +$ ls +39803 pmsdthr.prv +$ cd .. +$ cd wmd +$ ls +9516 rfstbvj.nhz +$ cd .. +$ cd .. +$ cd vvvrf +$ ls +dir ncvv +dir znwc +$ cd ncvv +$ ls +110667 jzmgcj.gmd +$ cd .. +$ cd znwc +$ ls +182248 ndfcj.crv +$ cd .. +$ cd .. +$ cd .. +$ cd rrfflbql +$ ls +231013 ccmvjm.bjb +dir jlc +dir rrfflbql +79210 ttm.zmw +$ cd jlc +$ ls +28096 ccmvjm.bjb +113156 pmsdthr.prv +$ cd .. +$ cd rrfflbql +$ ls +234558 lbg.bpn +$ cd .. +$ cd .. +$ cd .. +$ cd wbmtvr +$ ls +285832 fqhs +$ cd .. +$ cd .. +$ cd pfnglqgw +$ ls +110965 nnch +195414 pmsdthr.prv +243812 thcpw.jfw +$ cd .. +$ cd qzvcsn +$ ls +279179 gqmpvplj.vjg +191705 mhmlfc.czv +146298 pfnglqgw.ppm +2775 pmsdthr.prv +$ cd .. +$ cd .. +$ cd rftzzmq +$ ls +310418 bddhlvs.rwm +152681 cdznrjl +278447 rrfflbql +dir tmcltf +$ cd tmcltf +$ ls +dir mzr +$ cd mzr +$ ls +24154 ccmvjm.bjb +dir nnch +dir rqsbw +$ cd nnch +$ ls +100523 mqbz.fcp +$ cd .. +$ cd rqsbw +$ ls +64033 czzqg.pcz +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd wgblcl +$ ls +235768 dvdzbgv.vwl +186757 jzmgcj.gmd +$ cd .. +$ cd .. +$ cd pfnglqgw +$ ls +129407 zfphqcsf.cfn +$ cd .. +$ cd tlh +$ ls +89310 jzmgcj.gmd +21486 nwnbbmr.lsq +40023 rdmtp.zsf +$ cd .. +$ cd vnrhpc +$ ls +104731 gqmpvplj.vjg +176015 grn +3646 jzmgcj.gmd +dir ncvv +45414 nfrj.lvq +233767 pfnglqgw.bvf +$ cd ncvv +$ ls +252691 ccmvjm.bjb +dir ncvv +dir vwv +dir wwmwfbf +$ cd ncvv +$ ls +306441 qfhhnmqz.snc +$ cd .. +$ cd vwv +$ ls +56033 rrfflbql +$ cd .. +$ cd wwmwfbf +$ ls +45964 gqmpvplj.vjg +118391 mvwl +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd rrfflbql +$ ls +175540 bcw.sqp +154750 ggncvs.nvn +dir jqzm +dir mgbglnr +192820 ndfcj +dir pfnglqgw +217147 qjng.svz +dir rrfflbql +$ cd jqzm +$ ls +dir ncvv +dir nfcvcddz +242934 rjwlgm +dir wzj +$ cd ncvv +$ ls +298067 pfnglqgw.jdv +$ cd .. +$ cd nfcvcddz +$ ls +261264 gqmpvplj.vjg +19464 mqbz.fcp +121507 ncqhrf +dir ndfcj +58485 ndfcj.vhh +dir rcrzjm +228359 wnftnshq +$ cd ndfcj +$ ls +309008 bwn +$ cd .. +$ cd rcrzjm +$ ls +48178 fgzpwhvt +129342 qns.lnj +$ cd .. +$ cd .. +$ cd wzj +$ ls +91384 gqmpvplj.vjg +dir nnch +dir rzm +$ cd nnch +$ ls +dir sgbwrl +$ cd sgbwrl +$ ls +dir ndfcj +$ cd ndfcj +$ ls +295624 cbmdr +$ cd .. +$ cd .. +$ cd .. +$ cd rzm +$ ls +dir cprj +86746 dfwsj.hqq +dir dljnvq +dir ndfcj +159465 nsglq +202670 pfnglqgw.wbh +29700 rrfflbql.wln +dir vgtftq +$ cd cprj +$ ls +148192 gqmpvplj.vjg +165473 hwp.ltc +$ cd .. +$ cd dljnvq +$ ls +91675 pmsdthr.prv +$ cd .. +$ cd ndfcj +$ ls +83124 rrfflbql.ghs +$ cd .. +$ cd vgtftq +$ ls +186744 mqbz.fcp +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd mgbglnr +$ ls +dir ddtcz +dir ncvv +dir nhpwf +dir rrfflbql +203683 ttbc +dir tvbjgv +dir wvzcq +$ cd ddtcz +$ ls +61553 wfzj +$ cd .. +$ cd ncvv +$ ls +37589 ccmvjm.bjb +87987 dnct +196537 ndfcj.cqg +40448 pmsdthr.prv +$ cd .. +$ cd nhpwf +$ ls +243345 gqmpvplj.vjg +53165 nnch.gfc +dir pfnglqgw +dir vdnnf +$ cd pfnglqgw +$ ls +131411 mhvzv.scz +142119 nnch.gnt +$ cd .. +$ cd vdnnf +$ ls +83904 ccmvjm.bjb +dir czfqdtd +dir dgblftbz +dir jnftbbtm +dir pfbnl +dir pfnglqgw +$ cd czfqdtd +$ ls +dir nnch +$ cd nnch +$ ls +255744 ndfcj.ldv +$ cd .. +$ cd .. +$ cd dgblftbz +$ ls +278883 ncvv.zph +dir pfnglqgw +133315 phns.cmq +130316 sftj +$ cd pfnglqgw +$ ls +174155 vnwtv +$ cd .. +$ cd .. +$ cd jnftbbtm +$ ls +255828 nmln +30605 pfnglqgw +$ cd .. +$ cd pfbnl +$ ls +8603 ndfcj +$ cd .. +$ cd pfnglqgw +$ ls +235364 pmsdthr.prv +$ cd .. +$ cd .. +$ cd .. +$ cd rrfflbql +$ ls +dir gfgbj +$ cd gfgbj +$ ls +215226 jzmgcj.gmd +$ cd .. +$ cd .. +$ cd tvbjgv +$ ls +dir nnch +$ cd nnch +$ ls +dir gmsb +$ cd gmsb +$ ls +dir tlqdvpr +$ cd tlqdvpr +$ ls +130013 hzrq.zrg +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd wvzcq +$ ls +dir ncvv +$ cd ncvv +$ ls +103168 ccmvjm.bjb +18537 ncvv +dir nnch +dir rrfflbql +$ cd nnch +$ ls +20928 ndfcj.lln +$ cd .. +$ cd rrfflbql +$ ls +dir lgfwf +$ cd lgfwf +$ ls +dir fmzqt +dir rrfflbql +$ cd fmzqt +$ ls +301419 gqmpvplj.vjg +$ cd .. +$ cd rrfflbql +$ ls +dir nbvqch +$ cd nbvqch +$ ls +298966 csqvdql.cwr +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd pfnglqgw +$ ls +dir bhmndjpq +203077 fssbcjcm.hvt +dir lfslp +dir ncvv +dir nftdcrl +dir nnch +dir pfnglqgw +dir qfbbnr +dir wttc +$ cd bhmndjpq +$ ls +299744 cmgwwccb.tvv +173562 fpwv +dir hmnbfdtr +dir jqpcs +73425 mqbz.fcp +dir ncvv +dir ndfcj +95707 pmsdthr.prv +dir ptzdv +dir qzhrsnqh +dir sbqg +$ cd hmnbfdtr +$ ls +dir pfnglqgw +dir qmpplbtv +77228 tvpdstcn.zbb +$ cd pfnglqgw +$ ls +dir dwr +dir jmgp +102634 mqbz.fcp +148654 ncvv +257637 ncvv.nzn +286938 rrfflbql +$ cd dwr +$ ls +141669 ndfcj +9012 ptrlq.stq +$ cd .. +$ cd jmgp +$ ls +78473 pfnglqgw +$ cd .. +$ cd .. +$ cd qmpplbtv +$ ls +202948 wjp.rgt +$ cd .. +$ cd .. +$ cd jqpcs +$ ls +290654 fmmcph +8123 zrr.vqm +$ cd .. +$ cd ncvv +$ ls +dir hbrttp +$ cd hbrttp +$ ls +128004 nffzj +$ cd .. +$ cd .. +$ cd ndfcj +$ ls +dir vscwfsl +$ cd vscwfsl +$ ls +251706 snww.dzb +$ cd .. +$ cd .. +$ cd ptzdv +$ ls +113702 nnch +$ cd .. +$ cd qzhrsnqh +$ ls +118758 gqmpvplj.vjg +75504 vcnn.stz +102737 zvv +$ cd .. +$ cd sbqg +$ ls +287663 bhcpslm.wwt +dir bqr +dir czhfphh +39170 fqn +dir gqnts +267314 hlv.ljc +8701 jqpdpg.prz +dir ncvv +211749 psln.pdq +$ cd bqr +$ ls +dir chjfw +dir dgccfvtl +219440 fvfsfz +262276 jzmgcj.gmd +dir ndfcj +294287 pfnglqgw.lwh +163881 rrfflbql +278231 vgjm.rrh +$ cd chjfw +$ ls +134800 hvmvqbz.bqj +$ cd .. +$ cd dgccfvtl +$ ls +155579 lwmqrd.wvp +$ cd .. +$ cd ndfcj +$ ls +144255 ncvv.hrn +236730 ndfcj +dir pfz +$ cd pfz +$ ls +297448 rrfflbql.fdt +$ cd .. +$ cd .. +$ cd .. +$ cd czhfphh +$ ls +dir wmws +$ cd wmws +$ ls +14499 ncvv +$ cd .. +$ cd .. +$ cd gqnts +$ ls +165940 ccmvjm.bjb +dir gjcm +dir hldzdlrl +dir jtnpgg +dir nnch +287896 pmsdthr.prv +$ cd gjcm +$ ls +125716 gqmpvplj.vjg +dir mhjm +197155 msspbg +176407 trtdggnf +$ cd mhjm +$ ls +18749 jzmgcj.gmd +252999 nnch +76392 rrfflbql.mzh +$ cd .. +$ cd .. +$ cd hldzdlrl +$ ls +dir gzpcgj +dir rvvgn +$ cd gzpcgj +$ ls +dir vfz +$ cd vfz +$ ls +183852 pmsdthr.prv +$ cd .. +$ cd .. +$ cd rvvgn +$ ls +143443 ndfcj +$ cd .. +$ cd .. +$ cd jtnpgg +$ ls +dir dhvd +dir hlmgslbs +dir rrfflbql +dir vmqpcm +$ cd dhvd +$ ls +131598 ltr.rph +$ cd .. +$ cd hlmgslbs +$ ls +173562 rrfflbql +$ cd .. +$ cd rrfflbql +$ ls +dir ghvmc +$ cd ghvmc +$ ls +311611 jzmgcj.gmd +$ cd .. +$ cd .. +$ cd vmqpcm +$ ls +192032 mqbz.fcp +$ cd .. +$ cd .. +$ cd nnch +$ ls +112995 ccmvjm.bjb +$ cd .. +$ cd .. +$ cd ncvv +$ ls +106711 dswpw.wgr +46614 jzmgcj.gmd +115391 mqbz.fcp +dir nnch +61970 pmsdthr.prv +dir rrfflbql +$ cd nnch +$ ls +50060 gqjtv.gcs +dir lnmmd +73078 ncvv +49129 tfb +dir vgwpcjrl +dir wnqlrqlf +$ cd lnmmd +$ ls +71780 gqmpvplj.vjg +$ cd .. +$ cd vgwpcjrl +$ ls +8269 zcspgw +$ cd .. +$ cd wnqlrqlf +$ ls +dir wzsvhssb +$ cd wzsvhssb +$ ls +187249 jzmgcj.gmd +$ cd .. +$ cd .. +$ cd .. +$ cd rrfflbql +$ ls +dir bpzmvds +24889 hfnbzcn +dir lqffwfr +274793 nnch.svh +dir rhwm +$ cd bpzmvds +$ ls +298048 tfnqwpj +$ cd .. +$ cd lqffwfr +$ ls +dir mzfpbjtl +$ cd mzfpbjtl +$ ls +217469 jzmgcj.gmd +$ cd .. +$ cd .. +$ cd rhwm +$ ls +54198 gqmpvplj.vjg +dir tlnmwhdt +$ cd tlnmwhdt +$ ls +94380 ndfcj.bvv +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd lfslp +$ ls +dir cmrwvp +dir cnh +311549 jgmf.fbs +dir jtgrbqvj +166298 pbwsqpcg.whf +151437 pfnglqgw.mcj +dir sqlwn +$ cd cmrwvp +$ ls +217666 pmsdthr.prv +172469 vzw.rqw +$ cd .. +$ cd cnh +$ ls +84011 jfb.mpt +$ cd .. +$ cd jtgrbqvj +$ ls +dir dcfpfq +dir ghhs +dir ntbmh +dir pfnglqgw +$ cd dcfpfq +$ ls +159731 pfnglqgw +$ cd .. +$ cd ghhs +$ ls +104713 blwnhcn +$ cd .. +$ cd ntbmh +$ ls +174007 jzmgcj.gmd +dir pfnglqgw +60549 rrfflbql.scj +dir zwcdggd +$ cd pfnglqgw +$ ls +49844 rfdw.pqh +$ cd .. +$ cd zwcdggd +$ ls +135925 ccmvjm.bjb +1135 gqmpvplj.vjg +120968 hmgpcj.nbb +$ cd .. +$ cd .. +$ cd pfnglqgw +$ ls +184937 ccmvjm.bjb +128621 llsjsmg.vtv +dir lsbf +42834 ndfcj.fwq +85391 trrchml.sgp +$ cd lsbf +$ ls +192593 cfdtsfq.sln +289191 nnch.qzj +$ cd .. +$ cd .. +$ cd .. +$ cd sqlwn +$ ls +77962 ccmvjm.bjb +114288 ndfcj +$ cd .. +$ cd .. +$ cd ncvv +$ ls +dir gsd +$ cd gsd +$ ls +14949 jwcp.lmq +$ cd .. +$ cd .. +$ cd nftdcrl +$ ls +185409 jzmgcj.gmd +$ cd .. +$ cd nnch +$ ls +280827 djftt +dir ljt +dir ncvv +dir ndfcj +33396 qvhndl.pwn +136204 qvlc.cbr +14063 rrfflbql.mrq +130666 vscjncbm.sls +$ cd ljt +$ ls +166514 cgrgbpvw +55138 jzmgcj.gmd +$ cd .. +$ cd ncvv +$ ls +dir fhnw +56003 gqtcgszl.vnf +145831 jzmgcj.gmd +dir mwcbd +dir rclnhb +$ cd fhnw +$ ls +28050 gqmpvplj.vjg +$ cd .. +$ cd mwcbd +$ ls +288468 ncvv +$ cd .. +$ cd rclnhb +$ ls +190004 ndjmjbp +$ cd .. +$ cd .. +$ cd ndfcj +$ ls +287125 gqmpvplj.vjg +dir ndfcj +247237 nnch +138902 pfnglqgw +$ cd ndfcj +$ ls +143400 ssvsvffz +$ cd .. +$ cd .. +$ cd .. +$ cd pfnglqgw +$ ls +dir cwwb +dir dtf +97867 mqbz.fcp +$ cd cwwb +$ ls +dir wtz +$ cd wtz +$ ls +300949 zcq +$ cd .. +$ cd .. +$ cd dtf +$ ls +28018 gqmpvplj.vjg +$ cd .. +$ cd .. +$ cd qfbbnr +$ ls +dir bqmdfjp +89207 gjfzv +176709 pmsdthr.prv +246390 rrfflbql.vdl +$ cd bqmdfjp +$ ls +137504 cwz.jdg +9191 ncvv +$ cd .. +$ cd .. +$ cd wttc +$ ls +279516 ccmvjm.bjb +115478 lwnpwdqt.jpj +$ cd .. +$ cd .. +$ cd rrfflbql +$ ls +162371 nnch.pnm diff --git a/src/day7.rs b/src/day7.rs new file mode 100644 index 0000000..7a95e6f --- /dev/null +++ b/src/day7.rs @@ -0,0 +1,143 @@ +//! Day 7: No Space Left On Device + +use std::collections::HashMap; + +enum TermLine<'a> { + CdRoot, + CdUp, + Cd(&'a str), + Dir(&'a str), + File(u32), + None, +} + +fn parse_line(line: &str) -> TermLine { + match line.strip_prefix("$ ") { + Some(cmd) => match cmd.strip_prefix("cd ") { + Some(dir) => match dir { + "/" => TermLine::CdRoot, + ".." => TermLine::CdUp, + _ => TermLine::Cd(dir), + }, + // Ignore ls command + None => TermLine::None, + }, + None => { + let (i_size, i_name) = line.split_once(' ').unwrap(); + if i_size == "dir" { + TermLine::Dir(i_name) + } else { + TermLine::File(i_size.parse().unwrap()) + } + } + } +} + +fn get_dir_size>(input: &[S], dir_path: &[&str]) -> u32 { + let mut path = Vec::new(); + let mut size = 0; + + for line in input { + match parse_line(line.as_ref()) { + TermLine::CdRoot => path.clear(), + TermLine::CdUp => { + path.pop(); + } + TermLine::Cd(dir) => path.push(dir), + TermLine::Dir(name) => { + if path == dir_path { + let mut i_path = path.clone(); + i_path.push(name); + size += get_dir_size(input, &i_path); + } + } + TermLine::File(i_size) => { + if path == dir_path { + size += i_size + } + } + TermLine::None => {} + } + } + size +} + +fn get_dir_sizes>(input: &[S]) -> HashMap, u32> { + let mut dir_sizes: HashMap, u32> = HashMap::new(); + let mut path = Vec::new(); + + for line in input.iter() { + let path_upd = match parse_line(line.as_ref()) { + TermLine::CdRoot => { + path.clear(); + true + } + TermLine::CdUp => { + path.pop(); + true + } + TermLine::Cd(dir) => { + path.push(dir); + true + } + _ => false, + }; + + if path_upd && !dir_sizes.contains_key(&path) { + let dir_size = get_dir_size(input, &path); + dir_sizes.insert(path.clone(), dir_size); + } + } + dir_sizes +} + +pub fn part1(input: Vec) -> String { + let dir_sizes = get_dir_sizes(&input); + dir_sizes + .values() + .filter(|size| size <= &&100_000) + .sum::() + .to_string() +} + +pub fn part2(input: Vec) -> String { + let dir_sizes = get_dir_sizes(&input); + + let total_size = dir_sizes[&Vec::new()]; + let unused = 70_000_000 - total_size; + let to_free = 30_000_000 - unused; + + dir_sizes + .values() + .filter(|size| size >= &&to_free) + .min() + .unwrap() + .to_string() +} + +#[cfg(test)] +mod tests { + use super::*; + + const DAY: u8 = 7; + + #[test] + fn t_example1() { + assert_eq!(part1(crate::read_example(DAY)), "95437"); + } + + #[test] + fn t_example2() { + assert_eq!(part2(crate::read_example(DAY)), "24933642"); + } + + #[test] + fn t_part1() { + assert_eq!(part1(crate::read_input(DAY)), "1084134"); + } + + #[test] + fn t_part2() { + assert_eq!(part2(crate::read_input(DAY)), "6183184"); + } +} diff --git a/src/main.rs b/src/main.rs index 9dc4c86..62e3909 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,7 @@ use std::{ fs::File, io::{BufRead, BufReader}, + path::Path, }; use path_macro::path; @@ -12,10 +13,19 @@ mod day3; mod day4; mod day5; mod day6; +mod day7; pub(crate) fn read_input(day: u8) -> Vec { - let f = - File::open(path!("input" / format!("day{}.txt", day))).expect("could not read input file"); + read_input_file(path!("input" / format!("day{}.txt", day))) +} + +#[cfg(test)] +pub(crate) fn read_example(day: u8) -> Vec { + read_input_file(path!("example" / format!("day{}.txt", day))) +} + +fn read_input_file>(path: P) -> Vec { + let f = File::open(path).expect("could not read input file"); let mut input = BufReader::new(f).lines().flatten().collect::>(); if let Some(last) = input.last() { if last.is_empty() { @@ -50,7 +60,8 @@ days! { 3, day3, 4, day4, 5, day5, - 6, day6 + 6, day6, + 7, day7 } fn main() { From 80e67c25175868eb6f42102144bcb74895e7608d Mon Sep 17 00:00:00 2001 From: ThetaDev Date: Thu, 8 Dec 2022 14:11:09 +0100 Subject: [PATCH 09/10] day 8 --- example/day8.txt | 5 ++ input/day8.txt | 99 +++++++++++++++++++++++++++++++ src/day8.rs | 151 +++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 4 +- 4 files changed, 258 insertions(+), 1 deletion(-) create mode 100644 example/day8.txt create mode 100644 input/day8.txt create mode 100644 src/day8.rs diff --git a/example/day8.txt b/example/day8.txt new file mode 100644 index 0000000..16d6fbd --- /dev/null +++ b/example/day8.txt @@ -0,0 +1,5 @@ +30373 +25512 +65332 +33549 +35390 diff --git a/input/day8.txt b/input/day8.txt new file mode 100644 index 0000000..6186368 --- /dev/null +++ b/input/day8.txt @@ -0,0 +1,99 @@ +222322213033345255533423306545562424165440655115256171674620442636621123532003623343351021112300040 +110200134424131544435511513034235114207220346712122743100173142606016164253614600344452542043234001 +130241122341034230060506400411050451436457131115555076655121366640166143420114243260041315245140033 +044201155341204150154421223651421072455456424262034065175730703125367662022422056423113110423033043 +341242043015141323013444355403421162464354167412604633147554242145501111651130260045523542142405124 +104105335542011255154624541673170452421201174751065233125053535203606402110631631303264231443334523 +022251232401442443240604013073567051751261046006060310057162723056356047312301014332262320405344224 +005045010303234050011604134516553223421500156708426287011206217044163376565240420066564035322425031 +433400302252163605112001002152533254526577025774185772532522645162112005222006334211222615202150023 +514421532420126245053363421750507267400406222575606338538284336516406721212651434025136363121253314 +145500111514555142462447264776571468434327564631361615475570716477174343523375640422633224154214330 +450422253465464405163267335560338812657620448402661157163042574016476542116027110002556401004144041 +315250435323412100266435752414460614818523238015312045767348145251346503446110002404643412031033110 +335554260614516260275514014501773808684115234652573523444781127146772327147027164546041361322264113 +552212024666234517403145053462886425015423537515312180012163268455035077313422213613314122403623422 +554116462330326333717726071743410713076038127356572547895446465004382875747801510560161030046236534 +105512113261674505476760210308840028176731611387575863387841328453867641286570312570624613101433450 +201620055416403673411402684861111672134437556816818962692824522386258176417471541216750515352320263 +100064152502471121126620147804267474177133917793419871348867649139837456117360472552512240365464543 +434125501030234366461354647212882961264276372277577656415297827484543874788435871670574372006541656 +354231632241763433421115602887827646148345796776349994375746935256394267608007744846602344220506426 +320055542235452511286776246551436249838211357522914296165427513242286591157867047217062671166212220 +630533311432024274286282747578525892839185272964722232382686773975896514921876068064231003115130640 +054351445323650230374860016787334957865672953893647532392634515193476444753234751431776670673443625 +122161643553043361465661234215425777375428328842756826885237348622679574815841302214422454334622455 +310361643547534388254741787957515835394388424384634948237897787328779899668951118145613227272341123 +553203043313014483116602739588328966436465926583287463393949837268357287783771610187112515346661056 +361112773030626240167311193287631239264325599932928434546936456455966294955629721437363071113555100 +223116444062065127823414995768335224963787626949338374537976548374532595386885887817322311512135354 +165533456256685465133638322212456677839479985883969576788652889447966945526287513125252251227266360 +050427105764654780569971719539982722683736488643647867988885285677254929533615456260111805471544762 +612564200420147420768231643572583337363778975687346456978779493453467879471982875321111751612056704 +154715031561015145153929571787325853755436888933888338496548994492842954225615713257348715562770051 +042706406641878154761493925944579827297695668447479695968796749364665547438536956376866503052450261 +111103621158778860687333696596964667768654674764978799646936368489433243322422814121081720104310375 +256106134082036385674358835842774753667963458466356667369999749769462758256568366264544861030650514 +412372400235780556155749924663448584664436986744984487994753564686643982758245295828805442657020150 +316402420685442228837832744963276587378457659649458546869353576749393683736838444639170007152747441 +343664452531110164851537867349623666557879764884856546986489797534353938689749649842383748067621444 +004444326256452015911854963372679986857738459895764946977798848396957278774226377383181551011246332 +324213275444575144439872496886847394968576968689979497664689986774356463883323582931740408774664723 +167274127524542565635816457474433733785665667989859545577574597555588393452438866921746374545730347 +312523103467383946589439238265777486435445747964954446957896799994745579737227145455742444466763354 +042101230038108485461717949225836749699798599445858954785786668636987937236256346361252208168136312 +447311762287744692475573737395565466544999496459956989976897778946499374824747773197595161266172226 +371671376376358926187663595697867739555794587855655786889854499686796363666939989643254552166201754 +305514713774776948141752365256767736786987588655596589796495868756859657723267975757697551713050730 +505672367783107579727693596546487757757799568755578685859899884563658799498726941257953878711714017 +457520154136801497456138426564536866785654498777579675857476597474753988679778543462582232418154231 +050570576377124118562748278969974597659546757999795788577798949457569458597349739399679324147816176 +064032506374261599382993468784374444687579995969557585785777758755334674823733669516557164016427124 +346565566106489839877287435227877647875697657779798687889989777455747976752522588271842557284604304 +320630184267887846149779595575797453867985966985895787576699496556963977669453525683498188461017663 +155470636481748966932554775295985386984477788779777769886686888674939686949557211549684318562843364 +355422032736165249425585822655654853976657576675655856568867858693534395443986997713966364825241516 +014307615355116598362643769888978393745848466775686878856446548776874844332797955528420388441472072 +434677415171655145937867232795798374659889895676897986696777754584999873398343952258322518308312370 +614462625176544968398772979677993886597595888444699849445968855456666794598363878647397472370611124 +342426038105820512291673236242588648634466744646845777477557645589764482986857645122398666420720742 +303446222610437533389493227477389695959868644577787974876888684975836425953723362621837202318302515 +101334031274541867475755424764549646588488959767847459756867963388465874367294735519312864601156672 +311162646802122687272848655359448638774475595989694889897479694563583798875942767189780830474311553 +670525052313105734222877229995545497896735689799858955459867784679684938565425586596166033035473164 +625002126405824409197545328243248943664786877896564684896757759666544465836966863943043883874535107 +304504507410725215339562444778866968394554865956559998446945777964348595952748915112623570510705144 +447671532535258771129625534992686968475466769559485845995386695599378847572918425825332176641466062 +022123400381035622672492659655854245887594385374756573547685957733982636227675215225616456802746606 +143231134353837650051751146983252488977634698367786398395985875992826958228379769214124442703715262 +135170610171160182526139534726928582959959837673658879565485535346824864319872955144130017452466064 +102646524660025304032183156274795333553888848798733487439766887562335538983732625811202053035706412 +105313211023023674376715267861525696823925879646687867997695888347896781494452164804031634615425042 +256205106513734680231582333362957987973927998375834773457684994689349817858524813244430142704015520 +021404347726028733646641268434424287525532769279452643745555528837589599615841240070086220361652546 +224024214320367157758819746327682985953929797339659337579259325447482167689237531580534124157434533 +432614136036164731080370551893833148799729789672732794952993828437433612152513202747342230044776363 +314003554513122735473322134512738781597463362529856243299295546734985494922406881657630707442021510 +514504073756450312710340856355119778239528232648473554453443349559599228483527304806846455610024545 +424156167665132531871223763346631626522767446256444526857773846279396461782420177025261412610600431 +440544203354025350033001226782826234581598745799586998494951719551921447963680834604761003456033433 +030261134056327505538138763401733512835785873969133667323138843188738957384853306401550446745105426 +524041041607103210643621736677777415881386586134322795167285489925353353273682055677320161701632002 +532513043015722226170325461521708421659348265984277116386737884679183833325305770247107056515000632 +450631113064541102213228045638208741315429696459599317816954821479256672633762880514531576001505104 +214165164302237003505054233882824157688219632493411326562973319962407073638136757124756516600661503 +304465104303261562212345808766401574871933592693641614758134144886301815682807153126027453244204015 +135203445204521251600013125103586202465438696331861292616228156657585723603452536740112103314030555 +514516400551406327564011243567833388064737417461387480870726483204868682076024701011125332602254113 +421341053633214625706123464303687686511262673301412775315212752511877023033424215721261615303053012 +022044105062611536144720566574153723272833357078078000286705430350220142407371067103103416226230555 +410213236064223263635201443404636873030520630316216081306535811175741246467466645353201136163050545 +303520211553166661506232367472736401455544832872162057805076267517121166516261355403030034621243454 +203254222143353045650002244735005340271145025041572764542706827413452562704066262265323455430133504 +441232355335366123446141420412727113134404853821274587121188823260264625415453362560311464455210441 +414115054424201303442043777071262633223473236654360080370734624214365605172654033011620153152523134 +412115334452334460565220356727405160520263276133202400177553665136056247040612461135000421154322204 +041335455114425155032554320512112112454416721273507036712353407602170012763156424133043141034050241 +030023102524414043125455041405115606664661260240725146305660271437703406461522205621551001134541002 +404443532404034526430106366644217325436253634242603222554235163606432511155652343363021250505540430 +324114252001344342204144440320420641222766650735522654115551234652234245431600152550532022520100132 diff --git a/src/day8.rs b/src/day8.rs new file mode 100644 index 0000000..0814c08 --- /dev/null +++ b/src/day8.rs @@ -0,0 +1,151 @@ +//! Day 8: Treetop Tree House + +type Grid = Vec>; + +fn parse_grid(input: &[String]) -> Grid { + let mut grid = Grid::new(); + + for line in input { + let mut grid_ln = Vec::new(); + for tree in line.chars() { + let tree_h = tree.to_digit(10).unwrap() as u8; + grid_ln.push(tree_h); + } + grid.push(grid_ln); + } + grid +} + +fn is_tree_visible(grid: &Grid, x: usize, y: usize) -> bool { + let h = grid.len(); + let w = grid[0].len(); + let tree_height = grid[y][x]; + + if x == 0 || x == w - 1 || y == 0 || y == h - 1 { + return true; + } + + if (0..x).all(|xi| grid[y][xi] < tree_height) { + return true; + } + + if (x + 1..w).all(|xi| grid[y][xi] < tree_height) { + return true; + } + + if (0..y).all(|yi| grid[yi][x] < tree_height) { + return true; + } + + if (y + 1..h).all(|yi| grid[yi][x] < tree_height) { + return true; + } + + false +} + +fn scenic_score(grid: &Grid, x: usize, y: usize) -> usize { + let h = grid.len(); + let w = grid[0].len(); + let tree_height = grid[y][x]; + + let side_score = |start: i64, end: i64, coord: usize, is_x: bool| { + let mut coord2 = start; + let mut n = 1; + + while coord2 != end { + let h = if is_x { + grid[usize::try_from(coord2).unwrap()][coord] + } else { + grid[coord][usize::try_from(coord2).unwrap()] + }; + + if h >= tree_height { + return n; + } + + if start < end { + coord2 += 1; + } else { + coord2 -= 1; + } + n += 1; + } + start.abs_diff(end) + }; + + let left = side_score(x as i64 - 1, -1, y, false); + let right = side_score(x as i64 + 1, w as i64, y, false); + let up = side_score(y as i64 - 1, -1, x, true); + let down = side_score(y as i64 + 1, h as i64, x, true); + + (left * right * up * down).try_into().unwrap() +} + +pub fn part1(input: Vec) -> String { + let grid = parse_grid(&input); + let mut n: u32 = 0; + + for (y, row) in grid.iter().enumerate() { + for x in 0..row.len() { + if is_tree_visible(&grid, x, y) { + n += 1; + } + } + } + + n.to_string() +} + +pub fn part2(input: Vec) -> String { + let grid = parse_grid(&input); + let mut max_score = 0; + + for (y, row) in grid.iter().enumerate() { + for x in 0..row.len() { + max_score = max_score.max(scenic_score(&grid, x, y)); + } + } + + max_score.to_string() +} + +#[cfg(test)] +mod tests { + use super::*; + + const DAY: u8 = 8; + + #[test] + fn t_example1() { + let input = crate::read_example(DAY); + let grid = parse_grid(&input); + assert!(is_tree_visible(&grid, 4, 0)); + assert!(is_tree_visible(&grid, 1, 1)); + assert!(!is_tree_visible(&grid, 3, 1)); + + assert_eq!(part1(input), "21"); + } + + #[test] + fn tmp() { + let input = crate::read_example(DAY); + let grid = parse_grid(&input); + dbg!(scenic_score(&grid, 2, 3)); + } + + #[test] + fn t_example2() { + assert_eq!(part2(crate::read_example(DAY)), "8"); + } + + #[test] + fn t_part1() { + assert_eq!(part1(crate::read_input(DAY)), "1533"); + } + + #[test] + fn t_part2() { + assert_eq!(part2(crate::read_input(DAY)), "6183184"); + } +} diff --git a/src/main.rs b/src/main.rs index 62e3909..ddcaf30 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,6 +14,7 @@ mod day4; mod day5; mod day6; mod day7; +mod day8; pub(crate) fn read_input(day: u8) -> Vec { read_input_file(path!("input" / format!("day{}.txt", day))) @@ -61,7 +62,8 @@ days! { 4, day4, 5, day5, 6, day6, - 7, day7 + 7, day7, + 8, day8 } fn main() { From 0ed8ae54de2662488df54bdff786603a23ba9c19 Mon Sep 17 00:00:00 2001 From: ThetaDev Date: Fri, 9 Dec 2022 15:12:39 +0100 Subject: [PATCH 10/10] day 9 --- example/day9.txt | 8 + input/day9.txt | 2000 ++++++++++++++++++++++++++++++++++++++++++++++ src/day8.rs | 2 +- src/day9.rs | 258 ++++++ src/main.rs | 6 +- 5 files changed, 2271 insertions(+), 3 deletions(-) create mode 100644 example/day9.txt create mode 100644 input/day9.txt create mode 100644 src/day9.rs diff --git a/example/day9.txt b/example/day9.txt new file mode 100644 index 0000000..9874df2 --- /dev/null +++ b/example/day9.txt @@ -0,0 +1,8 @@ +R 4 +U 4 +L 3 +D 1 +R 4 +D 1 +L 5 +R 2 diff --git a/input/day9.txt b/input/day9.txt new file mode 100644 index 0000000..875fe38 --- /dev/null +++ b/input/day9.txt @@ -0,0 +1,2000 @@ +U 1 +D 1 +R 1 +L 2 +D 2 +U 1 +R 2 +D 2 +U 1 +R 1 +L 1 +D 2 +R 2 +U 2 +L 1 +D 1 +U 1 +L 1 +D 2 +U 1 +D 1 +L 1 +U 2 +R 1 +L 1 +R 1 +L 1 +U 2 +L 2 +U 2 +L 1 +U 2 +D 1 +U 2 +D 1 +L 2 +U 1 +D 1 +U 2 +R 2 +D 2 +R 1 +D 2 +L 1 +U 1 +L 2 +D 2 +L 2 +R 1 +U 1 +L 2 +D 1 +U 1 +R 2 +L 2 +D 1 +R 1 +L 1 +R 2 +U 1 +R 2 +D 1 +U 1 +L 2 +D 1 +R 2 +D 1 +U 2 +R 1 +D 1 +R 2 +U 1 +L 2 +D 2 +U 2 +R 2 +L 1 +D 2 +R 1 +D 1 +U 2 +D 2 +L 2 +R 2 +L 2 +D 1 +R 2 +D 2 +L 1 +U 1 +D 1 +L 2 +U 1 +R 1 +D 1 +U 2 +D 1 +L 2 +D 2 +R 2 +L 2 +R 2 +U 2 +L 1 +R 1 +L 1 +D 2 +R 2 +U 1 +R 1 +D 2 +R 1 +L 1 +R 3 +L 3 +D 1 +R 2 +D 2 +U 1 +D 1 +R 2 +D 1 +L 3 +U 3 +L 1 +U 3 +L 1 +D 2 +R 3 +L 2 +R 1 +U 3 +R 2 +D 3 +L 2 +D 1 +R 1 +L 2 +U 3 +D 2 +U 3 +R 1 +L 2 +D 2 +R 2 +U 3 +D 1 +L 2 +D 1 +R 1 +D 3 +U 1 +R 1 +D 3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+R 14 +L 12 +U 2 diff --git a/src/day8.rs b/src/day8.rs index 0814c08..528d39f 100644 --- a/src/day8.rs +++ b/src/day8.rs @@ -146,6 +146,6 @@ mod tests { #[test] fn t_part2() { - assert_eq!(part2(crate::read_input(DAY)), "6183184"); + assert_eq!(part2(crate::read_input(DAY)), "345744"); } } diff --git a/src/day9.rs b/src/day9.rs new file mode 100644 index 0000000..eac4fe4 --- /dev/null +++ b/src/day9.rs @@ -0,0 +1,258 @@ +//! Day 9: Rope Bridge + +use std::{collections::HashSet, fmt::Debug}; + +use itertools::Itertools; + +#[derive(Default, Clone, Copy, PartialEq, Eq, Hash)] +struct Position { + x: i32, + y: i32, +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +enum Direction { + U, + D, + R, + L, +} + +impl Position { + fn move_dir(self, dir: Direction) -> Self { + match dir { + Direction::U => Self { + x: self.x, + y: self.y + 1, + }, + Direction::D => Self { + x: self.x, + y: self.y - 1, + }, + Direction::R => Self { + x: self.x + 1, + y: self.y, + }, + Direction::L => Self { + x: self.x - 1, + y: self.y, + }, + } + } + + fn dx(self, p2: Position) -> i32 { + self.x - p2.x + } + + fn dy(self, p2: Position) -> i32 { + self.y - p2.y + } +} + +impl Debug for Position { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_fmt(format_args!("{}|{}", self.x, self.y)) + } +} + +fn update_rope_pos(hpos: Position, tpos: Position) -> (Position, Position) { + let dx = hpos.dx(tpos); + let dy = hpos.dy(tpos); + + match (dx.abs() < 2, dy.abs() < 2) { + (true, true) => (hpos, tpos), + (true, false) => ( + hpos, + Position { + x: hpos.x, + y: hpos.y - dy + dy.signum(), + }, + ), + (false, true) => ( + hpos, + Position { + x: hpos.x - dx + dx.signum(), + y: hpos.y, + }, + ), + (false, false) => ( + hpos, + Position { + x: hpos.x - dx + dx.signum(), + y: hpos.y - dy + dy.signum(), + }, + ), + } +} + +fn parse_line(line: &str) -> (Direction, i32) { + let (d_str, n_str) = line.split_once(' ').unwrap(); + let d = match d_str { + "U" => Direction::U, + "D" => Direction::D, + "R" => Direction::R, + "L" => Direction::L, + _ => panic!("invalid dir: {}", d_str), + }; + (d, n_str.parse().unwrap()) +} + +fn print_rope(rope: &[Position]) { + let (min_x, max_x) = match rope.iter().map(|p| p.x).minmax() { + itertools::MinMaxResult::NoElements => return, + itertools::MinMaxResult::OneElement(x) => (x, x), + itertools::MinMaxResult::MinMax(min_x, max_x) => (min_x, max_x), + }; + let (min_y, max_y) = match rope.iter().map(|p| p.y).minmax() { + itertools::MinMaxResult::NoElements => return, + itertools::MinMaxResult::OneElement(y) => (y, y), + itertools::MinMaxResult::MinMax(min_y, max_y) => (min_y, max_y), + }; + + for y in (min_y..=max_y).rev() { + for x in min_x..=max_x { + if let Some((n, _)) = rope.iter().find_position(|p| p == &&Position { x, y }) { + print!("{}", n) + } else { + print!("."); + } + } + println!(); + } + println!(); +} + +pub fn part1(input: Vec) -> String { + let mut visited = HashSet::new(); + + let mut hpos = Position::default(); + let mut tpos = Position::default(); + visited.insert(tpos); + + for (dir, n) in input.iter().map(|line| parse_line(line)) { + for _ in 0..n { + (hpos, tpos) = update_rope_pos(hpos.move_dir(dir), tpos); + visited.insert(tpos); + } + } + + visited.len().to_string() +} + +pub fn part2(input: Vec) -> String { + let mut visited = HashSet::new(); + let rope_len = 10; + + // head of the rope first + let mut rope = (0..rope_len) + .map(|_| Position::default()) + .collect::>(); + visited.insert(rope[rope_len - 1]); + + for (dir, n) in input.iter().map(|line| parse_line(line)) { + for _ in 0..n { + let mut new_rope = Vec::with_capacity(rope_len); + let mut rope_iter = rope.iter(); + let mut hpos = rope_iter.next().copied().unwrap().move_dir(dir); + + for tpos in rope_iter { + let (n_hpos, n_tpos) = update_rope_pos(hpos, *tpos); + new_rope.push(n_hpos); + // Tail pos of this section will be head pos of next section + hpos = n_tpos; + } + new_rope.push(hpos); + rope = new_rope; + visited.insert(rope[rope_len - 1]); + // print_rope(&rope); + } + } + + print_rope(&rope); + visited.len().to_string() +} + +#[cfg(test)] +mod tests { + use super::*; + + use rstest::rstest; + + const DAY: u8 = 9; + + #[rstest] + #[case( + Position {x: 0, y: 0}, + Position {x: 0, y: 0}, + Direction::R, + Position {x: 0, y: 0}, + )] + #[case( + Position {x: 1, y: 0}, + Position {x: 0, y: 0}, + Direction::R, + Position {x: 1, y: 0}, + )] + #[case( + Position {x: 4, y: 1}, + Position {x: 3, y: 0}, + Direction::U, + Position {x: 4, y: 1}, + )] + #[case( + Position {x: 4, y: 4}, + Position {x: 4, y: 3}, + Direction::L, + Position {x: 4, y: 3}, + )] + #[case( + Position {x: 3, y: 4}, + Position {x: 4, y: 3}, + Direction::L, + Position {x: 3, y: 4}, + )] + fn rope_pos( + #[case] hpos: Position, + #[case] tpos: Position, + #[case] dir: Direction, + #[case] exp: Position, + ) { + let (_, tpos) = update_rope_pos(hpos.move_dir(dir), tpos); + assert_eq!(tpos, exp); + } + + #[test] + fn t_example1() { + assert_eq!(part1(crate::read_example(DAY)), "13"); + } + + #[test] + fn t_example2() { + assert_eq!(part2(crate::read_example(DAY)), "1"); + } + + #[test] + fn t_example2b() { + let input = vec![ + "R 5".to_owned(), + "U 8".to_owned(), + "L 8".to_owned(), + "D 3".to_owned(), + "R 17".to_owned(), + "D 10".to_owned(), + "L 25".to_owned(), + "U 20".to_owned(), + ]; + assert_eq!(part2(input), "36"); + } + + #[test] + fn t_part1() { + assert_eq!(part1(crate::read_input(DAY)), "6332"); + } + + #[test] + fn t_part2() { + assert_eq!(part2(crate::read_input(DAY)), "2511"); + } +} diff --git a/src/main.rs b/src/main.rs index ddcaf30..3579986 100644 --- a/src/main.rs +++ b/src/main.rs @@ -15,6 +15,7 @@ mod day5; mod day6; mod day7; mod day8; +mod day9; pub(crate) fn read_input(day: u8) -> Vec { read_input_file(path!("input" / format!("day{}.txt", day))) @@ -37,7 +38,7 @@ fn read_input_file>(path: P) -> Vec { } macro_rules! days { - ( $($n:expr, $module:ident),* ) => { + ( $($n:expr, $module:ident,)* ) => { fn get_result(day: u8, part: u8) -> String { let input = read_input(day); @@ -63,7 +64,8 @@ days! { 5, day5, 6, day6, 7, day7, - 8, day8 + 8, day8, + 9, day9, } fn main() {