solve year 2022 day 03 part 1
This commit is contained in:
39
src/year2022/day03.rs
Normal file
39
src/year2022/day03.rs
Normal file
@ -0,0 +1,39 @@
|
||||
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())
|
||||
}
|
Reference in New Issue
Block a user