From 4ae6b3cb7f2dff42e3b9b1c0dc307919f0481ec4 Mon Sep 17 00:00:00 2001 From: Joe Ardent Date: Sat, 3 Dec 2022 15:06:01 -0800 Subject: [PATCH] day 3, part 1 done --- 2022-aoc/src/d3.rs | 38 ++++++++++++++++++++++++++++++++++++++ 2022-aoc/src/lib.rs | 1 + 2 files changed, 39 insertions(+) create mode 100644 2022-aoc/src/d3.rs diff --git a/2022-aoc/src/d3.rs b/2022-aoc/src/d3.rs new file mode 100644 index 0000000..478d151 --- /dev/null +++ b/2022-aoc/src/d3.rs @@ -0,0 +1,38 @@ +use std::collections::{HashMap, 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) { + *item as u32 - 96 + } else { + *item as u32 - 38 + } +} + +#[aoc_generator(day3, part1)] +fn parse_input(input: &str) -> Vec { + 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 +} diff --git a/2022-aoc/src/lib.rs b/2022-aoc/src/lib.rs index 08ff265..31a0c97 100644 --- a/2022-aoc/src/lib.rs +++ b/2022-aoc/src/lib.rs @@ -2,5 +2,6 @@ use aoc_runner_derive::aoc_lib; mod d1; mod d2; +mod d3; aoc_lib! { year = 2022 }