40 lines
889 B
Rust
40 lines
889 B
Rust
use std::collections::BTreeSet;
|
|
|
|
fn char_to_score(c: char) -> u32 {
|
|
match c {
|
|
'a'..='z' => c as u32 - 96,
|
|
'A'..='Z' => c as u32 - 38,
|
|
_ => unreachable!(),
|
|
}
|
|
}
|
|
|
|
pub fn part1(input: &str) -> impl std::fmt::Display {
|
|
input
|
|
.lines()
|
|
.map(|l| l.split_at(l.len() / 2))
|
|
.map(|(l, r)| {
|
|
let a: BTreeSet<char> = l.chars().collect();
|
|
let b: BTreeSet<char> = r.chars().collect();
|
|
a.intersection(&b)
|
|
.next()
|
|
.expect("We know there is at least one")
|
|
.clone()
|
|
})
|
|
.map(char_to_score)
|
|
.sum::<u32>()
|
|
}
|
|
|
|
pub fn part2(input: &str) -> impl std::fmt::Display {
|
|
""
|
|
}
|
|
|
|
#[test]
|
|
fn test_part1() {
|
|
assert_eq!("15", part1("A Y\nB X\nC Z").to_string())
|
|
}
|
|
|
|
#[test]
|
|
fn test_part2() {
|
|
assert_eq!("12", part2("A Y\nB X\nC Z").to_string())
|
|
}
|