From f202587c426d51912793612da8addaf230f27bf6 Mon Sep 17 00:00:00 2001 From: Wesley Moore Date: Wed, 6 Dec 2023 17:50:26 +1000 Subject: [PATCH] 2023: Day 6, part 1 --- 2023/day6/Cargo.lock | 7 ++++++ 2023/day6/Cargo.toml | 8 +++++++ 2023/day6/src/main.rs | 54 ++++++++++++++++++++++++++++++++++++++++++ 2023/input/day6.sample | 3 +++ 2023/input/day6.txt | 2 ++ 5 files changed, 74 insertions(+) create mode 100644 2023/day6/Cargo.lock create mode 100644 2023/day6/Cargo.toml create mode 100644 2023/day6/src/main.rs create mode 100644 2023/input/day6.sample create mode 100644 2023/input/day6.txt diff --git a/2023/day6/Cargo.lock b/2023/day6/Cargo.lock new file mode 100644 index 0000000..a16c0bf --- /dev/null +++ b/2023/day6/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "day6" +version = "0.1.0" diff --git a/2023/day6/Cargo.toml b/2023/day6/Cargo.toml new file mode 100644 index 0000000..89d04ae --- /dev/null +++ b/2023/day6/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "day6" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/2023/day6/src/main.rs b/2023/day6/src/main.rs new file mode 100644 index 0000000..1c42c89 --- /dev/null +++ b/2023/day6/src/main.rs @@ -0,0 +1,54 @@ +use std::{env, fs}; + +type BoxError = Box; + +fn main() -> Result<(), BoxError> { + let input_path = env::args_os() + .skip(1) + .next() + .ok_or("missing input file path")?; + let input = fs::read_to_string(input_path)?; + let mut lines = input.lines(); + let times = lines.next().ok_or("missing times")?; + let distances = lines.next().ok_or("missing distances")?; + + let times = parse("Time:", times)?; + let distances = parse("Distance:", distances)?; + if times.len() != distances.len() { + return Err("time/distance length mismatch".into()); + } + + let mut results = vec![0; times.len()]; + for (i, (time, record_distance)) in times + .iter() + .copied() + .zip(distances.iter().copied()) + .enumerate() + { + (1..time - 1).for_each(|hold| { + let velocity = hold; + let distance = (time - hold) * velocity; + if distance > record_distance { + results[i] += 1; + } + }) + } + + println!("Part 1: {}", results.iter().fold(1, |a, b| a * b)); + + Ok(()) +} + +fn parse(prefix: &str, line: &str) -> Result, BoxError> { + if !line.starts_with(prefix) { + return Err(format!("line did not match expected prefix ({}): {}", prefix, line).into()); + } + + let numbers = line + .split_ascii_whitespace() + .skip(1) + .filter(|word| !word.is_empty()) + .map(|word| word.parse::()) + .collect::, _>>()?; + Ok(numbers) +} diff --git a/2023/input/day6.sample b/2023/input/day6.sample new file mode 100644 index 0000000..ff1c069 --- /dev/null +++ b/2023/input/day6.sample @@ -0,0 +1,3 @@ +Time: 7 15 30 +Distance: 9 40 200 + diff --git a/2023/input/day6.txt b/2023/input/day6.txt new file mode 100644 index 0000000..e0996e8 --- /dev/null +++ b/2023/input/day6.txt @@ -0,0 +1,2 @@ +Time: 60 80 86 76 +Distance: 601 1163 1559 1300