part 2 done

This commit is contained in:
Joe Ardent 2022-12-03 16:15:56 -08:00
parent 4ae6b3cb7f
commit ad309f5b33

View file

@ -1,11 +1,11 @@
use std::collections::{HashMap, HashSet}; use std::collections::HashSet;
use aoc_runner_derive::{aoc as aoc_run, aoc_generator}; use aoc_runner_derive::{aoc as aoc_run, aoc_generator};
type Contents = (HashSet<char>, HashSet<char>); type Contents = (HashSet<char>, HashSet<char>);
fn get_priority(item: &char) -> u32 { fn get_priority(item: &char) -> u32 {
if ('a'..='z').contains(item) { if item.is_ascii_lowercase() {
*item as u32 - 96 *item as u32 - 96
} else { } else {
*item as u32 - 38 *item as u32 - 38
@ -25,14 +25,34 @@ fn parse_input(input: &str) -> Vec<Contents> {
out out
} }
#[aoc_run(day3, part1)] #[aoc_generator(day3, part2)]
fn part1(sacks: &[Contents]) -> u32 { fn parse_input2(input: &str) -> Vec<u32> {
let mut out = 0; let mut out = Vec::with_capacity(input.len() / 3);
for sack in sacks { let lines: Vec<&str> = input.lines().collect();
let (lo, hi) = sack; for group in lines.chunks_exact(3) {
let common = lo.intersection(hi).into_iter().next().unwrap(); let [x, y, z] = group else {panic!()};
out += get_priority(common); let x: HashSet<char> = HashSet::from_iter(x.chars());
let y = HashSet::from_iter(y.chars());
let z = HashSet::from_iter(z.chars());
let xy: HashSet<char> = x.intersection(&y).copied().collect();
let common = xy.intersection(&z).next().unwrap();
out.push(get_priority(common));
} }
out out
} }
#[aoc_run(day3, part1)]
fn part1(sacks: &[Contents]) -> u32 {
let mut out = 0;
for (lo, hi) in sacks {
let common = lo.intersection(hi).next().unwrap();
out += get_priority(common);
}
out
}
#[aoc_run(day3, part2)]
fn p2(items: &[u32]) -> u32 {
items.iter().sum()
}