diff --git a/day_03/Cargo.lock b/day_03/Cargo.lock new file mode 100644 index 0000000..c7aa5f8 --- /dev/null +++ b/day_03/Cargo.lock @@ -0,0 +1,589 @@ +# 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_03" +version = "0.1.0" +dependencies = [ + "criterion", +] + +[[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 = "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 = "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 = "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 = "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_03/Cargo.toml b/day_03/Cargo.toml new file mode 100644 index 0000000..bb626f8 --- /dev/null +++ b/day_03/Cargo.toml @@ -0,0 +1,19 @@ +[package] +name = "day_03" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] + +[dev-dependencies] +criterion = { version = "0.4", features = ["html_reports"] } + +[[bench]] +name = "part1" +harness = false + +[[bench]] +name = "part2" +harness = false diff --git a/day_03/benches/part1.rs b/day_03/benches/part1.rs new file mode 100644 index 0000000..8a3ef8b --- /dev/null +++ b/day_03/benches/part1.rs @@ -0,0 +1,11 @@ +use criterion::{black_box, criterion_group, criterion_main, Criterion}; + +use day_02::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_03/benches/part2.rs b/day_03/benches/part2.rs new file mode 100644 index 0000000..c9c9a5e --- /dev/null +++ b/day_03/benches/part2.rs @@ -0,0 +1,11 @@ +use criterion::{black_box, criterion_group, criterion_main, Criterion}; + +use day_02::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_03/input.txt b/day_03/input.txt new file mode 100644 index 0000000..0d465e9 --- /dev/null +++ b/day_03/input.txt @@ -0,0 +1,140 @@ +.......262....300...................507.....961..............668.....................189.906...........................624.................. +..148.................805..130..880*...........*684.............*......*..............*..-......%.................$........17...65....91*... +......272.....464.....=......*.........................208*.....260.967.38.......692*.........676............@247..652.585.#......@......74. +.................*734........772......171.........728$.....52........................747.=866............350...........*.................... +..328....21...........941......../.....*...243................429@......567$...623.................654..#.....*815....360......812%......... +..........$.....%....@....6....583.....598...&.114@...618......................+...............156.........817...../.................340.... +.....883........359.....*.*..........................*............@............................./...920............302.712...........*...... +........*.....*......100..192.160........791......843...680*643.252.................322...354..........*.....+.........*............556..... +.........958.580.................*.............................................879.....-..$............633...63......561.....289............ +..............................745.......221..504-................356..............*827........355...............................*46......... +...................206..382...........$...........142@.105...534........./.......................*..........535.....................515..... +..201.....336.......-..*.....268..121..775...-...........%..............791.............129...449.....450..*.........790...........*........ +............%.559*......7.....+..*..........865......895............930.....427..242......#...............414.830............37..809........ +..................85.............466..550-..........+....................44...*...*.........361..215..........*......808.220*............... +................*......709....................577......430...&..148........*.696.988....801.*.....*..........202..25..*..................... +.....636......14...........372.............64...*...........586.%...15..241.............&....387..447...............*..811....146........... +.....-...........892*226....*....161.........%.623..................*...........$...............................................&.....657... +........987.................147.=....+.254..........887........773...321.....221....415.971..................74.......702..212.............. +......@............182............335.....*...........*..77.......#......................./.....29..........%...........&..*.......872...... +......643............*...................756.........677.*..........251......521..620......................................685.206.......132 +............274*...........929.................871........686.542..%........*......*.......................882....756.............*859..*... +................516.........*.....................*..............*...289...10.....890..985...258...963...............*.................526.. +.............................610...............857........210..818......*.......@.........#..=......*.....968.........42..........732....... +.............../..*453.................467..........376......$..........429..218.....................955..%..............357................ +.......841....493..............830#.......*342.........*........*................./....562....945................224.......*..435........... +..................843.803......................2........56...248.560.............281.....*.......*.........-........*5....728..*......156... +...822*288....352.*...+..............$.........$........................@..............794......539..-814..528.81.......+.......687.....*... +................*.783..../454.859....809..742........................663...267/..................................*.....847..............167. +....&371.296.484..............*..........=...........516.......925...............................#................441...............224..... +.........#............594..417....458............112*.....65.....#.....337...357....#..369*277.825....386..................212.............. +................651..#...........*.....$..................*............*.....*...125...................*......497.........*..............509 +......320..........*......-...883..989..733.....668.905...997.......454.....353...........946....18.....340...&......515...774........*..... +......-.....880....476...955.......=................*...........58.....................23....*69.+..102..............*.............933.687.. +........931....*..............................757....602.983......-..822..............*.............*.......732.....15...................... +....../....*....463.............183$..961........*.......*....#...........572......618.............52..........@........205......*....&429.. +...204......550............-.........%.........628.....240..588............*................................/..........*......167.776....... +................360....*....69..........*..........534..................%..340.....................885*912..52......949..................... +.........483......*..46.754......-....607.346.....*..............851.302..................@....................-........330.-201............ +.........@...274.811..............819...........56......326......................496....650...........382......168.420...*.........452...... +..............#........325.449........581...........112*........................+............163.......*.................407.......*...460.. +......595.........*......*...................226+.................268..153.........#123........*.....$..877..........#..........361...*..... +......*........280.287.427...........755...........................%.........................722...323................307................... +..132.553...............................*.......117......450.109...............849.....749.................................105..537#..741... +...*.................................788.......*....691...%...$............148.....960..*....512.728#..544.624.....$......*................. +.523..97...186..907....508....................739......*................*.....*856.&....15..................$.....911.......&..877..632..... +..............*.*.........*.........161*....+........381.............453.376..........................30..................806.......*....... +............560.877...773...............168..446..............465..............435@..24..622...%.................................=..347..... +...*165................-.......................................*......@816..............*....313..421......269....539.49.530..113........... +768......*626.926....=.....676...981=............703.........682.&141....................444......=........@.........*......*......539...... +......159.......$..362......../.......254.....14..=...............................219.................383.........*......182.............733 +.324........879.....................*.=...............316.....*480......*............*............439*.........621.961.......*481........... +....*...208*...........188.......763.....553...........*...957.......771.944........445......4...........375..............174.....903.665... +.................651........851.......37*...........303.................................446......61......*......346................@...*.... +......160....412*......../.....=.220.........354.........-.....838...........473..........#........$.870.......*............*........391.... +.....*....................218.....*.............*.....739..320*.........358....*...521......383.........*.....27...........107.............. +...969...718..................+....200.534.237+..........................=..889....*..........@........396.......................103..243... +........&..................841...-......&........./748...655..325.................430............813.................618.....687...#..@..... +..........825...761............800......................*....&...........496............652...$..*...............206..*..................... +...................+....&584.........35..663*590.....677...................*........741*....908...293..918............570....$......%....... +...........794.......................&...................852*...........361...............-.................................502.769.82...... +..535.......$............523*522................-..............847..........593..........267.....................................*.......... +....*...........73...............................299..262..941*..................813..............682..........877........494...411......... +..528................*997....*...477........464.....................521........@....*315......#...=..............*.......................... +............669...909.......342..*...........*..........444@...@.....-..611.911..............864......114....555..628..890.................. +............*.....................444.....921................783.......*.......................................*........=...414............. +..........622..................................422..-...............404......&906..381............438..........605.........@.....389.352@... +....77.........519...282........720.....159=..*....87......225.605......125...........+.............*..860-......................=.......... +...$...556.......-.....*..........#..........479............&....*.........*967...343..............400...........*883..................*881. +......*............207.653....113...359............227............357............*..........771...........426.905..............313..563..... +...74..233....959../.........*.........&............*...#.................543..64..*....179................*........@.............*......... +.....*..........*.......707.341..........&489.....206..132..407..........*........224..*................310........255./.........136........ +...251.......770........................................................475.............694....514.....................236.................. +........944........-379...............403.....934......47........................................*..............694........359.............. +732.............14..........*..........*.........*.................720....325..............375.483.............*.....356&..............686.. +.......160..672*.........250.125....791.....&76...1.....&963...422..#........../....191......*................925..........771...405&.-..... +...205....%......402................................117........*.........887....509..........44.....417.................=.-................. +...................*............=..145......746.......*........861..804....*.........313...............*....*.........761.......638......... +..852..767.....#67.835......$.359..*..................789.@................675.938.............957..........459.652..............*...949.... +........*.................977.....779.......348...627.....136.........+623..........964.............948+..........*.................=....... +........882......+............................./...#...............................*......311...152.......186......233.......374.......989.. +..148.........125.....931......706%....#...379.........................181.......227..........*..........*..............581..*.............. +.....*..179..............*.............970.../.......605...........736*......................700......970.................*.........+.487... +....209...*..............810....790..............787*......................#...+....-...824......587......709*.....899.....283....324..*.... +...........50...750............./.......633...............................364..66..593./......................115......-..............561... +...................#......989..............*217.......................620...........................................740.....*361....+....... +..........266.........647*....$..992....*...........318...345...........*..............-........252..797.................354........8...110. +.............................375.....344.334.......*.......*..........460......558.....364.......#..$...........@....%..........3........... +...$14...314...652...............................626............%...................%.....................778....868..215................... +...........&....*.......$........241.................*796....552..738..#234...@....358............993....*.................................. +................594..230..-..647*.....&............56.............*............479.....921..............519................426......713..... +.....944*................404........53..349....$........459......986..+......=..............-666..............237...897#..*........&....755. +.........176........280......714..........*..876.268...*.............946.492.113..........*..............................855................ +.....................*........*...936..................973.................*............736.......844.659....652.@............266...547..... +...677......@........426.....89........./...................185..........630.285............196...../....*...*....349....102....*....*...... +........765..677................*..46....85.901.....*...440...................../..........*.............304..952............1........507... +...........=.........669......847....*........*..821......*.301.......880.................829.951.460....................876................ +.................466*......*.........69.....207.........419...*...................442............*...........-.....898.....=.667............ +....421.56=................318....................................178...............+...............896*855...522..&..........%....151*405.. +....*.......481-...70*401..............550...................*.......*296...91........998.566..112.......................................... +...235.472..............................*......962....963.935...921.........+.....................-...........168.....=516.304...416*699.... +........#...*10.................240=.....687..*...................*.475..-...........697.......................*..............*............. +..261.....47.......750....954.........=......274........-...203.570....*..71...........*...484..*.....%61...397..854...#.......523.....931.. +..............34............$....%.....500........216.647............271.......908....906.......488...............*..92..634................ +..........83*....................994.......511.55*........991...16.........417*....*......96...................697.................&........ +.............582.450=.....................................=.....*...............654.411........313..................29.........32.849....... +.......563..............239.........&.............405............193......+.411.........%......*...&.....623.........$....+748..=.....840... +..........&......#.......-....761....639.275+.......*...................69..$....../.233..@..88.....226...*.....366................../...... +.............248.16............/...............+...556...........306............354......308..............682.../......894...#.............. +..697.......*.........@..........959...125....297.......................*337...................72....108...............*......991.344....781 +...*.......715...49.392.........*.......*................652/........#.......55*.......................-.844......./..344..........-........ +...591....................334.701.......838.......................397....501....759.....-.........................946.......442............. +..................557*421...@.....749........500..........#170..........%............941..373....425..818.......................828.....237. +..793*144...........................$.302....*.....938*..................................*..........*....%.378........66..63...*............ +............474...............893............267.......810................585..........558.......694......../.................340.701....... +..............#..798......332*.....855.....................592.589........&.....833=.........199......730.......+......177.................. +.................*..................*.........................*........................#.......*......*.....154.589...*..................... +.....64*809........../............@.139............228..374....../.......771.........534........281...........*......506.......=............ +..............427.750......546...98............707..=......&..381..877..*..........*........6.........%......489.........../.795............ +...895.805*.....*.....................*694....=........848...............616.....69.86.....*..460......895.......544.....651.......14.886... +...........390...850..777.534..138..52..........570....@.........812.$71................795.......557............-.....................*.... +..46*41...............*...........*................@.........613*.................584.......=........*...............106*....627.602....829. +........217*380.............721..928.873...........................593..353...............341..430..989..923.............695......-...%..... +..................586.......$.........*...207.......585&......485..*...*..........................*......*..........................929..... +............#.154*.....60.............95.......................*..138.764..........................425.275......%.331*664..854..........887. +908.917..359........../........659........125..785...........499..........991....358..........102*...........903..........*.....409....-.... +...*.........963*.../......408.............%........830*871...............*......*...828..........764.671................293...*............ +....................559....=..........81............................391...969.513....*.......991........&..518.....208*.......75............ +.....461.560*......................../........950.........320%.....*................97........................#..........+675.....352...339. +......*......439.%101.709...302.870......838....@.....594........953.......514*862........339....................................*.......... +...247...................@......*.................312....#.................................-...271..800...@..818.850....333*722...330..951.. +........922.................184.666.............../..........................356...105.........#...*.....353./........................*..... +832.....-.......758...800....$......................337.......419...........=.......*...258........926.........................../592..186.. +...................+...........220.287...117....783......646..#.........451........750..........27.....$.......*750......890................ +........719............982....%.......*..*.........*144.*..................*...........*764./.....*373..368.201..........@.................. +......../..........388....*........115...432.............594............345..658$...442......833.................408.........=..........542. +..........27.#498..*.....286..+490...............................966.....................262.......................#..542.237............... +72....251..#.......667.....................282..556..260...........%.$......................*....107..................*..................... +.......*.......*..............................*..-...*...............64..#...=.....402..@....790.@.....................295.766...484..969... +......85....882.80.......184$................117........................454..583......*..351...............266....................&....*.... +.......................................................48...........................436........275...................869............258..... diff --git a/day_03/src/lib.rs b/day_03/src/lib.rs new file mode 100644 index 0000000..94db824 --- /dev/null +++ b/day_03/src/lib.rs @@ -0,0 +1,296 @@ +//const LINE_LENGTH: usize = 10; +const LINE_LENGTH: usize = 140; + +#[derive(Debug)] +pub struct Number { + start: usize, + stop: usize, + line_number: usize, + value: u32, +} + +#[derive(Debug)] +pub enum NumberPosition { + Top, + WholeTop, + WholeBottom, + Bottom, + Right, + Left, + TopLeft, + TopRight, + BottomLeft, + BottomRight, + Middle, + WholeMiddle, +} + +impl Number { + pub fn get_surrounding(&self, total_lines: usize) -> Vec<(usize, usize)> { + let position = + if self.line_number == 0 { + if self.start == 0 { + if self.stop == LINE_LENGTH - 1 { + NumberPosition::WholeTop + } else { + NumberPosition::TopLeft + } + } else if self.stop == LINE_LENGTH - 1 { + NumberPosition::TopRight + } else { + NumberPosition::Top + } + } else if self.line_number == total_lines - 1 { + if self.start == 0 { + if self.stop == LINE_LENGTH - 1 { + NumberPosition::WholeBottom + } else { + NumberPosition::BottomLeft + } + } else if self.stop == LINE_LENGTH - 1 { + NumberPosition::BottomRight + } else { + NumberPosition::Bottom + } + } else { + if self.start == 0 { + if self.stop == LINE_LENGTH - 1 { + NumberPosition::WholeMiddle + } else { + NumberPosition::Left + } + } else if self.stop == LINE_LENGTH - 1 { + NumberPosition::Right + } else { + NumberPosition::Middle + } + }; + match position { + NumberPosition::Top => { + let mut row = (self.start-1..self.stop+2) + .map(|x| {(1, x)}) + .collect::>(); + row.push((0, self.start - 1)); + row.push((0, self.stop + 1)); + row + }, + NumberPosition::WholeTop => { + (self.start-1..self.stop+2) + .map(|x| {(1, x)}) + .collect::>() + }, + NumberPosition::WholeBottom => { + (self.start-1..self.stop+2) + .map(|x| {(total_lines - 2, x)}) + .collect::>() + }, + NumberPosition::Bottom => { + let mut row = (self.start-1..self.stop+2) + .map(|x| {(total_lines - 2, x)}) + .collect::>(); + row.push((total_lines - 1, self.start - 1)); + row.push((total_lines - 1, self.stop + 1)); + row + }, + NumberPosition::Right => { + let mut output = (self.start-1..LINE_LENGTH) + .map(|x| {(self.line_number - 1, x)}) + .collect::>(); + output.append(&mut (self.start-1..LINE_LENGTH) + .map(|x| {(self.line_number + 1, x)}) + .collect::>()); + output.push((self.line_number, self.start - 1)); + output + }, + NumberPosition::Left => { + let mut output = (0..self.stop+2) + .map(|x| {(self.line_number - 1, x)}) + .collect::>(); + output.append(&mut (0..self.stop+2) + .map(|x| {(self.line_number + 1, x)}) + .collect::>()); + output.push((self.line_number, self.stop + 1)); + output + }, + NumberPosition::TopLeft => { + let mut row = (0..self.stop+2) + .map(|x| {(1, x)}) + .collect::>(); + row.push((0, self.stop + 1)); + row + }, + NumberPosition::TopRight => { + let mut row = (self.start-1..LINE_LENGTH) + .map(|x| {(1, x)}) + .collect::>(); + row.push((0, self.start - 1)); + row + }, + NumberPosition::BottomLeft => { + let mut row = (0..self.stop+2) + .map(|x| {(total_lines - 2, x)}) + .collect::>(); + row.push((total_lines - 1, self.stop + 1)); + row + }, + NumberPosition::BottomRight => { + let mut row = (self.start-1..LINE_LENGTH) + .map(|x| {(total_lines - 2, x)}) + .collect::>(); + row.push((total_lines - 1, self.start - 1)); + row + }, + NumberPosition::Middle => { + let mut output = (self.start-1..self.stop+2) + .map(|x| {(self.line_number - 1, x)}) + .collect::>(); + output.append(&mut (self.start-1..self.stop+2) + .map(|x| {(self.line_number + 1, x)}) + .collect::>()); + output.push((self.line_number, self.start - 1)); + output.push((self.line_number, self.stop + 1)); + output + }, + NumberPosition::WholeMiddle => { + let mut output = (self.start-1..self.stop+2) + .map(|x| {(self.line_number - 1, x)}) + .collect::>(); + output.append(&mut (self.start-1..self.stop+2) + .map(|x| {(self.line_number + 1, x)}) + .collect::>()); + output + }, + } + } +} + +pub fn part1(input: &str) -> String { + let mut number_list = Vec::::new(); + input.lines().enumerate().for_each(|(i, line)| { + let mut num = false; + let mut current_digits = String::from(""); + for j in 0..LINE_LENGTH { + let next_value = line.chars().nth(j).unwrap(); + let is_next_num = next_value.is_digit(10); + if num == false && is_next_num { // Number starts + current_digits.push(next_value); + num = true; + } else if num == true && is_next_num { // Number continues + if j == LINE_LENGTH - 1 { // Number continued to end + current_digits.push(next_value); + number_list.push(Number{ + start: j - current_digits.len(), + stop: j, + line_number: i, + value: current_digits.parse::().unwrap(), + }); + } else { // Normal continue + current_digits.push(next_value); + } + } else if num == true && !is_next_num { // Number ends + number_list.push(Number{ + start: j - current_digits.len(), + stop: j - 1, + line_number: i, + value: current_digits.parse::().unwrap(), + }); + current_digits = "".to_string(); + num = false; + } + } + }); + let total_lines = input.lines().collect::>().len(); + let output: u32 = number_list.iter().map(|number| { + let surroundings = number.get_surrounding(total_lines); + let is_part_number = surroundings.iter().any(|(i,j)| { + !input.lines().nth(*i).unwrap().chars().nth(*j).unwrap().is_digit(10) && + input.lines().nth(*i).unwrap().chars().nth(*j).unwrap() != '.' + }); + if is_part_number { + number.value + } else { + 0 + } + }).sum(); + output.to_string() +} + +pub fn part2(input: &str) -> String { + let total_lines = input.lines().collect::>().len(); + let output: u32 = input.lines().enumerate().map(|(i, line)| { + line.chars().enumerate().map(move |(j, character)| { + if character == '*' { + // Get the upper and lower bounds (lines above and below) + let (lower, upper) = if i == 0 { + (0, 1) + } else if i == total_lines { + (total_lines - 2, total_lines - 1) + } else { + (i - 1, i + 1) + }; + + // Get the numbers in that range + let mut number_list = Vec::::new(); + input.lines().collect::>()[lower..upper+1].iter().enumerate().for_each(|(k, line)| { + let mut num = false; + let mut current_digits = String::from(""); + for j in 0..LINE_LENGTH { + let next_value = line.chars().nth(j).unwrap(); + let is_next_num = next_value.is_digit(10); + if num == false && is_next_num { // Number starts + current_digits.push(next_value); + num = true; + } else if num == true && is_next_num { // Number continues + if j == LINE_LENGTH - 1 { // Number continued to end + current_digits.push(next_value); + number_list.push(Number{ + start: j - current_digits.len(), + stop: j, + line_number: i - 1 + k, + value: current_digits.parse::().unwrap(), + }); + } else { // Normal continue + current_digits.push(next_value); + } + } else if num == true && !is_next_num { // Number ends + number_list.push(Number{ + start: j - current_digits.len(), + stop: j - 1, + line_number: i - 1 + k, + value: current_digits.parse::().unwrap(), + }); + current_digits = "".to_string(); + num = false; + } + } + }); + + let mut count = 0; + let mut ratio = 1; + number_list.into_iter().for_each(|number| { + let surroundings = number.get_surrounding(total_lines); + surroundings.into_iter().for_each(|(s_i, s_j)| { + if s_i == i && s_j == j { + count += 1; + ratio *= number.value; + } + }); + }); + if count >= 2 { + ratio + } else { + 0 + } + } else { + 0 + } + }).sum::() + }).sum(); + output.to_string() +} + +pub mod prelude { + pub use super::part1; + pub use super::part2; +} + diff --git a/day_03/src/main.rs b/day_03/src/main.rs new file mode 100644 index 0000000..891edf4 --- /dev/null +++ b/day_03/src/main.rs @@ -0,0 +1,43 @@ +use day_03::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 = "467..114.. +...*...... +..35..633. +......#... +617*...... +.....+.58. +..592..... +......755. +...$.*.... +.664.598.."; + assert_eq!(part1(test_input), "4361".to_string()); + } + + #[test] + fn test_part2() { + let test_input = "467..114.. +...*...... +..35..633. +......#... +617*...... +.....+.58. +..592..... +......755. +...$.*.... +.664.598.."; + assert_eq!(part2(test_input), "467835".to_string()); + } + +}