dumb_shit/2022-aoc/src/d3.rs

39 lines
929 B
Rust
Raw Normal View History

2022-12-03 23:06:01 +00:00
use std::collections::{HashMap, HashSet};
use aoc_runner_derive::{aoc as aoc_run, aoc_generator};
type Contents = (HashSet<char>, HashSet<char>);
fn get_priority(item: &char) -> u32 {
if ('a'..='z').contains(item) {
*item as u32 - 96
} else {
*item as u32 - 38
}
}
#[aoc_generator(day3, part1)]
fn parse_input(input: &str) -> Vec<Contents> {
let mut out = Vec::with_capacity(input.len());
for line in input.lines() {
let half_len = line.len() / 2;
let lo = HashSet::from_iter(line[0..half_len].chars());
let hi = HashSet::from_iter(line[half_len..].chars());
out.push((lo, hi));
}
out
}
#[aoc_run(day3, part1)]
fn part1(sacks: &[Contents]) -> u32 {
let mut out = 0;
for sack in sacks {
let (lo, hi) = sack;
let common = lo.intersection(hi).into_iter().next().unwrap();
out += get_priority(common);
}
out
}