diff --git a/2023/day06/Cargo.toml b/2023/day06/Cargo.toml new file mode 100644 index 0000000..e1ca023 --- /dev/null +++ b/2023/day06/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "day06" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +nom.workspace = true +helper.workspace = true +divan.workspace = true + +[[bench]] +name = "benches" +harness = false diff --git a/2023/day06/benches/benches.rs b/2023/day06/benches/benches.rs new file mode 100644 index 0000000..f1fa741 --- /dev/null +++ b/2023/day06/benches/benches.rs @@ -0,0 +1,3 @@ +fn main() { + day06::bench(); +} diff --git a/2023/day06/input.txt b/2023/day06/input.txt new file mode 100644 index 0000000..45e74fa --- /dev/null +++ b/2023/day06/input.txt @@ -0,0 +1,2 @@ +Time: 40 92 97 90 +Distance: 215 1064 1505 1100 diff --git a/2023/day06/input_small.txt b/2023/day06/input_small.txt new file mode 100644 index 0000000..28f5ae9 --- /dev/null +++ b/2023/day06/input_small.txt @@ -0,0 +1,2 @@ +Time: 7 15 30 +Distance: 9 40 200 diff --git a/2023/day06/src/lib.rs b/2023/day06/src/lib.rs new file mode 100644 index 0000000..37d0692 --- /dev/null +++ b/2023/day06/src/lib.rs @@ -0,0 +1,83 @@ +use helper::{Day, IteratorExt, Variants}; + +pub fn main() { + helper::main::(include_str!("../input.txt")); +} + +struct Day06; + +helper::define_variants! { + day => crate::Day06; + part1 { + basic => crate::part1; + } + part2 { + basic => crate::part2; + } +} + +impl Day for Day06 { + fn part1() -> Variants { + part1_variants!(construct_variants) + } + + fn part2() -> Variants { + part2_variants!(construct_variants) + } +} + +#[derive(Debug)] +struct Race { + time: u64, + distance: u64, +} + +fn parse(input: &str) -> Vec { + let lines = input.lines().collect_array::<2>().unwrap(); + lines[0] + .split_ascii_whitespace() + .zip(lines[1].split_ascii_whitespace()) + .skip(1) + .map(|(time, distance)| Race { + time: time.parse().unwrap(), + distance: distance.parse().unwrap(), + }) + .collect() +} + +fn part1(input: &str) -> u64 { + let races = parse(input); + + races + .iter() + .map(|race| { + let t = race.time; + + (0..t) + .filter(|&i| { + let t_run = t - i; + let d = t_run * i; + + d > race.distance + }) + .count() as u64 + }) + .product() +} + +fn part2(_input: &str) -> u64 { + 0 +} + +helper::tests! { + day06 Day06; + part1 { + small => 0; + default => 0; + } + part2 { + small => 0; + default => 0; + } +} +helper::benchmarks! {} diff --git a/2023/day06/src/main.rs b/2023/day06/src/main.rs new file mode 100644 index 0000000..5f82631 --- /dev/null +++ b/2023/day06/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + day06::main(); +} diff --git a/Cargo.lock b/Cargo.lock index 78e9cf5..f8d2391 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -170,6 +170,15 @@ dependencies = [ "nom", ] +[[package]] +name = "day06" +version = "0.1.0" +dependencies = [ + "divan", + "helper", + "nom", +] + [[package]] name = "divan" version = "0.1.4"