Day 02 optimizations + Day 03
This commit is contained in:
parent
07251c2305
commit
4aaf8aca52
5 changed files with 52 additions and 10 deletions
|
@ -8,3 +8,4 @@ edition = "2021"
|
|||
[dependencies]
|
||||
aoc-runner = "0.3.0"
|
||||
aoc-runner-derive = "0.3.0"
|
||||
itertools = "0.10.5"
|
||||
|
|
|
@ -16,7 +16,6 @@ pub enum Outcome {
|
|||
Draw,
|
||||
}
|
||||
|
||||
|
||||
impl Outcome {
|
||||
fn other(&self, choice: &Choice) -> Choice {
|
||||
match (self, choice) {
|
||||
|
@ -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
|
||||
|
|
46
src/day03.rs
Normal file
46
src/day03.rs
Normal file
|
@ -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<Vec<char>> {
|
||||
input.lines().map(|line| line.chars().collect()).collect()
|
||||
}
|
||||
|
||||
#[aoc(day3, part1)]
|
||||
pub fn solve_part1(input: &[Vec<char>]) -> 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::<usize>();
|
||||
}
|
||||
total
|
||||
}
|
||||
|
||||
#[aoc(day3, part2)]
|
||||
pub fn solve_part2(input: &[Vec<char>]) -> usize {
|
||||
let mut total: usize = 0;
|
||||
for chunk in input.chunks_exact(3) {
|
||||
let chunk: HashSet<&char> = chunk
|
||||
.iter()
|
||||
.map(|v| v.iter().collect::<HashSet<&char>>())
|
||||
.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::<usize>();
|
||||
}
|
||||
total
|
||||
}
|
|
@ -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!()
|
||||
}
|
||||
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
use aoc_runner_derive::aoc_lib;
|
||||
pub mod day01;
|
||||
pub mod day02;
|
||||
pub mod day03;
|
||||
aoc_lib! { year = 2022 }
|
||||
|
|
Loading…
Reference in a new issue