Day 01 optimization + Day02

This commit is contained in:
Daniel S. 2022-12-02 10:50:12 +01:00
parent f9b3e9214b
commit 07251c2305
4 changed files with 164 additions and 25 deletions

View File

@ -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<usize> {
input.split("\n\n").map(|chunk| chunk.lines().map(|line| line.trim().parse::<usize>().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<usize> = 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<usize> {
input
.split("\n\n")
.map(|chunk| {
chunk
.lines()
.map(|line| line.trim().parse::<usize>().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<usize> = input.to_vec();
scores.sort_by_key(|v| Reverse(*v));
scores.iter().take(3).sum()
}

131
src/day02.rs Normal file
View File

@ -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<Outcome> 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<Self, Self::Err> {
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::<Vec<Choice>>();
(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()
}

View File

@ -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!()
// }

View File

@ -1,4 +1,4 @@
use aoc_runner_derive::aoc_lib;
pub mod day01;
aoc_lib! { year = 2022 }
use aoc_runner_derive::aoc_lib;
pub mod day01;
pub mod day02;
aoc_lib! { year = 2022 }