Day 2 and 3 improvements
This commit is contained in:
parent
129c9f85a0
commit
6ba1e39d98
@ -10,12 +10,9 @@ enum Symbol {
|
||||
impl From<char> for Symbol {
|
||||
fn from(value: char) -> Self {
|
||||
match value {
|
||||
'A' => Self::Rock,
|
||||
'B' => Self::Paper,
|
||||
'C' => Self::Scissors,
|
||||
'X' => Self::Rock,
|
||||
'Y' => Self::Paper,
|
||||
'Z' => Self::Scissors,
|
||||
'A' | 'X' => Self::Rock,
|
||||
'B' | 'Y' => Self::Paper,
|
||||
'C' | 'Z' => Self::Scissors,
|
||||
_ => panic!("invalid input"),
|
||||
}
|
||||
}
|
||||
|
@ -3,12 +3,12 @@ use std::{collections::HashSet, fs::read_to_string};
|
||||
use color_eyre::{eyre::*, Result};
|
||||
use itertools::Itertools;
|
||||
|
||||
fn priority(item: &char) -> usize {
|
||||
fn priority(item: char) -> Result<usize> {
|
||||
('a'..='z')
|
||||
.position(|c| *item == c)
|
||||
.position(|c| item == c)
|
||||
.map(|i| i + 1)
|
||||
.or_else(|| ('A'..='Z').position(|c| *item == c).map(|i| i + 27))
|
||||
.unwrap_or(0)
|
||||
.or_else(|| ('A'..='Z').position(|c| item == c).map(|i| i + 27))
|
||||
.ok_or_else(|| eyre!("invalid character '{}'", item))
|
||||
}
|
||||
|
||||
fn initial(input: &str) -> (usize, usize) {
|
||||
@ -20,7 +20,7 @@ fn initial(input: &str) -> (usize, usize) {
|
||||
let compartment2 = compartment2.chars().collect::<HashSet<_>>();
|
||||
compartment1
|
||||
.intersection(&compartment2)
|
||||
.map(priority)
|
||||
.filter_map(|v| priority(*v).ok())
|
||||
.sum::<usize>()
|
||||
})
|
||||
.sum();
|
||||
@ -32,7 +32,7 @@ fn initial(input: &str) -> (usize, usize) {
|
||||
group
|
||||
.map(|line| line.chars().collect::<HashSet<_>>())
|
||||
.coalesce(|a, b| Ok(a.intersection(&b).copied().collect()))
|
||||
.map(|set| set.iter().map(priority).sum::<usize>())
|
||||
.map(|set| set.iter().filter_map(|v| priority(*v).ok()).sum::<usize>())
|
||||
.sum::<usize>()
|
||||
})
|
||||
.sum();
|
||||
|
Loading…
Reference in New Issue
Block a user