This commit is contained in:
2022-12-03 14:34:33 -05:00
parent e902e6f82d
commit 493b28e3d9
4 changed files with 340 additions and 0 deletions
+33
View File
@@ -0,0 +1,33 @@
use std::collections::HashSet;
fn get_priority(c: char) -> i32 {
match c {
'a'..='z' => c as i32 - 'a' as i32 + 1,
'A'..='Z' => c as i32 - 'A' as i32 + 27,
_ => panic!("expected letter"),
}
}
advent_of_code_macro::solve_problem!(
day 3,
Input Vec<String>,
parse |input: &str| {
input.lines().map(str::to_owned).collect()
},
part one |data: Input| {
data.iter().map(|s| {
let (left, right) = s.split_at(s.len() / 2);
let right_set: HashSet<char> = right.chars().collect();
get_priority(left.chars().find(|c| right_set.contains(c)).unwrap())
}).sum()
},
part two |data: Input| {
data.as_slice().chunks(3).map(|c| {
let second_set: HashSet<char> = c[1].chars().collect();
let third_set: HashSet<char> = c[2].chars().collect();
get_priority(c[0].chars().find(|c| second_set.contains(c) && third_set.contains(c)).unwrap())
}).sum()
},
sample tests [157, 70],
star tests [7908, 2838]
);
+1
View File
@@ -1,3 +1,4 @@
#![allow(dead_code)]
mod day_1;
mod day_2;
mod day_3;