Implemented day 2.
This commit is contained in:
@@ -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>>()
|
||||
}
|
||||
@@ -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()?)
|
||||
}
|
||||
|
||||
@@ -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
130
src/two/mod.rs
Normal 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))
|
||||
}
|
||||
Reference in New Issue
Block a user