Added day 4

This commit is contained in:
Dennis Brentjes 2023-12-05 12:27:14 +01:00
parent 4a94c602f6
commit f0370944d3
2 changed files with 50 additions and 5 deletions

View File

@ -1,12 +1,28 @@
use std::{collections::HashSet, path::Path, str::FromStr};
use itertools::Itertools;
use crate::input::{AdventError, read_into_vec};
impl FromStr for ScratchGame {
type Err = AdventError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
todo!()
let split = s.split(':').collect_vec();
let id = split[0].split_whitespace().collect_tuple::<(&str, &str)>().ok_or(AdventError("Game Id not found.".into()))?.1.parse::<i32>()?;
let split = split[1].split('|').collect_vec();
let my_numbers: HashSet<i32> = split[0].trim().split_whitespace().map(|x| {
x.parse::<i32>()
}).collect::<Result<HashSet<_>, _>>()?;
let winning_numbers: HashSet<i32> = split[1].trim().split_whitespace().map(|x| -> Result<i32, AdventError> {
x.parse::<i32>().map_err(Into::into)
}).collect::<Result<HashSet<_>, _>>()?;
Ok(ScratchGame {
id,
my_numbers,
winning_numbers
})
}
}
@ -17,10 +33,33 @@ struct ScratchGame {
}
pub fn one() -> Result<u32, AdventError> {
let games = read_into_vec::<ScratchGame>(Path::new("resources/input4.txt"));
Ok(0)
let games = read_into_vec::<ScratchGame>(Path::new("resources/input4.txt"))?;
Ok(games.into_iter().fold(Ok(0), |acc: Result<u64, AdventError>, game: ScratchGame| {
let acc = acc?;
let my_winning_numbers = game.my_numbers.intersection(&game.winning_numbers);
Ok(acc + match my_winning_numbers.count() {
0 => 0,
x => (2 as u64).pow((x-1).try_into()?)
})
})? as u32)
}
pub fn two() -> Result<u32, AdventError> {
Ok(0)
let games = read_into_vec::<ScratchGame>(Path::new("resources/input4.txt"))?;
let mut vec = Vec::<u32>::new();
vec.resize(games.len(), 1);
for game in games.into_iter() {
let my_winning_numbers = game.my_numbers.intersection(&game.winning_numbers);
let nr_free_tickets = my_winning_numbers.count() as i32;
for index in game.id..(game.id + nr_free_tickets ) {
let nr_to_count = vec[(game.id - 1) as usize];
for _ in 0..nr_to_count {
vec[index as usize] += 1;
}
}
}
Ok(vec.iter().sum())
}

View File

@ -1,4 +1,4 @@
use std::{io::{BufReader, BufRead, self}, fs::File, str::FromStr, path::Path, convert::Infallible, num::ParseIntError};
use std::{io::{BufReader, BufRead, self}, fs::File, str::FromStr, path::Path, convert::Infallible, num::{ParseIntError, TryFromIntError}};
#[derive(Debug)]
pub struct AdventError(pub String);
@ -21,6 +21,12 @@ impl From<Infallible> for AdventError {
}
}
impl From<TryFromIntError> for AdventError {
fn from(e: TryFromIntError) -> Self {
AdventError(e.to_string())
}
}
pub fn read_into_vec<T:FromStr>(file_path : &Path) -> Result<Vec<T>, AdventError> where AdventError: From<<T as FromStr>::Err> {
let file = File::open(file_path).expect("no such file");
let buf = BufReader::new(file);