diff --git a/day_05/Cargo.lock b/day_05/Cargo.lock new file mode 100644 index 0000000..5cd7ebc --- /dev/null +++ b/day_05/Cargo.lock @@ -0,0 +1,639 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "aho-corasick" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b2969dcb958b36655471fc61f7e416fa76033bdd4bfed0678d8fee1e2d07a1f0" +dependencies = [ + "memchr", +] + +[[package]] +name = "anes" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4b46cbb362ab8752921c97e041f5e366ee6297bd428a31275b9fcf1e380f7299" + +[[package]] +name = "atty" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" +dependencies = [ + "hermit-abi", + "libc", + "winapi", +] + +[[package]] +name = "autocfg" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" + +[[package]] +name = "bitflags" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" + +[[package]] +name = "bumpalo" +version = "3.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f30e7476521f6f8af1a1c4c0b8cc94f0bee37d91763d0ca2665f299b6cd8aec" + +[[package]] +name = "cast" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "37b2a672a2cb129a2e41c10b1224bb368f9f37a2b16b612598138befd7b37eb5" + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "ciborium" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "effd91f6c78e5a4ace8a5d3c0b6bfaec9e2baaef55f3efc00e45fb2e477ee926" +dependencies = [ + "ciborium-io", + "ciborium-ll", + "serde", +] + +[[package]] +name = "ciborium-io" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cdf919175532b369853f5d5e20b26b43112613fd6fe7aee757e35f7a44642656" + +[[package]] +name = "ciborium-ll" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "defaa24ecc093c77630e6c15e17c51f5e187bf35ee514f4e2d67baaa96dae22b" +dependencies = [ + "ciborium-io", + "half", +] + +[[package]] +name = "clap" +version = "3.2.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ea181bf566f71cb9a5d17a59e1871af638180a18fb0035c92ae62b705207123" +dependencies = [ + "bitflags", + "clap_lex", + "indexmap", + "textwrap", +] + +[[package]] +name = "clap_lex" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2850f2f5a82cbf437dd5af4d49848fbdfc27c157c3d010345776f952765261c5" +dependencies = [ + "os_str_bytes", +] + +[[package]] +name = "criterion" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7c76e09c1aae2bc52b3d2f29e13c6572553b30c4aa1b8a49fd70de6412654cb" +dependencies = [ + "anes", + "atty", + "cast", + "ciborium", + "clap", + "criterion-plot", + "itertools", + "lazy_static", + "num-traits", + "oorandom", + "plotters", + "rayon", + "regex", + "serde", + "serde_derive", + "serde_json", + "tinytemplate", + "walkdir", +] + +[[package]] +name = "criterion-plot" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6b50826342786a51a89e2da3a28f1c32b06e387201bc2d19791f622c673706b1" +dependencies = [ + "cast", + "itertools", +] + +[[package]] +name = "crossbeam-deque" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce6fd6f855243022dcecf8702fef0c297d4338e226845fe067f6341ad9fa0cef" +dependencies = [ + "cfg-if", + "crossbeam-epoch", + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-epoch" +version = "0.9.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae211234986c545741a7dc064309f67ee1e5ad243d0e48335adc0484d960bcc7" +dependencies = [ + "autocfg", + "cfg-if", + "crossbeam-utils", + "memoffset", + "scopeguard", +] + +[[package]] +name = "crossbeam-utils" +version = "0.8.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a22b2d63d4d1dc0b7f1b6b2747dd0088008a9be28b6ddf0b1e7d335e3037294" +dependencies = [ + "cfg-if", +] + +[[package]] +name = "day_05" +version = "0.1.0" +dependencies = [ + "criterion", + "nom", + "strum", + "strum_macros", +] + +[[package]] +name = "either" +version = "1.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a26ae43d7bcc3b814de94796a5e736d4029efb0ee900c12e2d54c993ad1a1e07" + +[[package]] +name = "half" +version = "1.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eabb4a44450da02c90444cf74558da904edde8fb4e9035a9a6a4e15445af0bd7" + +[[package]] +name = "hashbrown" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" + +[[package]] +name = "heck" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" + +[[package]] +name = "hermit-abi" +version = "0.1.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" +dependencies = [ + "libc", +] + +[[package]] +name = "indexmap" +version = "1.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bd070e393353796e801d209ad339e89596eb4c8d430d18ede6a1cced8fafbd99" +dependencies = [ + "autocfg", + "hashbrown", +] + +[[package]] +name = "itertools" +version = "0.10.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b0fd2260e829bddf4cb6ea802289de2f86d6a7a690192fbe91b3f46e0f2c8473" +dependencies = [ + "either", +] + +[[package]] +name = "itoa" +version = "1.0.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "af150ab688ff2122fcef229be89cb50dd66af9e01a4ff320cc137eecc9bacc38" + +[[package]] +name = "js-sys" +version = "0.3.66" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cee9c64da59eae3b50095c18d3e74f8b73c0b86d2792824ff01bbce68ba229ca" +dependencies = [ + "wasm-bindgen", +] + +[[package]] +name = "lazy_static" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" + +[[package]] +name = "libc" +version = "0.2.150" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "89d92a4743f9a61002fae18374ed11e7973f530cb3a3255fb354818118b2203c" + +[[package]] +name = "log" +version = "0.4.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f" + +[[package]] +name = "memchr" +version = "2.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f665ee40bc4a3c5590afb1e9677db74a508659dfd71e126420da8274909a0167" + +[[package]] +name = "memoffset" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a634b1c61a95585bd15607c6ab0c4e5b226e695ff2800ba0cdccddf208c406c" +dependencies = [ + "autocfg", +] + +[[package]] +name = "minimal-lexical" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" + +[[package]] +name = "nom" +version = "7.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d273983c5a657a70a3e8f2a01329822f3b8c8172b73826411a55751e404a0a4a" +dependencies = [ + "memchr", + "minimal-lexical", +] + +[[package]] +name = "num-traits" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "39e3200413f237f41ab11ad6d161bc7239c84dcb631773ccd7de3dfe4b5c267c" +dependencies = [ + "autocfg", +] + +[[package]] +name = "once_cell" +version = "1.18.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dd8b5dd2ae5ed71462c540258bedcb51965123ad7e7ccf4b9a8cafaa4a63576d" + +[[package]] +name = "oorandom" +version = "11.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ab1bc2a289d34bd04a330323ac98a1b4bc82c9d9fcb1e66b63caa84da26b575" + +[[package]] +name = "os_str_bytes" +version = "6.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2355d85b9a3786f481747ced0e0ff2ba35213a1f9bd406ed906554d7af805a1" + +[[package]] +name = "plotters" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2c224ba00d7cadd4d5c660deaf2098e5e80e07846537c51f9cfa4be50c1fd45" +dependencies = [ + "num-traits", + "plotters-backend", + "plotters-svg", + "wasm-bindgen", + "web-sys", +] + +[[package]] +name = "plotters-backend" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9e76628b4d3a7581389a35d5b6e2139607ad7c75b17aed325f210aa91f4a9609" + +[[package]] +name = "plotters-svg" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38f6d39893cca0701371e3c27294f09797214b86f1fb951b89ade8ec04e2abab" +dependencies = [ + "plotters-backend", +] + +[[package]] +name = "proc-macro2" +version = "1.0.70" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "39278fbbf5fb4f646ce651690877f89d1c5811a3d4acb27700c1cb3cdb78fd3b" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.33" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "rayon" +version = "1.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c27db03db7734835b3f53954b534c91069375ce6ccaa2e065441e07d9b6cdb1" +dependencies = [ + "either", + "rayon-core", +] + +[[package]] +name = "rayon-core" +version = "1.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5ce3fb6ad83f861aac485e76e1985cd109d9a3713802152be56c3b1f0e0658ed" +dependencies = [ + "crossbeam-deque", + "crossbeam-utils", +] + +[[package]] +name = "regex" +version = "1.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "380b951a9c5e80ddfd6136919eef32310721aa4aacd4889a8d39124b026ab343" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5f804c7828047e88b2d32e2d7fe5a105da8ee3264f01902f796c8e067dc2483f" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f" + +[[package]] +name = "rustversion" +version = "1.0.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7ffc183a10b4478d04cbbbfc96d0873219d962dd5accaff2ffbd4ceb7df837f4" + +[[package]] +name = "ryu" +version = "1.0.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ad4cc8da4ef723ed60bced201181d83791ad433213d8c24efffda1eec85d741" + +[[package]] +name = "same-file" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502" +dependencies = [ + "winapi-util", +] + +[[package]] +name = "scopeguard" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" + +[[package]] +name = "serde" +version = "1.0.193" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "25dd9975e68d0cb5aa1120c288333fc98731bd1dd12f561e468ea4728c042b89" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.193" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "43576ca501357b9b071ac53cdc7da8ef0cbd9493d8df094cd821777ea6e894d3" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "serde_json" +version = "1.0.108" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3d1c7e3eac408d115102c4c24ad393e0821bb3a5df4d506a80f85f7a742a526b" +dependencies = [ + "itoa", + "ryu", + "serde", +] + +[[package]] +name = "strum" +version = "0.25.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "290d54ea6f91c969195bdbcd7442c8c2a2ba87da8bf60a7ee86a235d4bc1e125" + +[[package]] +name = "strum_macros" +version = "0.25.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23dc1fa9ac9c169a78ba62f0b841814b7abae11bdd047b9c58f893439e309ea0" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "rustversion", + "syn", +] + +[[package]] +name = "syn" +version = "2.0.39" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23e78b90f2fcf45d3e842032ce32e3f2d1545ba6636271dcbf24fa306d87be7a" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "textwrap" +version = "0.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "222a222a5bfe1bba4a77b45ec488a741b3cb8872e5e499451fd7d0129c9c7c3d" + +[[package]] +name = "tinytemplate" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "be4d6b5f19ff7664e8c98d03e2139cb510db9b0a60b55f8e8709b689d939b6bc" +dependencies = [ + "serde", + "serde_json", +] + +[[package]] +name = "unicode-ident" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" + +[[package]] +name = "walkdir" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d71d857dc86794ca4c280d616f7da00d2dbfd8cd788846559a6813e6aa4b54ee" +dependencies = [ + "same-file", + "winapi-util", +] + +[[package]] +name = "wasm-bindgen" +version = "0.2.89" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ed0d4f68a3015cc185aff4db9506a015f4b96f95303897bfa23f846db54064e" +dependencies = [ + "cfg-if", + "wasm-bindgen-macro", +] + +[[package]] +name = "wasm-bindgen-backend" +version = "0.2.89" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b56f625e64f3a1084ded111c4d5f477df9f8c92df113852fa5a374dbda78826" +dependencies = [ + "bumpalo", + "log", + "once_cell", + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-macro" +version = "0.2.89" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0162dbf37223cd2afce98f3d0785506dcb8d266223983e4b5b525859e6e182b2" +dependencies = [ + "quote", + "wasm-bindgen-macro-support", +] + +[[package]] +name = "wasm-bindgen-macro-support" +version = "0.2.89" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0eb82fcb7930ae6219a7ecfd55b217f5f0893484b7a13022ebb2b2bf20b5283" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-backend", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-shared" +version = "0.2.89" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7ab9b36309365056cd639da3134bf87fa8f3d86008abf99e612384a6eecd459f" + +[[package]] +name = "web-sys" +version = "0.3.66" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "50c24a44ec86bb68fbecd1b3efed7e85ea5621b39b35ef2766b66cd984f8010f" +dependencies = [ + "js-sys", + "wasm-bindgen", +] + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-util" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f29e6f9198ba0d26b4c9f07dbe6f9ed633e1f3d5b8b414090084349e46a52596" +dependencies = [ + "winapi", +] + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" diff --git a/day_05/Cargo.toml b/day_05/Cargo.toml new file mode 100644 index 0000000..9acf0e3 --- /dev/null +++ b/day_05/Cargo.toml @@ -0,0 +1,22 @@ +[package] +name = "day_05" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +nom = "7.1.3" +strum = "0.25.0" +strum_macros = "0.25.3" + +[dev-dependencies] +criterion = { version = "0.4", features = ["html_reports"] } + +[[bench]] +name = "part1" +harness = false + +[[bench]] +name = "part2" +harness = false diff --git a/day_05/benches/part1.rs b/day_05/benches/part1.rs new file mode 100644 index 0000000..5e2452c --- /dev/null +++ b/day_05/benches/part1.rs @@ -0,0 +1,11 @@ +use criterion::{black_box, criterion_group, criterion_main, Criterion}; + +use day_05::prelude::*; + +fn criterion_benchmark(c: &mut Criterion) { + let input = include_str!("../input.txt"); + c.bench_function("part 1", |b| b.iter(|| part1(black_box(input)))); +} + +criterion_group!(benches, criterion_benchmark); +criterion_main!(benches); diff --git a/day_05/benches/part2.rs b/day_05/benches/part2.rs new file mode 100644 index 0000000..4dd3025 --- /dev/null +++ b/day_05/benches/part2.rs @@ -0,0 +1,11 @@ +use criterion::{black_box, criterion_group, criterion_main, Criterion}; + +use day_05::prelude::*; + +fn criterion_benchmark(c: &mut Criterion) { + let input = include_str!("../input.txt"); + c.bench_function("part 2", |b| b.iter(|| part2(black_box(input)))); +} + +criterion_group!(benches, criterion_benchmark); +criterion_main!(benches); diff --git a/day_05/input.txt b/day_05/input.txt new file mode 100644 index 0000000..d612f11 --- /dev/null +++ b/day_05/input.txt @@ -0,0 +1,249 @@ +seeds: 763445965 78570222 1693788857 146680070 1157620425 535920936 3187993807 180072493 1047354752 20193861 2130924847 274042257 20816377 596708258 950268560 11451287 3503767450 182465951 3760349291 265669041 + +seed-to-soil map: +0 1894195346 315486903 +1184603419 2977305241 40929361 +1225532780 597717 4698739 +1988113706 1603988885 78481073 +679195301 529385087 505408118 +1781158512 2285166785 39457705 +352613463 2324624490 326581838 +1820616217 1738931330 104130014 +2066594779 2671974456 78036460 +1288754536 1682469958 56461372 +1371340411 3442489267 409818101 +3341036988 1092718505 511270380 +315486903 1857068786 37126560 +1924746231 2209682249 49360033 +1345215908 2259042282 26124503 +2917167497 2651206328 20768128 +1230231519 1034793205 57925300 +2144631239 3421335965 21153302 +2689873172 2750010916 227294325 +1974106264 1843061344 14007442 +2165784541 5296456 524088631 +1288156819 0 597717 +2937935625 3018234602 403101363 + +soil-to-fertilizer map: +3849355959 3101885274 170573538 +3006688391 3025445580 66663107 +1656285214 0 92188755 +2361256790 4033128077 180329908 +2073349245 3676064680 287907545 +3458323699 4213457985 81509311 +2607585697 2380345050 99923790 +2541586698 3332501375 61727497 +3789313396 3272458812 60042563 +1858319884 917612384 162600389 +404885474 510011966 407600418 +4019929497 3603127472 72937208 +812485892 1190058688 253881561 +2603314195 2073349245 4271502 +1469824760 92188755 186460454 +0 278649209 231362757 +1249323571 1443940249 220501189 +4285190709 3092108687 9776587 +1066367453 1817839802 182956118 +3257369783 2724171240 184379320 +2707509487 2480268840 111317034 +2818826521 2192483180 187861870 +1748473969 1080212773 109845915 +3441749103 3394228872 16574596 +3142507350 2077620747 114862433 +3539833010 2908550560 116895020 +231362757 1664441438 153398364 +384761121 2000795920 20124353 +3656728030 2591585874 132585366 +3073351498 3963972225 69155852 +4092866705 3410803468 192324004 + +fertilizer-to-water map: +2289944373 962149121 35670423 +3187295333 3295892606 34862460 +3798694276 3187295333 48036047 +3633796172 3644673845 164898104 +3222157793 3909959418 311250910 +922447685 2248438960 142418952 +648510907 2390857912 2187794 +3533408703 3809571949 100387469 +650698701 1840321280 271748984 +464371310 553340958 30176545 +3907291549 3330755066 313918779 +2791296727 461310335 25110510 +397451197 486420845 66920113 +3846730323 3235331380 60561226 +2575399801 2393045706 96151163 +1064866637 583517503 106955439 +1308190772 822897256 139251865 +0 236670371 224639964 +2671550964 0 119745763 +1171822076 2112070264 136368696 +224639964 690472942 132424314 +494547855 2489196869 153963052 +2498862112 160132682 76537689 +1447442637 997819544 842501736 +357064278 119745763 40386919 +2325614796 2643159921 173247316 + +water-to-light map: +3181220930 3836164803 159006567 +1405924027 2473049234 39930303 +1275636734 1411161599 10830099 +3831476263 1580605379 266491658 +1570444205 1294782382 116379217 +3133505036 2271845049 47715894 +1305612481 2512979537 100311546 +1286466833 1275636734 19145648 +167024508 145118817 64336111 +92584517 615923894 40913901 +2227655152 2098041405 130620812 +2181794752 2613291083 34326669 +433451288 209454928 64553504 +4223684323 2228662217 43182832 +3527642272 3532330812 303833991 +2855274869 2647617752 119788915 +4266867155 2319560943 28100141 +231360619 1167459710 18705935 +2136463524 3995171370 45331228 +2978600831 1421991698 154904205 +4097967921 2825698355 78160170 +0 489813287 92584517 +1686823422 3023927616 328136942 +2014960364 2903858525 116532044 +1512152517 2767406667 58291688 +395185371 1129193793 38265917 +498004792 274008432 215804855 +2975063784 3020390569 3537047 +3340227497 3352064558 180266254 +3524203227 4040502598 3439045 +713809647 656837795 472355998 +1445854330 2395217316 66298187 +3520493751 1576895903 3709476 +4176128091 2347661084 47556232 +2216121421 2461515503 11533731 +2604330501 1847097037 250944368 +2358275964 4043941643 246054537 +133498418 582397804 33526090 +2131492408 4289996180 4971116 +250066554 0 145118817 + +light-to-temperature map: +1212130960 1395832798 58636484 +542019293 812004214 195851408 +1706861165 2688952641 3198757 +3097592274 3018951608 116723898 +1148165381 3135675506 2936711 +938881022 300892136 26815610 +2884354982 3318488806 103553821 +789335095 421331130 149545927 +3270968199 1036156523 69989778 +1142047574 1030038716 6117807 +1710059922 1367448764 28384034 +3961474833 1257923390 109525374 +300892136 570877057 241127157 +2564647329 3422042627 200129932 +3534803741 2001828127 16273450 +1738443956 1454469282 486329977 +3717055162 2018101577 244419671 +4071000207 2781125469 223967089 +1030038716 3005092558 13859050 +737870701 358179343 20992797 +3388483069 4209077325 84901810 +3551077191 3622172559 62988195 +3614065386 2769635501 11489968 +3215304333 3262824940 55663866 +2278270544 2395185346 286376785 +1151102092 1940799259 61028868 +3625555354 4044566844 91499808 +2987908803 1106146301 109683471 +1134657064 2681562131 7390510 +2236365744 2353280546 41904800 +1270767444 3685160754 311880998 +1043897766 2262521248 90759298 +2806870879 2692151398 77484103 +3340957977 3997041752 47525092 +3214316172 4293979135 988161 +2764777261 1215829772 42093618 +965696632 379172140 42158990 +3473384879 4147658463 61418862 +2224773933 4136066652 11591811 +1582648442 3201806221 61018719 +758863498 327707746 30471597 +1643667161 3138612217 63194004 + +temperature-to-humidity map: +3893671309 2897869925 193525827 +1232495182 999263760 191357681 +2440090529 1477966061 163000786 +3554817869 2745268041 3258240 +2845194314 3091395752 30017739 +4087197136 1397536284 33207741 +131252236 827398049 11627380 +2183328852 2476037434 27127608 +2603091315 2503165042 242102999 +2875212053 1778930312 348370034 +1427151141 891672290 77843721 +1642958327 3928570372 200739395 +3610489285 3219006125 283182024 +3277129360 3580618829 128344865 +2233175686 1190621441 206914843 +891672290 4129309767 165657529 +0 205218291 35419402 +3584280491 3551111757 26208794 +4247745260 1430744025 47222036 +2210456460 3502188149 21171921 +1843697722 2136406304 209010485 +1423852863 3577320551 3298278 +348097907 336470527 490927522 +1057329819 3708963694 175165363 +2231628381 3549564452 1547305 +3268023402 2127300346 9105958 +142879616 0 205218291 +3558076109 3523360070 26204382 +2052708207 2345416789 130620645 +4120404877 969516011 29747749 +35419402 240637693 95832834 +4150152626 3121413491 97592634 +3223582087 3884129057 44441315 +1504994862 1640966847 137963465 +3405474225 2748526281 149343644 + +humidity-to-location map: +3554305993 3441523152 94289982 +4052786034 3015714705 242181262 +491965155 895492593 72049890 +2437667299 3726944956 38441480 +0 387558480 141314689 +3811971022 3765386436 101972362 +3648595975 3535813134 51583790 +1318420824 1500487572 135197760 +230758172 30039204 163596268 +3097413876 4221249785 73717511 +3041068351 3257895967 56345525 +1454254795 641271247 181430537 +394354440 1139828795 97610715 +3215016925 2618366793 339289068 +827848339 193635472 113349295 +150184459 306984767 80573713 +1778801395 2099048921 519317872 +1176891800 1379491771 111489820 +1288381620 0 30039204 +3171131387 3891749067 43885538 +564015045 528873169 112398078 +3913943384 3935634605 11560990 +2767014161 3947195595 274054190 +941197634 822701784 72790809 +1453618584 1499851361 636211 +2298119267 3587396924 139548032 +3700179765 3867358798 24390269 +3742730647 1778801395 11181531 +1013988443 967542483 20851096 +141314689 1490981591 8869770 +3925504374 3314241492 127281660 +2476108779 2088802749 10246172 +676413123 988393579 151435216 +3753912178 2957655861 58058844 +2486354951 1789982926 280659210 +3724570034 2070642136 18160613 +1034839539 1237439510 142052261 diff --git a/day_05/src/lib.rs b/day_05/src/lib.rs new file mode 100644 index 0000000..8549a7a --- /dev/null +++ b/day_05/src/lib.rs @@ -0,0 +1,138 @@ +use nom::{ + bytes::complete::tag, + character::complete::{ + self, space0, space1, alpha1, newline + }, + multi::separated_list1, + sequence::{preceded, terminated, tuple, separated_pair}, + IResult, + +}; +use std::str::FromStr; + +extern crate strum; +#[macro_use] extern crate strum_macros; + +#[derive(Debug, Eq, PartialEq, EnumString)] +#[strum(serialize_all = "lowercase")] +enum Item { + Seed, + Soil, + Fertilizer, + Water, + Light, + Temperature, + Humidity, + Location, +} + +#[derive(Debug, Eq, PartialEq)] +struct ItemMap { + before: u64, + after: u64, + length: u64, +} + +impl ItemMap { + pub fn convert(&self, num: u64) -> Option { + if self.before <= num && num < self.before + self.length { + Some(self.after + num - self.before) + } else { + None + } + } +} + +#[derive(Debug, Eq, PartialEq)] +struct FullMap { + in_type: Item, + out_type: Item, + maps: Vec +} + +impl FullMap { + pub fn convert(&self, num: u64) -> u64 { + let mut try_convert = self.maps.iter().filter_map(|item_map| { + item_map.convert(num) + }); + match try_convert.next() { + Some(output) => output, + None => num, + } + } +} + +// 50 98 2 +fn parse_item_map(input: &str) -> IResult<&str, ItemMap> { + let (input, (after, before, length)) = tuple(( + terminated(complete::u64,space0), + terminated(complete::u64,space0), + terminated(complete::u64,space0) + ))(input)?; + Ok( (input, ItemMap{ before, after, length }) ) +} + +// seed-to-soil map: +// 50 98 2 +// 52 50 48 +fn parse_full_map(input: &str) -> IResult<&str, FullMap> { + let (input, (in_type_string, out_type_string)) = terminated(separated_pair(alpha1, tag("-to-"), alpha1), tag(" map:\n"))(input)?; + let (input, maps) = separated_list1(newline, parse_item_map)(input)?; + let in_type = Item::from_str(in_type_string).unwrap(); + let out_type = Item::from_str(out_type_string).unwrap(); + Ok( (input, FullMap { in_type, out_type, maps }) ) +} + +// seeds: 79 14 55 13 +fn parse_seeds(input: &str) -> IResult<&str, Vec> { + let (input, seeds) = preceded(tag("seeds: "), separated_list1(space1, complete::u64))(input)?; + Ok((input, seeds)) +} + +fn parse_seeds_alt(input: &str) -> IResult<&str, Vec> { + let (input, seed_ranges) = preceded( + tag("seeds: "), + separated_list1(space1, separated_pair(complete::u64, space1, complete::u64)) + )(input)?; + let mut seeds = vec![]; + seed_ranges.into_iter().for_each(|(start, length)| { (start..start+length).for_each(|x| {seeds.push(x);}) }); + Ok((input, seeds)) +} + +fn parse_all(input: &str) -> IResult<&str, (Vec, Vec)> { + let (input, seeds) = terminated(parse_seeds, tuple((newline, newline)))(input)?; + let (input, full_maps) = separated_list1(tuple((newline,newline)), parse_full_map)(input)?; + Ok((input, (seeds, full_maps))) +} + +fn parse_all_alt(input: &str) -> IResult<&str, (Vec, Vec)> { + let (input, seeds) = terminated(parse_seeds_alt, tuple((newline, newline)))(input)?; + dbg!(seeds.len()); + let (input, full_maps) = separated_list1(tuple((newline,newline)), parse_full_map)(input)?; + Ok((input, (seeds, full_maps))) +} + +pub fn part1(input: &str) -> String { + let (_, (seeds, full_maps)) = parse_all(input).unwrap(); + seeds.into_iter().map(|seed| { + full_maps.iter().fold(seed, |value, x| { + x.convert(value) + }) + }).min().unwrap().to_string() +} + +pub fn part2(input: &str) -> String { + let (_, (seeds, full_maps)) = parse_all_alt(input).unwrap(); + seeds.into_iter().enumerate().map(|(i,seed)| { + if i % 1_000_000 == 0 { println!( "{}", i as f64 / 2291774376.0 ); } + full_maps.iter().fold(seed, |value, x| { + x.convert(value) + }) + }).min().unwrap().to_string() +} + +pub mod prelude { + pub use super::part1; + pub use super::part2; +} + diff --git a/day_05/src/main.rs b/day_05/src/main.rs new file mode 100644 index 0000000..d5052a5 --- /dev/null +++ b/day_05/src/main.rs @@ -0,0 +1,89 @@ +use day_05::prelude::*; + +fn main() { + let input = include_str!("../input.txt"); + println!("{}", part1(input)); + println!("{}", part2(input)); +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_part1() { + let test_input = "seeds: 79 14 55 13 + +seed-to-soil map: +50 98 2 +52 50 48 + +soil-to-fertilizer map: +0 15 37 +37 52 2 +39 0 15 + +fertilizer-to-water map: +49 53 8 +0 11 42 +42 0 7 +57 7 4 + +water-to-light map: +88 18 7 +18 25 70 + +light-to-temperature map: +45 77 23 +81 45 19 +68 64 13 + +temperature-to-humidity map: +0 69 1 +1 0 69 + +humidity-to-location map: +60 56 37 +56 93 4"; + assert_eq!(part1(test_input), "35".to_string()); + } + + #[test] + fn test_part2() { + let test_input = "seeds: 79 14 55 13 + +seed-to-soil map: +50 98 2 +52 50 48 + +soil-to-fertilizer map: +0 15 37 +37 52 2 +39 0 15 + +fertilizer-to-water map: +49 53 8 +0 11 42 +42 0 7 +57 7 4 + +water-to-light map: +88 18 7 +18 25 70 + +light-to-temperature map: +45 77 23 +81 45 19 +68 64 13 + +temperature-to-humidity map: +0 69 1 +1 0 69 + +humidity-to-location map: +60 56 37 +56 93 4"; + assert_eq!(part2(test_input), "46".to_string()); + } + +}