aoc22/src/day3/main.rs

58 lines
1.4 KiB
Rust

use base::*;
fn calc_prio(item: char) -> u32{
if item.is_lowercase() {
item as u32 - ('a' as u32) + 1
} else {
item as u32 - ('A' as u32) + 27
}
}
fn main() {
let lines = read_file("day3.txt");
let sum = lines.iter()
.map(|line|line.split_at(line.len() / 2))
.map(|(first_half, second_half)| {
for char_0 in first_half.chars() {
for char_1 in second_half.chars() {
if char_0 == char_1 {
return Some(char_0);
}
}
}
None
})
.map(|item| {
match item{
None => 0,
Some(item) => calc_prio(item)
}
}).sum::<u32>();
println!("Task 1: {sum}");
let sum = lines.chunks_exact(3).map(|chunk| {
for char_0 in chunk[0].chars() {
for char_1 in chunk[1].chars() {
if char_0 == char_1 {
for char_2 in chunk[2].chars() {
if char_1 == char_2 {
return Some(char_0);
}
}
}
}
}
None
}).map(|item|{
match item {
None => 0,
Some(item) => calc_prio(item)
}
}).sum::<u32>();
println!("Task 2: {sum}");
}