aoc2022/examples/day03_1_rewrite.rs

15 lines
550 B
Rust
Raw Normal View History

2022-12-04 05:07:16 +00:00
fn main() {
println!("{}", std::io::stdin().lines()
.map(|x| x.unwrap())
.map(|line| (
line.chars().take(line.len() / 2).collect::<Vec<char>>(),
line.chars().skip(line.len() / 2).collect::<Vec<char>>()
)).map(|(a, b)| *a.iter().filter(|c| b.contains(c)).next().unwrap())
.map(|c| match c {
'a'..='z' => (c as u32) - ('a' as u32) + 1,
'A'..='Z' => (c as u32) - ('A' as u32) + 27,
_ => unreachable!()
}).reduce(|a, b| a + b)
.unwrap());
}