diff --git a/input/day6.txt b/input/day6.txt index e69de29..640d6bb 100644 --- a/input/day6.txt +++ b/input/day6.txt @@ -0,0 +1,2 @@ +Time: 47 70 75 66 +Distance: 282 1079 1147 1062 \ No newline at end of file diff --git a/src/day6.rs b/src/day6.rs index b0a54a3..89583c4 100644 --- a/src/day6.rs +++ b/src/day6.rs @@ -1,26 +1,144 @@ -pub fn day6_part1(_input: &str) -> String { - todo!(); +use itertools::Itertools; + +#[derive(Debug, PartialEq)] +struct Race { + time: u64, + distance: u64, +} +fn races(input: &str) -> Vec { + println!("{}", input); + input + .trim() + .lines() + .chunks(2) + .into_iter() + .map(|lines| { + let lines = lines.collect_vec(); + let times = lines[0]; + let distances = lines[1]; + + let times = times + .split(":") + .last() + .unwrap() + .trim() + .split(" ") + .filter_map(|t| t.trim().parse::().ok()); + let distances = distances + .split(":") + .last() + .unwrap() + .trim() + .split(" ") + .filter_map(|t| t.trim().parse::().ok()); + times + .zip(distances) + .map(|(time, distance)| Race { time, distance }) + .collect_vec() + }) + .flatten() + .collect::>() +} + +impl Race { + fn winning_hold_times(&self) -> u64 { + let mut winners: Vec = Vec::new(); + + for time in 1..self.time { + let time_to_travel = self.time - time; + let speed = time; + let distance_traveled = speed * time_to_travel; + if distance_traveled > self.distance { + winners.push(time) + } + } + winners.len() as u64 + } } -pub fn day6_part2(_input: &str) -> String { - todo!(); +pub fn day6_part1(input: &str) -> String { + println!("{}", &input); + let races = races(input); + dbg!(&races); + races + .iter() + .map(|race| race.winning_hold_times()) + .inspect(|i| println!("winning {i}")) + .product::() + .to_string() +} + +pub fn day6_part2(input: &str) -> String { + let input = input + .lines() + .map(|line| { + let line = line + .split(":") + .last() + .unwrap() + .chars() + .filter(|c| *c != ' ') + .collect::(); + let line = "Blah: ".to_string() + &line + "\n"; + dbg!(&line); + line + }) + .collect::(); + dbg!(&input); + day6_part1(&input) } #[cfg(test)] mod test { use super::*; - const INPUT: &str = ""; + const INPUT: &str = "Time: 7 15 30 +Distance: 9 40 200"; + + #[test] + fn test_parser() { + assert_eq!( + vec![ + Race { + time: 7, + distance: 9 + }, + Race { + time: 15, + distance: 40 + }, + Race { + time: 30, + distance: 200 + } + ], + races(INPUT) + ); + } + + #[test] + fn test_parser_p2() { + assert_eq!( + vec![Race { + time: 47707566, + distance: 282107911471062 + },], + races( + " +Blah: 47707566 +Blah: 282107911471062 +" + ) + ); + } #[test] - #[ignore] fn test_day6_part1() { - assert_eq!("0", day6_part1(INPUT)); + assert_eq!("288", day6_part1(INPUT)); } #[test] - #[ignore] fn test_day6_part2() { - assert_eq!("0", day6_part2(INPUT)); + assert_eq!("71503", day6_part2(INPUT)); } }