Adds Day 6
This commit is contained in:
parent
5adbbfc276
commit
77977f9bbc
2
resources/input6.txt
Normal file
2
resources/input6.txt
Normal file
@ -0,0 +1,2 @@
|
||||
Time: 62 64 91 90
|
||||
Distance: 553 1010 1473 1074
|
98
src/days/day6.rs
Normal file
98
src/days/day6.rs
Normal file
@ -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<Self, Self::Err> {
|
||||
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::<u64>()?;
|
||||
|
||||
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::<u64>()?;
|
||||
|
||||
Ok(Record{time, distance})
|
||||
}
|
||||
}
|
||||
|
||||
struct RecordsOverview {
|
||||
records: Vec<Record>
|
||||
}
|
||||
|
||||
impl FromStr for RecordsOverview {
|
||||
type Err = AdventError;
|
||||
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
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<u64, AdventError> {
|
||||
Ok(s.parse::<u64>()?)
|
||||
})
|
||||
.collect::<Result<Vec<u64>, 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<u64, AdventError> {
|
||||
Ok(s.parse::<u64>()?)
|
||||
})
|
||||
.collect::<Result<Vec<u64>, AdventError>>()?;
|
||||
|
||||
let records: Vec<Record> = zip(times, distances).map(|(time, distance)| {
|
||||
Record{time, distance}
|
||||
}).collect_vec();
|
||||
|
||||
Ok(RecordsOverview {
|
||||
records
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub fn one() -> Result<u32, AdventError> {
|
||||
let overview = read_into::<RecordsOverview>(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<u32, AdventError> {
|
||||
let record = read_into::<Record>(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)
|
||||
}
|
@ -2,4 +2,5 @@ pub mod day1;
|
||||
pub mod day2;
|
||||
pub mod day3;
|
||||
pub mod day4;
|
||||
pub mod day5;
|
||||
pub mod day5;
|
||||
pub mod day6;
|
@ -16,6 +16,7 @@ fn main() -> Result<(), AdventError> {
|
||||
(day3::one as fn() -> Result<u32, AdventError>, day3::two as fn() -> Result<u32, AdventError>),
|
||||
(day4::one as fn() -> Result<u32, AdventError>, day4::two as fn() -> Result<u32, AdventError>),
|
||||
(day5::one as fn() -> Result<u32, AdventError>, day5::two as fn() -> Result<u32, AdventError>),
|
||||
(day6::one as fn() -> Result<u32, AdventError>, day6::two as fn() -> Result<u32, AdventError>),
|
||||
];
|
||||
|
||||
if env::args().len() != 3 {
|
||||
|
Loading…
Reference in New Issue
Block a user