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