2023-06: Implement part one
This commit is contained in:
parent
aa81576743
commit
ef364a9ae0
|
@ -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]
|
|
@ -0,0 +1,2 @@
|
|||
Time: 51 92 68 90
|
||||
Distance: 222 2031 1126 1225
|
|
@ -0,0 +1,2 @@
|
|||
Time: 7 15 30
|
||||
Distance: 9 40 200
|
|
@ -0,0 +1,5 @@
|
|||
mod part_one;
|
||||
|
||||
fn main() {
|
||||
part_one::part_one();
|
||||
}
|
|
@ -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<u32>> = 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<u32> {
|
||||
|
||||
let nums_str = s.split(':').collect::<Vec<&str>>()[1].split_whitespace().collect::<Vec<&str>>();
|
||||
let nums: Vec<u32> = nums_str.iter().map(|s| s.parse::<u32>().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<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>>
|
||||
where
|
||||
P: AsRef<Path>,
|
||||
{
|
||||
let file = File::open(filename)?;
|
||||
Ok(io::BufReader::new(file).lines())
|
||||
}
|
Loading…
Reference in New Issue