From cb72de615df85251f98d85890929a89b52bfcb5b Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Sat, 3 Dec 2022 17:53:16 +0100 Subject: [PATCH] Day 03 --- 2022/.gitignore | 1 + 2022/src/bin/03.rs | 76 ++++++++++++++++++++++++++++++++++++++++ 2022/src/examples/03.txt | 6 ++++ 2022/src/helpers.rs | 2 ++ 4 files changed, 85 insertions(+) create mode 100644 2022/src/bin/03.rs create mode 100644 2022/src/examples/03.txt diff --git a/2022/.gitignore b/2022/.gitignore index f2fd7aa..1e0355a 100644 --- a/2022/.gitignore +++ b/2022/.gitignore @@ -18,3 +18,4 @@ target/ # @see https://old.reddit.com/r/adventofcode/comments/k99rod/sharing_input_data_were_we_requested_not_to/gf2ukkf/?context=3 /src/inputs !/src/inputs/.keep +puzzle.md diff --git a/2022/src/bin/03.rs b/2022/src/bin/03.rs new file mode 100644 index 0000000..da6840b --- /dev/null +++ b/2022/src/bin/03.rs @@ -0,0 +1,76 @@ +use advent_of_code::helpers::parse_input; +use std::collections::HashSet; + +static PRIORITY_CHARS: &str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; + +fn get_prio(c: char) -> u32 { + let i = PRIORITY_CHARS.chars().position(|cc| cc == c).unwrap(); + i as u32 + 1 +} + +fn get_comps(row: &str) -> (HashSet, HashSet) { + let (top, bot) = row.split_at(row.len() / 2); + ( + HashSet::from_iter(top.chars()), + HashSet::from_iter(bot.chars()), + ) +} + +pub fn part_one(input: &str) -> Option { + let comps = parse_input(input, get_comps); + let intersections = comps.iter().map(|(t, b)| t.intersection(b)); + let priorities = intersections.map(|inters| inters.map(|c| get_prio(*c))); + let sums: Vec = priorities.map(|inters| inters.sum()).collect(); + + let sum: u32 = sums.iter().sum(); + + Some(sum) +} + +fn find_badge(group: &[&str]) -> char { + let group_sets: Vec> = group + .iter() + .map(|s| HashSet::from_iter(s.chars())) + .collect(); + + let mut badges: HashSet = group_sets[0] + .intersection(&group_sets[1]) // cringe + .copied() + .collect(); + badges = badges.intersection(&group_sets[2]).copied().collect(); + + *badges.iter().next().unwrap() +} + +pub fn part_two(input: &str) -> Option { + let lines: Vec<&str> = input.lines().collect(); + let groups = lines.chunks(3); + + let prios: Vec = groups.map(|group| get_prio(find_badge(group))).collect(); + + let sum: u32 = prios.iter().sum(); + Some(sum) +} + +fn main() { + let input = &advent_of_code::read_file("inputs", 3); + advent_of_code::solve!(1, part_one, input); + advent_of_code::solve!(2, part_two, input); +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_part_one() { + let input = advent_of_code::read_file("examples", 3); + assert_eq!(part_one(&input), Some(157)); + } + + #[test] + fn test_part_two() { + let input = advent_of_code::read_file("examples", 3); + assert_eq!(part_two(&input), Some(70)); + } +} diff --git a/2022/src/examples/03.txt b/2022/src/examples/03.txt new file mode 100644 index 0000000..9919ffa --- /dev/null +++ b/2022/src/examples/03.txt @@ -0,0 +1,6 @@ +vJrwpWtwJgWrhcsFMMfFFhFp +jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL +PmmdzqPrVvPwwTWBwg +wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn +ttgJtRGJQctTZtZT +CrZsJsPPZsGzwwsLwLmpwMDw \ No newline at end of file diff --git a/2022/src/helpers.rs b/2022/src/helpers.rs index a8af6a5..77e4dc4 100644 --- a/2022/src/helpers.rs +++ b/2022/src/helpers.rs @@ -3,6 +3,8 @@ * Example import from this file: `use advent_of_code::helpers::example_fn;`. */ +use std::collections::HashSet; + pub fn parse_input(input: &str, cons: fn(&str) -> T) -> Vec where Vec: FromIterator,