From ad309f5b338cf4f7d00593abb2d6f73d07809010 Mon Sep 17 00:00:00 2001 From: Joe Ardent Date: Sat, 3 Dec 2022 16:15:56 -0800 Subject: [PATCH] part 2 done --- 2022-aoc/src/d3.rs | 38 +++++++++++++++++++++++++++++--------- 1 file changed, 29 insertions(+), 9 deletions(-) diff --git a/2022-aoc/src/d3.rs b/2022-aoc/src/d3.rs index 478d151..d189729 100644 --- a/2022-aoc/src/d3.rs +++ b/2022-aoc/src/d3.rs @@ -1,11 +1,11 @@ -use std::collections::{HashMap, HashSet}; +use std::collections::HashSet; use aoc_runner_derive::{aoc as aoc_run, aoc_generator}; type Contents = (HashSet, HashSet); fn get_priority(item: &char) -> u32 { - if ('a'..='z').contains(item) { + if item.is_ascii_lowercase() { *item as u32 - 96 } else { *item as u32 - 38 @@ -25,14 +25,34 @@ fn parse_input(input: &str) -> Vec { 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); +#[aoc_generator(day3, part2)] +fn parse_input2(input: &str) -> Vec { + let mut out = Vec::with_capacity(input.len() / 3); + let lines: Vec<&str> = input.lines().collect(); + for group in lines.chunks_exact(3) { + let [x, y, z] = group else {panic!()}; + let x: HashSet = HashSet::from_iter(x.chars()); + let y = HashSet::from_iter(y.chars()); + let z = HashSet::from_iter(z.chars()); + let xy: HashSet = x.intersection(&y).copied().collect(); + let common = xy.intersection(&z).next().unwrap(); + out.push(get_priority(common)); } 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() +}