From ef364a9ae004dd6e36dd0b488936304c6cb33116 Mon Sep 17 00:00:00 2001 From: LordMathis Date: Wed, 6 Dec 2023 22:18:28 +0100 Subject: [PATCH] 2023-06: Implement part one --- 2023/06-wait-for-it/Cargo.toml | 8 +++++ 2023/06-wait-for-it/input.txt | 2 ++ 2023/06-wait-for-it/input_test.txt | 2 ++ 2023/06-wait-for-it/src/main.rs | 5 +++ 2023/06-wait-for-it/src/part_one.rs | 51 +++++++++++++++++++++++++++++ 5 files changed, 68 insertions(+) create mode 100644 2023/06-wait-for-it/Cargo.toml create mode 100644 2023/06-wait-for-it/input.txt create mode 100644 2023/06-wait-for-it/input_test.txt create mode 100644 2023/06-wait-for-it/src/main.rs create mode 100644 2023/06-wait-for-it/src/part_one.rs diff --git a/2023/06-wait-for-it/Cargo.toml b/2023/06-wait-for-it/Cargo.toml new file mode 100644 index 0000000..49c02f8 --- /dev/null +++ b/2023/06-wait-for-it/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "wait-for-it" +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/06-wait-for-it/input.txt b/2023/06-wait-for-it/input.txt new file mode 100644 index 0000000..30a463c --- /dev/null +++ b/2023/06-wait-for-it/input.txt @@ -0,0 +1,2 @@ +Time: 51 92 68 90 +Distance: 222 2031 1126 1225 \ No newline at end of file diff --git a/2023/06-wait-for-it/input_test.txt b/2023/06-wait-for-it/input_test.txt new file mode 100644 index 0000000..b39f49d --- /dev/null +++ b/2023/06-wait-for-it/input_test.txt @@ -0,0 +1,2 @@ +Time: 7 15 30 +Distance: 9 40 200 \ No newline at end of file diff --git a/2023/06-wait-for-it/src/main.rs b/2023/06-wait-for-it/src/main.rs new file mode 100644 index 0000000..d52adb4 --- /dev/null +++ b/2023/06-wait-for-it/src/main.rs @@ -0,0 +1,5 @@ +mod part_one; + +fn main() { + part_one::part_one(); +} diff --git a/2023/06-wait-for-it/src/part_one.rs b/2023/06-wait-for-it/src/part_one.rs new file mode 100644 index 0000000..8bbce86 --- /dev/null +++ b/2023/06-wait-for-it/src/part_one.rs @@ -0,0 +1,51 @@ +use std::fs::File; +use std::io::{self, BufRead}; +use std::path::Path; + +pub fn part_one() { + if let Ok(lines) = read_lines("./input.txt") { + let mut margin = 1; + + let mut race_results: Vec> = Vec::new(); + + for line in lines { + race_results.push(process_line(&line.unwrap())); + } + + 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: Vec = nums_str.iter().map(|s| s.parse::().unwrap()).collect(); + + return nums; +} + +fn process_race_result(time: u32, distance: u32) -> u32 { + + 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()) +} \ No newline at end of file