use std::{collections::HashMap, marker::PhantomData}; #[derive(Debug)] struct Race { times: Vec, distances: Vec, state: std::marker::PhantomData, } struct Part1; struct Part2; impl From<&str> for Race { fn from(value: &str) -> Self { let (times, distances) = value.split_once("\n").unwrap(); let (_, times) = times.split_once(':').unwrap(); let (_, distances) = distances.split_once(':').unwrap(); let times = times .split_whitespace() .map(|x| x.parse::().unwrap()) .collect::>(); let distances = distances .split_whitespace() .map(|x| x.parse::().unwrap()) .collect::>(); Self { times, distances, state: PhantomData::, } } } impl From<&str> for Race { fn from(value: &str) -> Self { let (times, distances) = value.split_once("\n").unwrap(); let (_, times) = times.split_once(':').unwrap(); let (_, distances) = distances.split_once(':').unwrap(); let times = times .chars() .filter(|ch| ch.is_digit(10)) .collect::() .parse::() .unwrap(); let distances = distances .chars() .filter(|ch| ch.is_digit(10)) .collect::() .parse::() .unwrap(); Self { times: vec![times], distances: vec![distances], state: PhantomData::, } } } fn solve_part_one(data: &str) -> u64 { let race: Race = Race::from(data); let mut cnt = 0; let mut ans = 1; for (i, _) in race.times.iter().enumerate() { for t in 0..=race.times[i] { if (race.times[i] - t) * t > race.distances[i] { cnt += 1; } } ans *= cnt; cnt = 0; } ans } fn solve_part_two(data: &str) -> u64 { let race: Race = Race::from(data); let mut ans = 0; for t in 0..=race.times[0] { if (race.times[0] - t) * t > race.distances[0] { ans += 1; } } ans } fn main() { let test = include_str!("../input/day6.test"); let prod = include_str!("../input/day6.prod"); println!("part_1 test: {:?}", solve_part_one(test)); println!("part_1 prod {:?}", solve_part_one(prod)); println!("part_2 test: {:?}", solve_part_two(test)); println!("part_2 prod {:?}", solve_part_two(prod)); }