Day 3, puzzle 1

This commit is contained in:
jazzpi 2022-12-13 14:37:15 +01:00
parent 7dde03f46f
commit adb8cfd91a
3 changed files with 41 additions and 0 deletions

15
src/bin/d3p1.rs Normal file
View File

@ -0,0 +1,15 @@
use aoc22::day3;
use aoc22::util;
pub fn main() {
let rucksacks = day3::parse_rucksacks(&util::parse_input());
let mut sum = 0;
for (first, second) in rucksacks {
let common: Vec<_> = first.intersection(&second).collect();
assert!(common.len() == 1);
sum += day3::priority(**common.first().unwrap());
}
println!("Sum of priorities is {}", sum);
}

25
src/day3.rs Normal file
View File

@ -0,0 +1,25 @@
use std::collections::HashSet;
pub fn parse_rucksacks(input: &String) -> Vec<(HashSet<char>, HashSet<char>)> {
let lines = input.lines();
let mut rucksacks = Vec::new();
for line in lines {
let length = line.len();
assert!(length % 2 == 0);
let (first, second) = line.split_at(length / 2);
rucksacks.push((first.chars().collect(), second.chars().collect()));
}
rucksacks
}
pub fn priority(c: char) -> u32 {
assert!(c.is_ascii_alphabetic());
let ord = c as u32;
if c.is_ascii_lowercase() {
1 + ord - 'a' as u32
} else {
27 + ord - 'A' as u32
}
}

View File

@ -1,3 +1,4 @@
pub mod day1;
pub mod day2;
pub mod day3;
pub mod util;