master
E. Almqvist 2 years ago
parent 5637787a36
commit cb72de615d
  1. 1
      2022/.gitignore
  2. 76
      2022/src/bin/03.rs
  3. 6
      2022/src/examples/03.txt
  4. 2
      2022/src/helpers.rs

1
2022/.gitignore vendored

@ -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 # @see https://old.reddit.com/r/adventofcode/comments/k99rod/sharing_input_data_were_we_requested_not_to/gf2ukkf/?context=3
/src/inputs /src/inputs
!/src/inputs/.keep !/src/inputs/.keep
puzzle.md

@ -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<char>, HashSet<char>) {
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<u32> {
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<u32> = priorities.map(|inters| inters.sum()).collect();
let sum: u32 = sums.iter().sum();
Some(sum)
}
fn find_badge(group: &[&str]) -> char {
let group_sets: Vec<HashSet<char>> = group
.iter()
.map(|s| HashSet::from_iter(s.chars()))
.collect();
let mut badges: HashSet<char> = 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<u32> {
let lines: Vec<&str> = input.lines().collect();
let groups = lines.chunks(3);
let prios: Vec<u32> = 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));
}
}

@ -0,0 +1,6 @@
vJrwpWtwJgWrhcsFMMfFFhFp
jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL
PmmdzqPrVvPwwTWBwg
wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn
ttgJtRGJQctTZtZT
CrZsJsPPZsGzwwsLwLmpwMDw

@ -3,6 +3,8 @@
* Example import from this file: `use advent_of_code::helpers::example_fn;`. * Example import from this file: `use advent_of_code::helpers::example_fn;`.
*/ */
use std::collections::HashSet;
pub fn parse_input<T>(input: &str, cons: fn(&str) -> T) -> Vec<T> pub fn parse_input<T>(input: &str, cons: fn(&str) -> T) -> Vec<T>
where where
Vec<T>: FromIterator<T>, Vec<T>: FromIterator<T>,

Loading…
Cancel
Save