From 4aaf8aca525e2a0335171a9fdc5151b020f2e044 Mon Sep 17 00:00:00 2001 From: Daniel Seiller Date: Sat, 3 Dec 2022 19:22:43 +0100 Subject: [PATCH] Day 02 optimizations + Day 03 --- Cargo.toml | 3 ++- src/day02.rs | 10 ++-------- src/day03.rs | 46 ++++++++++++++++++++++++++++++++++++++++++++++ src/dayXX.rs | 2 +- src/lib.rs | 1 + 5 files changed, 52 insertions(+), 10 deletions(-) create mode 100644 src/day03.rs diff --git a/Cargo.toml b/Cargo.toml index 60c494b..6aa112d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,4 +7,5 @@ edition = "2021" [dependencies] aoc-runner = "0.3.0" -aoc-runner-derive = "0.3.0" \ No newline at end of file +aoc-runner-derive = "0.3.0" +itertools = "0.10.5" diff --git a/src/day02.rs b/src/day02.rs index 8b3be0a..59c0bae 100644 --- a/src/day02.rs +++ b/src/day02.rs @@ -16,10 +16,9 @@ pub enum Outcome { Draw, } - impl Outcome { fn other(&self, choice: &Choice) -> Choice { - match (self,choice) { + match (self, choice) { (Outcome::Win, Choice::Rock) => Choice::Paper, (Outcome::Win, Choice::Paper) => Choice::Scissors, (Outcome::Win, Choice::Scissors) => Choice::Rock, @@ -64,12 +63,10 @@ impl Choice { (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, + _ => Outcome::Draw, } } } @@ -113,9 +110,6 @@ pub fn solve_part1(input: &[(Choice, Choice)]) -> usize { .sum() } - - - #[aoc(day2, part2)] pub fn solve_part2(input: &[(Choice, Choice)]) -> usize { input diff --git a/src/day03.rs b/src/day03.rs new file mode 100644 index 0000000..1161d42 --- /dev/null +++ b/src/day03.rs @@ -0,0 +1,46 @@ +use aoc_runner_derive::{aoc, aoc_generator}; +use std::collections::HashSet; +#[aoc_generator(day3)] +pub fn input_generator(input: &str) -> Vec> { + input.lines().map(|line| line.chars().collect()).collect() +} + +#[aoc(day3, part1)] +pub fn solve_part1(input: &[Vec]) -> usize { + let mut total: usize = 0; + for line in input { + let (a, b) = line.split_at(line.len() / 2); + let a: HashSet<&char> = a.iter().collect(); + let b: HashSet<&char> = b.iter().collect(); + total += a + .intersection(&b) + .map(|&&v| match v { + 'a'..='z' => 1 + ((v as u8) - b'a') as usize, + 'A'..='Z' => 27 + ((v as u8) - b'A') as usize, + _ => unreachable!(), + }) + .sum::(); + } + total +} + +#[aoc(day3, part2)] +pub fn solve_part2(input: &[Vec]) -> usize { + let mut total: usize = 0; + for chunk in input.chunks_exact(3) { + let chunk: HashSet<&char> = chunk + .iter() + .map(|v| v.iter().collect::>()) + .reduce(|a, b| a.intersection(&b).copied().collect()) + .unwrap_or_default(); + total += chunk + .iter() + .map(|&&v| match v { + 'a'..='z' => 1 + ((v as u8) - b'a') as usize, + 'A'..='Z' => 27 + ((v as u8) - b'A') as usize, + _ => unreachable!(), + }) + .sum::(); + } + total +} diff --git a/src/dayXX.rs b/src/dayXX.rs index 83475ea..31eccf1 100644 --- a/src/dayXX.rs +++ b/src/dayXX.rs @@ -1,6 +1,6 @@ use aoc_runner_derive::{aoc, aoc_generator}; #[aoc_generator(dayX)] -pub fn input_generator(input: &str) -> _ { +pub fn input_generator(input: &str) -> usize { todo!() } diff --git a/src/lib.rs b/src/lib.rs index 290d5dd..6d55771 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,5 @@ use aoc_runner_derive::aoc_lib; pub mod day01; pub mod day02; +pub mod day03; aoc_lib! { year = 2022 }