Implemented day 2.

This commit is contained in:
Dennis Brentjes 2023-12-02 12:29:28 +01:00
parent 2524727f26
commit 0ad7cd3587
5 changed files with 277 additions and 35 deletions

100
resources/input2.txt Normal file
View File

@ -0,0 +1,100 @@
Game 1: 1 blue; 4 green, 5 blue; 11 red, 3 blue, 11 green; 1 red, 10 green, 4 blue; 17 red, 12 green, 7 blue; 3 blue, 19 green, 15 red
Game 2: 17 red, 10 green; 3 blue, 17 red, 7 green; 10 green, 1 blue, 10 red; 7 green, 15 red, 1 blue; 7 green, 8 blue, 16 red; 18 red, 5 green, 3 blue
Game 3: 10 blue, 3 green, 8 red; 15 green, 14 blue, 1 red; 8 blue, 11 red, 2 green; 5 red, 9 green, 6 blue
Game 4: 1 red, 3 blue; 3 blue, 3 green, 1 red; 11 blue, 2 green; 2 green, 14 blue; 1 green, 7 blue; 11 blue, 5 green
Game 5: 9 green, 5 red, 10 blue; 9 red, 4 blue, 12 green; 9 green, 6 blue, 14 red; 16 green, 8 red, 6 blue; 11 blue, 13 red, 1 green
Game 6: 1 blue, 2 green, 16 red; 1 green, 19 red; 1 blue; 3 blue, 2 red, 1 green; 18 red, 2 blue, 1 green
Game 7: 2 blue, 9 red, 5 green; 11 blue, 6 green, 4 red; 7 red, 3 green, 5 blue; 5 green, 11 blue, 7 red; 17 blue, 4 red, 3 green; 20 blue, 1 green, 2 red
Game 8: 1 green, 6 red, 4 blue; 8 green, 4 blue, 2 red; 2 blue, 5 green
Game 9: 1 green, 5 blue; 4 blue; 2 red, 1 blue
Game 10: 1 green, 12 blue; 6 red, 4 green; 5 green, 14 blue, 9 red; 6 red, 13 blue, 2 green; 6 red, 17 blue, 1 green
Game 11: 19 green, 1 red; 15 red, 7 green; 1 blue, 8 red, 14 green; 1 blue, 11 green, 1 red
Game 12: 4 red, 3 green, 12 blue; 4 green, 13 red; 2 green, 15 blue, 5 red; 5 red, 10 blue, 3 green; 3 green, 17 blue, 17 red; 1 red, 4 green, 15 blue
Game 13: 5 red, 7 blue; 6 red, 1 green, 11 blue; 17 blue, 11 green, 4 red; 6 red, 9 green, 5 blue; 4 green, 14 blue
Game 14: 6 red, 15 blue, 3 green; 4 green, 4 blue, 3 red; 3 blue, 1 green, 5 red
Game 15: 4 green, 3 blue, 6 red; 3 blue, 2 red, 4 green; 15 red, 3 green, 4 blue; 11 red, 2 blue, 1 green
Game 16: 4 green, 1 blue, 12 red; 10 green, 6 blue, 10 red; 1 blue, 2 green, 15 red; 1 green, 3 red, 4 blue
Game 17: 10 green, 11 blue; 13 green, 10 blue, 3 red; 8 red, 3 green, 10 blue
Game 18: 3 red; 4 red, 1 blue; 3 green, 3 red; 10 green, 1 blue; 4 red, 6 green, 1 blue; 3 green
Game 19: 4 red, 6 green; 11 red, 4 blue, 2 green; 4 green, 8 red; 9 red, 7 green, 2 blue; 13 red, 4 blue; 9 red
Game 20: 2 blue, 1 green, 8 red; 3 green, 11 blue, 1 red; 5 blue, 4 red, 6 green; 6 green, 7 red, 5 blue; 7 red, 2 green, 1 blue
Game 21: 7 blue, 3 green; 2 green, 2 red, 7 blue; 6 blue, 3 red, 1 green; 9 blue, 1 green, 3 red
Game 22: 7 red, 5 blue, 9 green; 6 red, 1 blue, 5 green; 18 red, 7 green, 5 blue; 13 red, 11 green, 1 blue; 13 red, 1 blue, 11 green
Game 23: 10 green, 1 blue, 5 red; 2 red, 4 green; 9 green, 2 red; 10 green, 1 blue, 5 red
Game 24: 2 red, 6 green, 16 blue; 3 blue, 12 red, 3 green; 6 blue, 4 red, 12 green; 12 green, 14 blue, 3 red
Game 25: 5 red, 2 blue, 1 green; 4 blue, 14 red, 2 green; 16 blue, 4 red; 5 red; 12 blue, 16 red; 2 red, 6 blue, 1 green
Game 26: 10 blue, 6 green, 5 red; 2 red, 2 green; 8 blue, 5 red; 7 blue, 11 green; 8 green, 9 blue
Game 27: 1 red, 2 green; 6 green, 10 blue; 1 red, 6 green, 11 blue; 4 blue, 1 green, 1 red; 1 red, 7 blue; 5 green, 2 blue, 1 red
Game 28: 6 blue, 5 red, 3 green; 5 blue, 1 green; 1 green, 8 red, 1 blue; 2 blue, 4 green; 4 red, 5 blue
Game 29: 2 red, 4 green, 6 blue; 6 blue, 2 green, 1 red; 10 green, 13 blue, 2 red; 11 green, 11 blue, 2 red; 5 red, 10 green
Game 30: 4 green, 4 blue, 5 red; 6 red, 18 green, 7 blue; 1 red, 11 green, 2 blue; 11 red, 2 blue, 1 green
Game 31: 1 red, 7 green, 2 blue; 5 red, 6 green, 9 blue; 3 green, 1 red, 9 blue; 3 red; 8 blue, 6 green, 1 red; 14 blue, 4 green, 7 red
Game 32: 11 red, 2 blue, 2 green; 18 blue, 1 green; 6 blue, 1 green, 15 red; 4 red, 2 green; 13 red, 6 green, 5 blue; 7 blue, 5 green
Game 33: 12 blue, 2 red, 2 green; 5 blue, 3 green; 2 green, 6 red, 4 blue; 12 blue, 13 red; 10 blue, 12 red, 1 green; 4 blue, 4 red
Game 34: 2 blue, 1 red, 6 green; 2 blue, 1 green; 1 red, 2 green, 2 blue; 17 red, 1 blue, 2 green; 4 green, 2 red
Game 35: 2 blue, 12 red, 2 green; 2 green, 8 red, 11 blue; 17 red, 16 blue; 8 blue, 8 red; 1 green, 6 red, 9 blue
Game 36: 6 red, 14 green, 7 blue; 1 green, 1 red, 6 blue; 9 red, 10 blue, 9 green; 11 blue, 14 green, 8 red; 10 red, 6 green, 3 blue
Game 37: 6 green; 2 red, 5 green; 4 blue, 4 red, 5 green; 1 red, 7 green, 4 blue
Game 38: 1 green, 8 red, 10 blue; 3 green, 5 red, 12 blue; 13 blue, 5 red, 1 green; 3 green, 1 red; 3 red, 15 blue, 6 green; 3 green, 6 red, 4 blue
Game 39: 15 green, 3 blue; 3 green; 3 blue, 3 red, 8 green; 4 blue, 10 green, 4 red; 2 green, 5 blue, 4 red; 3 green, 3 blue, 1 red
Game 40: 2 red, 3 green, 15 blue; 5 blue, 13 green, 2 red; 11 blue, 1 red; 5 blue, 20 green, 5 red
Game 41: 1 red, 2 green, 1 blue; 10 red; 11 green, 1 blue, 5 red; 10 red, 13 green
Game 42: 10 red, 8 green, 2 blue; 5 green, 4 red; 6 red; 5 red, 9 green, 2 blue; 2 blue, 2 red, 3 green; 1 blue, 7 red, 2 green
Game 43: 8 red, 6 blue, 12 green; 11 green, 2 red, 2 blue; 4 blue, 3 red, 8 green; 14 red, 3 blue; 9 green, 1 blue, 5 red
Game 44: 4 red, 5 blue, 4 green; 9 blue, 1 green; 10 green, 2 blue, 4 red; 5 red, 15 green, 12 blue
Game 45: 1 red, 2 green; 5 blue; 3 blue, 1 red; 1 blue; 4 green, 1 red
Game 46: 3 green, 8 red, 5 blue; 1 blue, 10 red, 5 green; 2 green, 5 red, 3 blue; 5 green, 4 red, 13 blue
Game 47: 2 green, 1 blue; 2 red, 2 green, 6 blue; 1 green, 1 red, 1 blue; 2 green, 4 blue, 4 red; 2 green, 6 blue, 3 red
Game 48: 4 blue, 1 green, 2 red; 6 blue, 1 red; 4 blue
Game 49: 16 blue, 18 green, 13 red; 7 red, 13 blue; 14 green, 12 red; 20 green, 14 red, 12 blue
Game 50: 8 blue, 2 red, 1 green; 4 red, 2 green, 5 blue; 6 blue, 2 green; 8 blue, 3 green, 5 red; 1 green, 4 blue
Game 51: 2 blue, 8 red; 2 green, 3 red, 2 blue; 12 green; 3 blue, 10 green, 1 red; 13 green, 3 blue, 5 red
Game 52: 12 blue, 5 red, 16 green; 4 blue, 13 green; 2 red, 5 blue, 5 green; 4 green, 8 blue, 3 red
Game 53: 7 blue, 5 green; 8 green, 8 blue; 4 red, 1 green; 8 blue, 5 red, 11 green
Game 54: 4 blue, 3 green, 1 red; 10 green, 4 red, 7 blue; 1 red, 6 blue, 2 green
Game 55: 2 red, 10 blue; 13 red, 10 blue; 4 red, 5 blue, 4 green; 3 green, 8 blue; 5 blue, 7 red, 2 green
Game 56: 6 green, 7 blue, 9 red; 3 blue, 4 green; 15 red, 3 blue, 3 green; 5 red, 8 green, 7 blue
Game 57: 9 red, 13 blue, 2 green; 3 red, 2 green, 3 blue; 1 blue, 5 red, 3 green; 9 blue, 2 green, 2 red; 3 red, 12 blue, 3 green; 2 green, 7 red
Game 58: 4 blue, 7 red; 2 blue, 5 green, 1 red; 17 blue, 3 red; 5 green, 1 blue, 4 red; 4 green, 14 blue, 3 red
Game 59: 5 blue, 4 red; 7 red; 4 blue, 4 red, 1 green; 1 green, 1 blue, 5 red
Game 60: 4 red, 2 green; 1 green, 2 blue; 2 red, 2 green; 4 red, 2 green; 3 red
Game 61: 6 red, 14 green; 12 green, 3 blue; 3 blue, 16 green, 11 red
Game 62: 12 green, 2 red, 1 blue; 14 red, 2 blue, 4 green; 7 red, 5 green, 1 blue; 4 green, 14 red, 1 blue; 2 blue, 7 green, 3 red; 14 red, 9 green, 2 blue
Game 63: 1 red, 8 blue, 12 green; 3 green, 10 blue, 4 red; 2 red, 1 blue, 2 green; 2 red, 8 green; 4 red, 3 blue, 9 green; 13 green, 4 blue
Game 64: 4 blue, 9 green, 4 red; 10 green, 6 blue, 7 red; 10 green, 8 red, 1 blue; 13 blue, 8 green, 8 red; 4 green, 1 red, 8 blue
Game 65: 16 blue, 5 red, 13 green; 5 red, 9 green, 10 blue; 2 green, 14 red; 6 red, 5 green, 5 blue; 19 blue, 4 green, 14 red; 7 red, 1 blue, 4 green
Game 66: 7 blue, 11 green, 5 red; 7 green, 8 red, 10 blue; 3 red, 1 green
Game 67: 9 green, 2 blue, 13 red; 11 red, 10 blue; 14 red, 1 green, 1 blue; 1 red, 13 green, 6 blue; 7 blue, 3 green, 5 red; 3 green, 2 blue
Game 68: 6 green, 2 blue; 1 blue, 3 red, 5 green; 1 blue, 10 green
Game 69: 4 red, 6 blue, 1 green; 6 blue, 2 red, 4 green; 9 green, 7 blue, 2 red
Game 70: 11 blue, 1 green, 4 red; 7 blue, 1 green; 6 red, 9 blue; 1 green, 3 red, 7 blue; 1 green, 9 blue, 2 red
Game 71: 2 green, 6 red; 1 blue, 2 green, 15 red; 6 red, 1 blue; 2 green, 5 red
Game 72: 5 green, 6 red; 8 red, 1 blue, 12 green; 1 blue, 9 red, 15 green; 11 green, 1 blue, 6 red; 14 green, 5 red
Game 73: 9 green, 13 red, 1 blue; 14 green, 5 blue, 13 red; 13 green, 10 red, 16 blue; 3 blue, 7 red, 1 green; 7 red, 6 green, 11 blue
Game 74: 1 blue, 9 red, 15 green; 3 blue, 7 green; 9 green, 1 blue, 7 red
Game 75: 5 blue, 12 red, 2 green; 2 blue, 2 green; 4 green, 2 red, 7 blue; 4 green, 7 blue, 5 red; 1 green, 7 blue, 3 red
Game 76: 1 blue; 8 red, 13 blue; 4 green, 7 blue, 7 red; 3 red, 12 blue, 2 green; 2 green, 2 blue, 1 red
Game 77: 4 blue; 4 blue; 1 blue, 1 red; 1 red, 5 blue; 10 blue, 1 green
Game 78: 3 blue, 10 green, 6 red; 12 red, 7 blue, 8 green; 2 green, 18 red, 5 blue; 2 red, 15 blue, 14 green; 4 green, 6 blue, 13 red
Game 79: 7 red, 1 green; 1 blue, 6 red, 2 green; 1 blue, 12 red
Game 80: 4 red, 6 blue, 2 green; 5 blue, 2 red; 6 blue, 7 red, 2 green
Game 81: 2 green, 9 red; 2 green, 2 blue, 7 red; 12 red, 7 green; 8 green, 3 red, 3 blue
Game 82: 4 green, 5 blue; 2 red, 16 blue; 2 red, 2 green, 18 blue
Game 83: 14 red, 2 green; 3 blue, 16 red, 2 green; 4 green, 13 red, 1 blue
Game 84: 10 green, 6 blue, 2 red; 5 red, 6 blue; 7 green, 6 red, 9 blue
Game 85: 1 red; 12 red, 1 blue, 2 green; 6 red, 1 green; 12 red, 2 green
Game 86: 14 red, 1 green, 3 blue; 3 blue; 4 green, 8 red, 2 blue; 10 red, 2 green
Game 87: 4 red, 9 green, 8 blue; 3 green, 6 blue, 7 red; 4 blue, 1 red; 2 red, 7 blue, 11 green; 8 green, 2 blue, 5 red; 6 blue, 10 green, 8 red
Game 88: 1 green, 1 red; 2 green, 1 blue; 3 green, 1 red, 1 blue; 4 green; 1 blue, 3 green, 1 red
Game 89: 11 green, 7 blue, 8 red; 7 blue, 3 green, 2 red; 7 green, 6 red, 4 blue; 1 blue, 2 green, 10 red; 3 red, 2 blue, 1 green
Game 90: 10 green, 12 red, 2 blue; 7 red, 7 blue, 8 green; 2 blue, 11 red, 7 green; 6 green, 5 red, 2 blue; 7 red, 10 green
Game 91: 12 red, 8 green; 8 red, 6 green, 3 blue; 12 red, 4 blue, 2 green
Game 92: 10 blue, 3 green; 4 red, 13 blue, 8 green; 7 green, 8 blue, 7 red
Game 93: 7 red, 4 green, 1 blue; 15 green, 4 red; 2 blue, 15 red
Game 94: 10 red, 10 green, 11 blue; 3 red, 1 green, 7 blue; 9 red, 4 green, 9 blue; 7 red, 9 green, 13 blue; 9 blue, 2 green, 10 red
Game 95: 4 blue, 12 green; 7 green, 1 red, 5 blue; 2 blue, 8 green, 8 red
Game 96: 12 green, 2 blue; 11 green, 3 blue; 3 red, 2 green, 5 blue; 12 green, 2 blue, 4 red; 2 blue, 1 green, 1 red; 3 red, 11 green, 3 blue
Game 97: 6 red, 3 blue, 1 green; 1 blue, 2 red, 14 green; 4 blue, 14 green
Game 98: 13 green, 1 red, 5 blue; 2 red, 5 green, 7 blue; 19 green, 5 blue; 4 blue, 13 green; 5 green, 8 blue
Game 99: 11 red, 8 green; 16 red, 10 green; 9 red, 6 green; 3 blue, 2 red, 4 green
Game 100: 4 red, 2 blue, 4 green; 2 green, 1 red, 1 blue; 3 green, 4 blue, 5 red; 18 red, 2 blue; 9 red, 5 green, 4 blue

View File

@ -1,7 +1,13 @@
use std::{path::Path, io::{BufReader, BufRead, self}, fs::File};
use std::{io::{BufReader, BufRead, self}, fs::File, str::FromStr, path::Path, convert::Infallible, num::ParseIntError};
#[derive(Debug)]
pub struct AdventError(String);
pub struct AdventError(pub String);
impl From<ParseIntError> for AdventError {
fn from(e: ParseIntError) -> Self {
AdventError(e.to_string())
}
}
impl From<io::Error> for AdventError {
fn from(e: io::Error) -> Self {
@ -9,8 +15,14 @@ impl From<io::Error> for AdventError {
}
}
pub fn read_into_vec(path: &Path) -> Result<Vec<String>, AdventError> {
let file = File::open(path).expect("no such file");
impl From<Infallible> for AdventError {
fn from(_: Infallible) -> Self {
unreachable!()
}
}
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);
buf.lines().map(|line| line.map_err(Into::into)).collect::<Result<Vec<String>, AdventError>>()
buf.lines().map(|line| T::from_str(line?.as_str()).map_err(Into::into)).collect::<Result<Vec<T>, AdventError>>()
}

View File

@ -1,12 +1,15 @@
pub mod input;
pub mod one;
pub mod two;
use input::AdventError;
use one::one_one;
use one::one_two;
use one::{one_one, one_two};
use two::{two_one, two_two};
fn main() -> Result<(), AdventError> {
one_one()?;
Ok(one_two()?)
one_two()?;
two_one()?;
Ok(two_two()?)
}

View File

@ -5,7 +5,7 @@ use crate::input::AdventError;
use super::input::read_into_vec;
pub fn process(lines: Vec::<String>) -> Result<u32, AdventError> {
fn process(lines: Vec::<String>) -> Result<u32, AdventError> {
let calibrations: Vec<u32> = lines.iter().map(|v| {
let first_digit = v.as_bytes()
.iter()
@ -28,31 +28,31 @@ pub fn process(lines: Vec::<String>) -> Result<u32, AdventError> {
}
pub fn one_one() -> Result<(), AdventError> {
let lines = read_into_vec(Path::new("resources/input1.txt"))?;
println!("{}", process(lines)?);
Ok(())
let lines: Vec<String> = read_into_vec::<String>(Path::new("resources/input1.txt"))?;
Ok(println!("{}", process(lines)?))
}
struct Mutation(usize, char);
struct Pattern(String, char);
struct Pattern(&'static str, char);
pub fn one_two() -> Result<(), AdventError> {
let lines = read_into_vec(Path::new("resources/input1.txt"))?;
let mut lines = read_into_vec(Path::new("resources/input1.txt"))?;
let new_lines: Vec<String> = lines.iter().map(|str| {
let patterns = vec![
Pattern("one".to_string(), '1'),
Pattern("two".to_string(), '2'),
Pattern("three".to_string(), '3'),
Pattern("four".to_string(), '4'),
Pattern("five".to_string(), '5'),
Pattern("six".to_string(), '6'),
Pattern("seven".to_string(), '7'),
Pattern("eight".to_string(), '8'),
Pattern("nine".to_string(), '9')
];
let mut mutations = Vec::<Mutation>::new();
mutations.reserve(10);
let mut mutations = Vec::<Mutation>::new();
let new_lines: Vec<String> = lines.iter_mut().map(|str: &mut String| {
let patterns = vec![
Pattern("one", '1'),
Pattern("two", '2'),
Pattern("three", '3'),
Pattern("four", '4'),
Pattern("five", '5'),
Pattern("six", '6'),
Pattern("seven", '7'),
Pattern("eight", '8'),
Pattern("nine", '9')
];
for pattern in patterns.iter() {
let indices = str.match_indices(&pattern.0);
@ -65,20 +65,17 @@ pub fn one_two() -> Result<(), AdventError> {
mutations.sort_by(|l, r| l.0.cmp(&r.0));
let mut offset = 0;
let mut new_str = str.clone();
for mutation in mutations.iter() {
new_str.insert(mutation.0 + offset, mutation.1);
str.insert(mutation.0 + offset, mutation.1);
offset += 1;
}
new_str
mutations.clear();
str.to_owned()
}).collect();
println!("{}", process(new_lines)?);
Ok(())
Ok(println!("{}", process(new_lines)?))
}

130
src/two/mod.rs Normal file
View File

@ -0,0 +1,130 @@
use std::{path::Path, str::FromStr, cmp::max};
use crate::input::{AdventError, read_into_vec};
impl FromStr for Color {
type Err = AdventError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(match s {
"red" => Color::Red,
"green" => Color::Green,
"blue" => Color::Blue,
_ => return Err(AdventError("No matching Color enum.".to_string()))
})
}
}
impl FromStr for Group {
type Err = AdventError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let split = s.split(' ').collect::<Vec<&str>>();
Ok(Group{count: u32::from_str(split[0])?, color: Color::from_str(split[1])?})
}
}
impl FromStr for Pull {
type Err = AdventError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let mut res = Vec::<Group>::new();
for group in s.split(';') {
let group = group.trim();
res.push(Group::from_str(group)?);
}
Ok(Pull{groups: res})
}
}
impl FromStr for Game {
type Err = AdventError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let split = s.split(':').collect::<Vec<&str>>();
let id = u32::from_str(split[0].split(' ').collect::<Vec<&str>>()[1])?;
let pulls = split[1];
let mut res = Vec::<Pull>::new();
for pull in pulls.split(',') {
let pull = pull.trim();
res.push(Pull::from_str(pull)?);
}
Ok(Game{id: id, pulls: res})
}
}
#[derive(Debug)]
enum Color {
Red,
Green,
Blue
}
#[derive(Debug)]
struct Group {
count: u32,
color: Color,
}
#[derive(Debug)]
struct Pull {
groups: Vec<Group>
}
#[derive(Debug)]
struct Game {
id: u32,
pulls: Vec<Pull>
}
pub fn two_one() -> Result<(), AdventError> {
let games: Vec<Game> = read_into_vec(Path::new("resources/input2.txt"))?;
let mut sum: u32 = 0;
let max_red = 12;
let max_green = 13;
let max_blue = 14;
for game in games.iter() {
let mut valid: bool = true;
for pull in game.pulls.iter() {
for group in pull.groups.iter() {
match group.color {
Color::Red => if group.count > max_red { valid = false },
Color::Green => if group.count > max_green { valid = false },
Color::Blue => if group.count > max_blue { valid = false },
}
if valid == false { break };
}
if valid == false { break };
valid = true;
}
if valid { sum += game.id }
}
Ok(println!("{}", sum))
}
pub fn two_two() -> Result<(), AdventError> {
let games: Vec<Game> = read_into_vec(Path::new("resources/input2.txt"))?;
let mut power: u32 = 0;
for game in games.iter() {
let mut max_nr_red = 0;
let mut max_nr_green = 0;
let mut max_nr_blue = 0;
for pull in game.pulls.iter() {
for group in pull.groups.iter() {
match group.color {
Color::Red => max_nr_red = max(max_nr_red, group.count),
Color::Green => max_nr_green = max(max_nr_green, group.count),
Color::Blue => max_nr_blue = max(max_nr_blue, group.count)
}
}
}
power += max_nr_red * max_nr_green * max_nr_blue;
}
Ok(println!("{}", power))
}