From 1a0f1acf74f33aec8a711ef490da850e9c0442fe Mon Sep 17 00:00:00 2001 From: BlackDemonFire Date: Wed, 6 Dec 2023 12:40:10 +0100 Subject: [PATCH] Day 6 --- Cargo.lock | 8 ++++++++ Cargo.toml | 2 ++ d01t1/src/lib.rs | 23 ++++++++++++++++++++++ d01t1/src/main.rs | 27 ++------------------------ d06t1/Cargo.toml | 8 ++++++++ d06t1/demo_input.txt | 2 ++ d06t1/src/main.rs | 45 ++++++++++++++++++++++++++++++++++++++++++++ d06t2/Cargo.toml | 8 ++++++++ d06t2/demo_input.txt | 2 ++ d06t2/src/main.rs | 34 +++++++++++++++++++++++++++++++++ 10 files changed, 134 insertions(+), 25 deletions(-) create mode 100644 d01t1/src/lib.rs create mode 100644 d06t1/Cargo.toml create mode 100644 d06t1/demo_input.txt create mode 100644 d06t1/src/main.rs create mode 100644 d06t2/Cargo.toml create mode 100644 d06t2/demo_input.txt create mode 100644 d06t2/src/main.rs diff --git a/Cargo.lock b/Cargo.lock index 08072f6..b6f8e0e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -41,3 +41,11 @@ version = "0.1.0" [[package]] name = "d05t2" version = "0.1.0" + +[[package]] +name = "d06t1" +version = "0.1.0" + +[[package]] +name = "d06t2" +version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index c4fe8c6..64700a7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,4 +12,6 @@ members = [ "d04t2", "d05t1", "d05t2", + "d06t1", + "d06t2", ] diff --git a/d01t1/src/lib.rs b/d01t1/src/lib.rs new file mode 100644 index 0000000..fbaa9a5 --- /dev/null +++ b/d01t1/src/lib.rs @@ -0,0 +1,23 @@ +pub fn d01t1(input: &str) -> u32 { + input + .lines() + .map(|line| { + let digits = line + .chars() + .filter(char::is_ascii_digit) + .collect::>(); + let mut num_str = digits.first().unwrap().to_string(); + num_str.push(*digits.last().unwrap()); + num_str.parse::().unwrap() + }) + .sum::() +} +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn test_d01t1() { + let input = include_str!("../demo_input.txt"); + assert_eq!(d01t1(input), 142); + } +} diff --git a/d01t1/src/main.rs b/d01t1/src/main.rs index 5b780da..71d2b8e 100644 --- a/d01t1/src/main.rs +++ b/d01t1/src/main.rs @@ -1,31 +1,8 @@ use std::fs::read_to_string; +use d01t1::d01t1; + fn main() { let input = read_to_string("input.txt").unwrap(); println!("d01t1: {}", d01t1(&input)); } - -fn d01t1(input: &str) -> u32 { - input - .lines() - .map(|line| { - let digits = line - .chars() - .filter(char::is_ascii_digit) - .collect::>(); - let mut num_str = digits.first().unwrap().to_string(); - num_str.push(*digits.last().unwrap()); - num_str.parse::().unwrap() - }) - .sum::() -} - -#[cfg(test)] -mod tests { - use super::*; - #[test] - fn test_d01t1() { - let input = include_str!("../demo_input.txt"); - assert_eq!(d01t1(input), 142); - } -} diff --git a/d06t1/Cargo.toml b/d06t1/Cargo.toml new file mode 100644 index 0000000..2f3f691 --- /dev/null +++ b/d06t1/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "d06t1" +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/d06t1/demo_input.txt b/d06t1/demo_input.txt new file mode 100644 index 0000000..b39f49d --- /dev/null +++ b/d06t1/demo_input.txt @@ -0,0 +1,2 @@ +Time: 7 15 30 +Distance: 9 40 200 \ No newline at end of file diff --git a/d06t1/src/main.rs b/d06t1/src/main.rs new file mode 100644 index 0000000..91496f8 --- /dev/null +++ b/d06t1/src/main.rs @@ -0,0 +1,45 @@ +use std::fs::read_to_string; + +fn main() { + let input = read_to_string("input.txt").unwrap(); + println!("d06t1: {}", d06t1(&input)); +} + +pub fn d06t1(input: &str) -> usize { + let mut lines = input.lines(); + let mut timings = lines.next().unwrap().split_whitespace(); + let mut distances = lines.next().unwrap().split_whitespace(); + assert!(lines.next().is_none()); + assert_eq!(timings.next().unwrap(), "Time:"); + assert_eq!(distances.next().unwrap(), "Distance:"); + let timings = timings + .map(str::parse::) + .map(Result::unwrap) + .collect::>(); + let distances = distances + .map(str::parse::) + .map(Result::unwrap) + .collect::>(); + timings + .into_iter() + .zip(distances) + .map(min_press_time) + .reduce(|a, b| a * b) + .unwrap() +} + +fn min_press_time((time, distance): (usize, usize)) -> usize { + (1..=time) + .filter(|acceleration_time| acceleration_time * (time - acceleration_time) > distance) + .count() +} + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn test_d06t1() { + let input = include_str!("../demo_input.txt"); + assert_eq!(d06t1(input), 288); + } +} diff --git a/d06t2/Cargo.toml b/d06t2/Cargo.toml new file mode 100644 index 0000000..0950f2f --- /dev/null +++ b/d06t2/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "d06t2" +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/d06t2/demo_input.txt b/d06t2/demo_input.txt new file mode 100644 index 0000000..b39f49d --- /dev/null +++ b/d06t2/demo_input.txt @@ -0,0 +1,2 @@ +Time: 7 15 30 +Distance: 9 40 200 \ No newline at end of file diff --git a/d06t2/src/main.rs b/d06t2/src/main.rs new file mode 100644 index 0000000..81b472e --- /dev/null +++ b/d06t2/src/main.rs @@ -0,0 +1,34 @@ +use std::fs::read_to_string; + +fn main() { + let input = read_to_string("input.txt").unwrap(); + println!("d06t2: {}", d06t2(&input)); +} + +pub fn d06t2(input: &str) -> usize { + let mut lines = input.lines(); + let mut timings = lines.next().unwrap().split_whitespace(); + let mut distances = lines.next().unwrap().split_whitespace(); + assert!(lines.next().is_none()); + assert_eq!(timings.next().unwrap(), "Time:"); + assert_eq!(distances.next().unwrap(), "Distance:"); + let timing = timings.collect::().parse::().unwrap(); + let distance = distances.collect::().parse::().unwrap(); + min_press_time((timing, distance)) +} + +fn min_press_time((time, distance): (usize, usize)) -> usize { + (1..=time) + .filter(|acceleration_time| acceleration_time * (time - acceleration_time) > distance) + .count() +} + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn test_d06t2() { + let input = include_str!("../demo_input.txt"); + assert_eq!(d06t2(input), 71503); + } +}