From dae33e4a4b1177607879bc69ee86f3421c166161 Mon Sep 17 00:00:00 2001 From: LordMathis Date: Wed, 6 Dec 2023 22:38:42 +0100 Subject: [PATCH] 2023-06: Implement part 2 --- 2023/06-wait-for-it/src/main.rs | 6 ++-- 2023/06-wait-for-it/src/part_one.rs | 13 ++++---- 2023/06-wait-for-it/src/part_two.rs | 47 +++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 9 deletions(-) create mode 100644 2023/06-wait-for-it/src/part_two.rs diff --git a/2023/06-wait-for-it/src/main.rs b/2023/06-wait-for-it/src/main.rs index d52adb4..df0b383 100644 --- a/2023/06-wait-for-it/src/main.rs +++ b/2023/06-wait-for-it/src/main.rs @@ -1,5 +1,7 @@ -mod part_one; +//mod part_one; +mod part_two; fn main() { - part_one::part_one(); + //part_one::part_one(); + part_two::part_two(); } diff --git a/2023/06-wait-for-it/src/part_one.rs b/2023/06-wait-for-it/src/part_one.rs index 8bbce86..93d1e1e 100644 --- a/2023/06-wait-for-it/src/part_one.rs +++ b/2023/06-wait-for-it/src/part_one.rs @@ -15,21 +15,21 @@ pub fn part_one() { for i in 0..race_results[0].len() { margin *= process_race_result(race_results[0][i], race_results[1][i]); } - + println!("{:?}", margin); } } fn process_line(s: &str) -> Vec { - - let nums_str = s.split(':').collect::>()[1].split_whitespace().collect::>(); + let nums_str = s.split(':').collect::>()[1] + .split_whitespace() + .collect::>(); let nums: Vec = nums_str.iter().map(|s| s.parse::().unwrap()).collect(); - return nums; + return nums; } fn process_race_result(time: u32, distance: u32) -> u32 { - let mut win_count = 0; for i in 0..time { @@ -41,11 +41,10 @@ fn process_race_result(time: u32, distance: u32) -> u32 { return win_count; } - fn read_lines

(filename: P) -> io::Result>> where P: AsRef, { let file = File::open(filename)?; Ok(io::BufReader::new(file).lines()) -} \ No newline at end of file +} diff --git a/2023/06-wait-for-it/src/part_two.rs b/2023/06-wait-for-it/src/part_two.rs new file mode 100644 index 0000000..dfa2ab6 --- /dev/null +++ b/2023/06-wait-for-it/src/part_two.rs @@ -0,0 +1,47 @@ +use std::fs::File; +use std::io::{self, BufRead}; +use std::path::Path; + +pub fn part_two() { + if let Ok(lines) = read_lines("./input.txt") { + let mut race_result: Vec = Vec::new(); + + for line in lines { + race_result.push(process_line(&line.unwrap())); + } + + let win_count = process_race_result(race_result[0], race_result[1]); + + println!("{:?}", win_count); + } +} + +fn process_line(s: &str) -> u64 { + let nums_str = s.split(':').collect::>()[1] + .split_whitespace() + .collect::>() + .concat(); + let num: u64 = nums_str.parse::().unwrap(); + + return num; +} + +fn process_race_result(time: u64, distance: u64) -> u64 { + let mut win_count = 0; + + for i in 0..time { + if i * (time - i) > distance { + win_count += 1; + } + } + + return win_count; +} + +fn read_lines

(filename: P) -> io::Result>> +where + P: AsRef, +{ + let file = File::open(filename)?; + Ok(io::BufReader::new(file).lines()) +}