commit f3afa52c9b718d47c006e1076c87eedff9ef4bda Author: Jan Christian Grünhage Date: Wed Dec 1 23:05:08 2021 +0100 initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3ceb93d --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/target +/input diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..f6c9aa0 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,131 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "aoc-rs-2021" +version = "0.1.0" +dependencies = [ + "aoc-runner", + "aoc-runner-derive", + "itertools", +] + +[[package]] +name = "aoc-runner" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d21ef9204ad206a5a3e918e9920da04e1118ad91ce4f23570be964b9d6b9dfcb" + +[[package]] +name = "aoc-runner-derive" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba8b944269d3fee645d281b1335e1797044db497bb02d0098cc3fdb8900069cc" +dependencies = [ + "aoc-runner-internal", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "aoc-runner-internal" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "274b0ba7f3669a45ec0aaacf94eb032a749de880ab776091576cca94037c9982" +dependencies = [ + "serde", + "serde_derive", + "serde_json", +] + +[[package]] +name = "either" +version = "1.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457" + +[[package]] +name = "itertools" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "69ddb889f9d0d08a67338271fa9b62996bc788c7796a5c18cf057420aaed5eaf" +dependencies = [ + "either", +] + +[[package]] +name = "itoa" +version = "0.4.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b71991ff56294aa922b450139ee08b3bfc70982c6b2c7562771375cf73542dd4" + +[[package]] +name = "proc-macro2" +version = "1.0.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba508cc11742c0dc5c1659771673afbab7a0efab23aa17e854cbab0837ed0b43" +dependencies = [ + "unicode-xid", +] + +[[package]] +name = "quote" +version = "1.0.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38bc8cc6a5f2e3655e0899c1b848643b2562f853f114bfec7be120678e3ace05" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "ryu" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c9613b5a66ab9ba26415184cfc41156594925a9cf3a2057e57f31ff145f6568" + +[[package]] +name = "serde" +version = "1.0.130" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f12d06de37cf59146fbdecab66aa99f9fe4f78722e3607577a5375d66bd0c913" + +[[package]] +name = "serde_derive" +version = "1.0.130" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d7bc1a1ab1961464eae040d96713baa5a724a8152c1222492465b54322ec508b" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "serde_json" +version = "1.0.72" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d0ffa0837f2dfa6fb90868c2b5468cad482e175f7dad97e7421951e663f2b527" +dependencies = [ + "itoa", + "ryu", + "serde", +] + +[[package]] +name = "syn" +version = "1.0.82" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8daf5dd0bb60cbd4137b1b587d2fc0ae729bc07cf01cd70b36a1ed5ade3b9d59" +dependencies = [ + "proc-macro2", + "quote", + "unicode-xid", +] + +[[package]] +name = "unicode-xid" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..5681870 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "aoc-rs-2021" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +aoc-runner = "0.3.0" +aoc-runner-derive = "0.3.0" +itertools = "0.10.1" diff --git a/src/day_01.rs b/src/day_01.rs new file mode 100644 index 0000000..94dc343 --- /dev/null +++ b/src/day_01.rs @@ -0,0 +1,55 @@ +use aoc_runner_derive::{aoc, aoc_generator}; + +use itertools::Itertools; + +#[aoc_generator(day1)] +pub fn parse_measurements(input: &str) -> Vec { + input + .split('\n') + .map(|input| input.parse().unwrap()) + .collect() +} + +#[aoc(day1, part1)] +pub fn count_increases_indexed(input: &[usize]) -> usize { + let mut count = 0; + for i in 1..input.len() { + if input[i - 1] < input[i] { + count += 1; + } + } + count +} + +#[aoc(day1, part1, tuple_windows)] +pub fn count_increases_tuple_windows(input: &[usize]) -> usize { + let mut count = 0; + for (a, b) in input.iter().tuple_windows() { + if a < b { + count += 1; + } + } + count +} + +#[aoc(day1, part2)] +pub fn count_sliding_increases_indexed(input: &[usize]) -> usize { + let mut count = 0; + for i in 3..input.len() { + if input[i - 3] + input[i - 2] + input[i - 1] < input[i - 2] + input[i - 1] + input[i] { + count += 1; + } + } + count +} + +#[aoc(day1, part2, tuple_windows)] +pub fn count_sliding_increases_tuple_windows(input: &[usize]) -> usize { + let mut count = 0; + for (a, b, c, d) in input.iter().tuple_windows() { + if a + b + c < b + c + d { + count += 1; + } + } + count +} diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..5cda2df --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,5 @@ +use aoc_runner_derive::aoc_lib; + +pub mod day_01; + +aoc_lib! { year = 2021 }