From 07251c2305984e0076e0cf6d0da682ee4b0bf262 Mon Sep 17 00:00:00 2001 From: Daniel Seiller Date: Fri, 2 Dec 2022 10:50:12 +0100 Subject: [PATCH] Day 01 optimization + Day02 --- src/day01.rs | 46 ++++++++++-------- src/day02.rs | 131 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/dayXX.rs | 4 +- src/lib.rs | 8 ++-- 4 files changed, 164 insertions(+), 25 deletions(-) create mode 100644 src/day02.rs diff --git a/src/day01.rs b/src/day01.rs index 41d20bb..ab806dc 100644 --- a/src/day01.rs +++ b/src/day01.rs @@ -1,19 +1,27 @@ -use std::cmp::Reverse; - -use aoc_runner_derive::{aoc, aoc_generator}; -#[aoc_generator(day1)] -pub fn input_generator(input: &str) -> Vec { - input.split("\n\n").map(|chunk| chunk.lines().map(|line| line.trim().parse::().unwrap()).sum()).collect() -} - -#[aoc(day1, part1)] -pub fn solve_part1(input: &[usize]) -> usize { - input.iter().max().copied().unwrap_or(0) -} - -#[aoc(day1, part2)] -pub fn solve_part2(input: &[usize]) -> usize { - let mut scores: Vec = input.to_vec(); - scores.sort_by_key(|v| Reverse(*v)); - scores.iter().take(3).sum() -} +use std::cmp::Reverse; + +use aoc_runner_derive::{aoc, aoc_generator}; +#[aoc_generator(day1)] +pub fn input_generator(input: &str) -> Vec { + input + .split("\n\n") + .map(|chunk| { + chunk + .lines() + .map(|line| line.trim().parse::().unwrap()) + .sum() + }) + .collect() +} + +#[aoc(day1, part1)] +pub fn solve_part1(input: &[usize]) -> usize { + input.iter().max().copied().unwrap_or(0) +} + +#[aoc(day1, part2)] +pub fn solve_part2(input: &[usize]) -> usize { + let mut scores: Vec = input.to_vec(); + scores.sort_by_key(|v| Reverse(*v)); + scores.iter().take(3).sum() +} diff --git a/src/day02.rs b/src/day02.rs new file mode 100644 index 0000000..8b3be0a --- /dev/null +++ b/src/day02.rs @@ -0,0 +1,131 @@ +use std::str::FromStr; + +use aoc_runner_derive::{aoc, aoc_generator}; + +#[derive(Debug, PartialEq, Eq, Clone, Copy)] +pub enum Choice { + Rock, + Paper, + Scissors, +} + +#[derive(Debug, PartialEq, Eq, Clone, Copy)] +pub enum Outcome { + Win, + Loose, + Draw, +} + + +impl Outcome { + fn other(&self, choice: &Choice) -> Choice { + match (self,choice) { + (Outcome::Win, Choice::Rock) => Choice::Paper, + (Outcome::Win, Choice::Paper) => Choice::Scissors, + (Outcome::Win, Choice::Scissors) => Choice::Rock, + (Outcome::Loose, Choice::Rock) => Choice::Scissors, + (Outcome::Loose, Choice::Paper) => Choice::Rock, + (Outcome::Loose, Choice::Scissors) => Choice::Paper, + (Outcome::Draw, other) => *other, + } + } +} + +impl From for usize { + fn from(val: Outcome) -> Self { + match val { + Outcome::Win => 6, + Outcome::Loose => 0, + Outcome::Draw => 3, + } + } +} + +impl From<&Choice> for Outcome { + fn from(value: &Choice) -> Self { + match value { + Choice::Rock => Outcome::Loose, + Choice::Paper => Outcome::Draw, + Choice::Scissors => Outcome::Win, + } + } +} + +impl Choice { + fn shape_score(&self) -> usize { + match self { + Choice::Rock => 1, + Choice::Paper => 2, + Choice::Scissors => 3, + } + } + fn outcome(&self, other: &Self) -> Outcome { + match (self, other) { + (Choice::Rock, Choice::Scissors) => Outcome::Win, + (Choice::Paper, Choice::Rock) => Outcome::Win, + (Choice::Scissors, Choice::Paper) => Outcome::Win, + (Choice::Rock, Choice::Rock) => Outcome::Draw, + (Choice::Paper, Choice::Paper) => Outcome::Draw, + (Choice::Scissors, Choice::Scissors) => Outcome::Draw, + (Choice::Rock, Choice::Paper) => Outcome::Loose, + (Choice::Paper, Choice::Scissors) => Outcome::Loose, + (Choice::Scissors, Choice::Rock) => Outcome::Loose, + } + } +} + +impl FromStr for Choice { + type Err = (); + + fn from_str(s: &str) -> Result { + match s.to_ascii_uppercase().as_str() { + "A" | "X" => Ok(Self::Rock), + "B" | "Y" => Ok(Self::Paper), + "C" | "Z" => Ok(Self::Scissors), + _ => unreachable!(), + } + } +} + +#[aoc_generator(day2)] +pub fn input_generator(input: &str) -> Vec<(Choice, Choice)> { + input + .lines() + .map(|line| { + let rule = line + .split_ascii_whitespace() + .map(|r| r.parse().unwrap()) + .collect::>(); + (rule[0], rule[1]) + }) + .collect() +} + +#[aoc(day2, part1)] +pub fn solve_part1(input: &[(Choice, Choice)]) -> usize { + input + .iter() + .map(|(other, mine)| { + let shape_score = mine.shape_score(); + let outcome: usize = mine.outcome(other).into(); + shape_score + outcome + }) + .sum() +} + + + + +#[aoc(day2, part2)] +pub fn solve_part2(input: &[(Choice, Choice)]) -> usize { + input + .iter() + .map(|(other, goal)| { + let goal: Outcome = goal.into(); + let mine = goal.other(other); + let shape_score = mine.shape_score(); + let outcome: usize = goal.into(); + shape_score + outcome + }) + .sum() +} diff --git a/src/dayXX.rs b/src/dayXX.rs index bb03e4b..83475ea 100644 --- a/src/dayXX.rs +++ b/src/dayXX.rs @@ -5,11 +5,11 @@ pub fn input_generator(input: &str) -> _ { } #[aoc(dayX, part1)] -pub fn solve_part1(input: &[usize]) -> _ { +pub fn solve_part1(input: &[usize]) -> usize { todo!() } // #[aoc(dayX, part2)] -// pub fn solve_part2(input: &[usize]) -> _ { +// pub fn solve_part2(input: &[usize]) -> usize { // todo!() // } diff --git a/src/lib.rs b/src/lib.rs index 0d9c6bf..290d5dd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,4 @@ - -use aoc_runner_derive::aoc_lib; -pub mod day01; -aoc_lib! { year = 2022 } \ No newline at end of file +use aoc_runner_derive::aoc_lib; +pub mod day01; +pub mod day02; +aoc_lib! { year = 2022 }