1
0
This commit is contained in:
2022-12-23 19:12:52 +00:00
parent cede3289dc
commit 04490516f0
4 changed files with 387 additions and 0 deletions

73
day3/src/main.rs Normal file
View File

@ -0,0 +1,73 @@
use std::collections::HashSet;
fn char_to_prio(c: char) -> u64 {
if c.is_ascii_uppercase() {
let ci = c as u8;
return u64::from(ci - b'A' + 27);
}
else if c.is_ascii_lowercase() {
let ci = c as u8;
return u64::from(ci - b'a' + 1);
}
0
}
fn main() {
const INPUT: &str = include_str!("../input.txt");
let input = INPUT.lines();
let mut priority_sum: u64 = 0;
for line in input {
let compartments = line.clone().split_at(line.len() / 2);
let items: HashSet<char> = HashSet::from_iter(compartments.0.chars());
let mut new_items: HashSet<char> = HashSet::new();
for c in compartments.1.chars() {
if new_items.insert(c) && items.contains(&c) {
priority_sum += char_to_prio(c);
}
}
}
println!("Priority sum: {}", priority_sum);
let mut input = INPUT.lines().peekable();
priority_sum = 0;
while input.peek().is_some() {
let lines = [input.next().unwrap().clone(), input.next().unwrap().clone(), input.next().unwrap().clone()];
for c in b'A'..=b'Z' {
let c = c as char;
let mut count: u8 = 0;
for line in lines {
if line.chars().any(|x| x == c) {
count += 1;
}
}
if count == 3 {
priority_sum += char_to_prio(c);
break;
}
count = 0;
let c = c.to_lowercase().next().unwrap();
for line in lines {
if line.chars().any(|x| x == c) {
count += 1;
}
}
if count == 3 {
priority_sum += char_to_prio(c);
break;
}
}
}
println!("Priority sum: {}", priority_sum);
}