From: Greg Burri Date: Fri, 2 Dec 2022 20:32:15 +0000 (+0100) Subject: Day 2 X-Git-Url: https://git.euphorik.ch/?a=commitdiff_plain;h=07f2b51fe6b5add74c96436c3002756b4b035416;p=advent_of_code_2022.git Day 2 --- diff --git a/Cargo.toml b/Cargo.toml index bf82e1f..0d0f199 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,4 +11,9 @@ itertools = "0.10" threadpool = "1.8" regex = "1" num = "0.4" -num_enum = "0.5" \ No newline at end of file +num_enum = "0.5" + +[profile.release] +opt-level = 3 +lto = true +codegen-units = 1 \ No newline at end of file diff --git a/src/day01.rs b/src/day01.rs index d88d000..0a397c0 100644 --- a/src/day01.rs +++ b/src/day01.rs @@ -24,10 +24,9 @@ pub fn get_most_calories(calories: &[i64]) -> i64 { } pub fn get_sum_most_three_calories(calories: &[i64]) -> i64 { - calories.iter().map(Reverse).k_smallest(3).map(|n| n.0).sum() + calories.iter().map(Reverse).k_smallest(3).fold(0, |s, v| s + v.0) } - #[cfg(test)] mod tests { use super::*; diff --git a/src/day02.rs b/src/day02.rs new file mode 100644 index 0000000..cebdb21 --- /dev/null +++ b/src/day02.rs @@ -0,0 +1,89 @@ +use std::io::BufRead; + +#[derive(PartialEq, Clone, Copy)] +pub enum Shape { + Rock, + Paper, + Scissors, +} + +impl Shape { + pub fn battle(&self, other: &Shape) -> i32 { + (match (self, other) { + (Shape::Rock, Shape::Scissors) | + (Shape::Paper, Shape::Rock) | + (Shape::Scissors, Shape::Paper) => 6, + (a, b) if a == b => 3, + _ => 0, + }) + + match self { Shape::Rock => 1, Shape::Paper => 2, Shape::Scissors => 3, } + } + + pub fn parse(str: &str) -> Self { + match str { + "A" | "X" => Shape::Rock, + "B" | "Y" => Shape::Paper, + "C" | "Z" => Shape::Scissors, + _ => panic!("Unknown letter: {}", str) + } + } +} + +pub fn read_shapes(reader: R) -> Vec<(Shape, Shape)> +where + R: BufRead +{ + let mut shapes: Vec<(Shape, Shape)> = Vec::new(); + for l in reader.lines() { + let s: Vec = l.unwrap().trim().split(' ').map(Shape::parse).collect(); + shapes.push((s[0], s[1])); + } + shapes +} + +pub fn read_shapes_2(reader: R) -> Vec<(Shape, Shape)> +where + R: BufRead +{ + let mut shapes: Vec<(Shape, Shape)> = Vec::new(); + for l in reader.lines() { + let l = l.unwrap(); + let l: Vec<&str> = l.trim().split(' ').collect(); + let s1: Shape = Shape::parse(l[0]); + let s2 = match l[1] { + "X" => match s1 { Shape::Rock => Shape::Scissors, Shape::Paper => Shape::Rock, Shape::Scissors => Shape::Paper, }, // Need to lose. + "Z" => match s1 { Shape::Rock => Shape::Paper, Shape::Paper => Shape::Scissors, Shape::Scissors => Shape::Rock, }, // Need to win + _ => s1, // Draw. + }; + + shapes.push((s1, s2)); + } + shapes +} + +pub fn get_score(shapes: &[(Shape, Shape)]) -> i32 { + shapes.iter().fold(0, |sum, (s1, s2)| sum + s2.battle(s1)) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn part1() { + let stategy_guide = + "A Y + B X + C Z"; + assert_eq!(get_score(&read_shapes(stategy_guide.as_bytes())), 15); + } + + #[test] + fn part2() { + let stategy_guide = + "A Y + B X + C Z"; + assert_eq!(get_score(&read_shapes_2(stategy_guide.as_bytes())), 12); + } +} \ No newline at end of file diff --git a/src/day03.rs b/src/day03.rs new file mode 100644 index 0000000..fb593c2 --- /dev/null +++ b/src/day03.rs @@ -0,0 +1,31 @@ +use std::io::BufRead; + +pub fn read_(reader: R) -> Vec +where + R: BufRead +{ + /* + let mut shapes: Vec<(Shape, Shape)> = Vec::new(); + for l in reader.lines() { + let s: Vec = l.unwrap().trim().split(' ').map(Shape::parse).collect(); + shapes.push((s[0], s[1])); + } + shapes + */ + Vec::new() +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn part1() { + assert_eq!(1, 1); + } + + #[test] + fn part2() { + assert_eq!(2, 2); + } +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index a236cec..6a78796 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,15 +1,28 @@ -use std::{env, fs, io, time::Instant}; +use std::{env, fs, time::Instant, io::{BufReader, Seek, SeekFrom}}; mod common; mod day01; +mod day02; +mod day03; fn day01() -> String { - //let report = common::read_list_of_numbers("data/day01.input", "\n"); let f = fs::File::open("data/day01.input").unwrap(); - let calories = day01::read_calories(std::io::BufReader::new(f)); + let calories = day01::read_calories(BufReader::new(f)); format!("part1: {}, part2: {}", day01::get_most_calories(&calories), day01::get_sum_most_three_calories(&calories)) } +fn day02() -> String { + let mut f = fs::File::open("data/day02.input").unwrap(); + let shapes = day02::read_shapes(BufReader::new(f.try_clone().unwrap())); + let _ = f.seek(SeekFrom::Start(0)); + let shapes_2 = day02::read_shapes_2(BufReader::new(f)); + format!("part1: {}, part2: {}", day02::get_score(&shapes), day02::get_score(&shapes_2)) +} + +fn day03() -> String { + format!("part1: {}, part2: {}", 0, 0) +} + fn format_micros(t: u128) -> String { if t < 10_000 { format!("{} μs", t) @@ -30,6 +43,8 @@ fn main() { let days: Vec String> = vec!( day01, + day02, + day03, ); let args: Vec = env::args().skip(1).collect();