diff --git a/resources/input6.txt b/resources/input6.txt new file mode 100644 index 0000000..a15dea2 --- /dev/null +++ b/resources/input6.txt @@ -0,0 +1,2 @@ +Time: 62 64 91 90 +Distance: 553 1010 1473 1074 \ No newline at end of file diff --git a/src/days/day6.rs b/src/days/day6.rs new file mode 100644 index 0000000..d68f8b0 --- /dev/null +++ b/src/days/day6.rs @@ -0,0 +1,98 @@ +use std::{path::Path, str::FromStr, iter::zip}; + +use itertools::Itertools; + +use crate::{error::AdventError, input::read_into}; + +struct Record { + time: u64, + distance: u64, +} + +impl FromStr for Record { + type Err = AdventError; + + fn from_str(s: &str) -> Result { + let mut lines = s.lines(); + let time = lines + .nth(0).ok_or(AdventError("A zero'th row doesn't exist".into()))? + .trim() + .split_ascii_whitespace() + .skip(1) + .fold("".to_string(), |mut sb, s| { + sb.push_str(s); + sb + }).parse::()?; + + let distance = lines + .nth(0).ok_or(AdventError("A zero'th row doesn't exist".into()))? + .trim() + .split_ascii_whitespace() + .skip(1) + .fold("".to_string(), |mut sb, s| { + sb.push_str(s); + sb + }).parse::()?; + + Ok(Record{time, distance}) + } +} + +struct RecordsOverview { + records: Vec +} + +impl FromStr for RecordsOverview { + type Err = AdventError; + + fn from_str(s: &str) -> Result { + let mut lines = s.lines(); + let times = lines + .nth(0).ok_or(AdventError("A zero'th row doesn't exist".into()))? + .trim() + .split_ascii_whitespace() + .skip(1) + .map(|s| -> Result { + Ok(s.parse::()?) + }) + .collect::, AdventError>>()?; + + let distances = lines + .nth(0).ok_or(AdventError("A zero'th row doesn't exist".into()))? + .trim() + .split_ascii_whitespace() + .skip(1) + .map(|s| -> Result { + Ok(s.parse::()?) + }) + .collect::, AdventError>>()?; + + let records: Vec = zip(times, distances).map(|(time, distance)| { + Record{time, distance} + }).collect_vec(); + + Ok(RecordsOverview { + records + }) + } +} + +pub fn one() -> Result { + let overview = read_into::(Path::new("resources/input6.txt"))?; + Ok(overview.records.iter().fold(1, |acc, record| { + acc * ((0..(record.time + 1)).into_iter().map(|x| { + x * (record.time - x) + }).filter(|x| { + *x > record.distance + }).count() as u32) + })) +} + +pub fn two() -> Result { + let record = read_into::(Path::new("resources/input6.txt"))?; + Ok((0..(record.time + 1)).into_iter().map(|x| { + x * (record.time - x) + }).filter(|x| { + *x > record.distance + }).count() as u32) +} \ No newline at end of file diff --git a/src/days/mod.rs b/src/days/mod.rs index c64c2fc..889dd8d 100644 --- a/src/days/mod.rs +++ b/src/days/mod.rs @@ -2,4 +2,5 @@ pub mod day1; pub mod day2; pub mod day3; pub mod day4; -pub mod day5; \ No newline at end of file +pub mod day5; +pub mod day6; \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 2ffabfd..3b9b266 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,6 +16,7 @@ fn main() -> Result<(), AdventError> { (day3::one as fn() -> Result, day3::two as fn() -> Result), (day4::one as fn() -> Result, day4::two as fn() -> Result), (day5::one as fn() -> Result, day5::two as fn() -> Result), + (day6::one as fn() -> Result, day6::two as fn() -> Result), ]; if env::args().len() != 3 {