From 39aa33152ff7b6b7dbade75e4dde860089d314e5 Mon Sep 17 00:00:00 2001 From: Lennard Brinkhaus Date: Wed, 6 Dec 2023 09:37:00 +0100 Subject: [PATCH] feat: implement day 06 --- src/day06.rs | 104 ++++++++++++++++++++++++++++++++++++++ src/input/day06/input.txt | 2 + src/main.rs | 7 ++- 3 files changed, 112 insertions(+), 1 deletion(-) create mode 100644 src/day06.rs create mode 100644 src/input/day06/input.txt diff --git a/src/day06.rs b/src/day06.rs new file mode 100644 index 0000000..e1c3e7f --- /dev/null +++ b/src/day06.rs @@ -0,0 +1,104 @@ +use std::time::Instant; + +pub fn execute_task01(content: &str) { + let start = Instant::now(); + let product_correct_races = solve_01(content).unwrap(); + let duration = start.elapsed(); + + assert_eq!(1624896, product_correct_races); + println!("Day06 - Task01 - Duration: {duration:?} - Product Correct Races: {}", product_correct_races) +} + +pub fn execute_task02(content: &str) { + let start = Instant::now(); + let product_correct_races = solve_02(content).unwrap(); + let duration = start.elapsed(); + + //assert_eq!(322500873, product_correct_races); + println!("Day06 - Task02 - Duration: {duration:?} - Correct Race: {}", product_correct_races) +} + +fn solve_01(content: &str) -> anyhow::Result { + let result = content + .lines() + .map(|line| line + .split_once(":") + .unwrap() + .1 + .trim()) + .map(|line| line + .split(" ") + .filter(|part| part.len() > 0) + .map(|data| data.parse::().unwrap()) + .collect::>()) + .collect::>>(); + + let mut races = vec![]; + + for i in 0..result[0].len() { + races.push((result[0][i].clone(), result[1][i].clone())) + } + + Ok(races + .iter() + .map(|(time, distance)| calc_possible_options(*time, *distance)) + .map(|options| options.len()) + .product()) +} + +fn solve_02(content: &str) -> anyhow::Result { + let result = content + .lines() + .map(|line| line + .split_once(":") + .unwrap() + .1 + .trim()) + .map(|line| line + .replace(" ", "") + .parse::().unwrap()) + .collect::>(); + + let mut races = vec![(result[0].clone(), result[1].clone())]; + Ok(races + .iter() + .map(|(time, distance)| calc_possible_options(*time, *distance)) + .map(|options| options.len()) + .product()) +} + + +fn calc_possible_options(time: i64, distance: i64) -> Vec { + let mut data = vec![]; + // TODO Performance!!! + for i in 1..time { + let y = i * (time - i); + if y > distance { + data.push(i) + } else if data.len() > 0 { + break + } + } + + data +} + +#[test] +fn test_solve_01() { + let example = r#"Time: 7 15 30 +Distance: 9 40 200"#; + + let result = solve_01(example).unwrap(); + + assert_eq!(288, result); +} + +#[test] +fn test_solve_02() { + let example = r#"Time: 7 15 30 +Distance: 9 40 200"#; + + let result = solve_02(example).unwrap(); + + assert_eq!(71503, result); +} diff --git a/src/input/day06/input.txt b/src/input/day06/input.txt new file mode 100644 index 0000000..e3ea82e --- /dev/null +++ b/src/input/day06/input.txt @@ -0,0 +1,2 @@ +Time: 56 97 78 75 +Distance: 546 1927 1131 1139 \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 695ba95..da80718 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,12 +4,14 @@ mod day02; mod day03; mod day04; mod day05; +mod day06; const CONTENT01: &'static str = include_str!("input/day01/input.txt"); const CONTENT02: &'static str = include_str!("input/day02/input.txt"); const CONTENT03: &'static str = include_str!("input/day03/input.txt"); const CONTENT04: &'static str = include_str!("input/day04/input.txt"); const CONTENT05: &'static str = include_str!("input/day05/input.txt"); +const CONTENT06: &'static str = include_str!("input/day06/input.txt"); fn main() { day01::execute_task01(CONTENT01); @@ -25,5 +27,8 @@ fn main() { day04::execute_task02(CONTENT04); println!(); day05::execute_task01(CONTENT05); - day05::execute_task02(CONTENT05); + //day05::execute_task02(CONTENT05); // Performance Issues + println!(); + day06::execute_task01(CONTENT06); + day06::execute_task02(CONTENT06); }