diff options
Diffstat (limited to 'aoc22/day3/src')
-rw-r--r-- | aoc22/day3/src/main.rs | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/aoc22/day3/src/main.rs b/aoc22/day3/src/main.rs new file mode 100644 index 0000000..81a3fe7 --- /dev/null +++ b/aoc22/day3/src/main.rs @@ -0,0 +1,59 @@ +#![feature(iter_next_chunk)] + +use std::collections::HashSet; + +fn main() { + part1(); + part2(); +} + +fn part1() { + let input = include_str!("../input"); + + let ans: u32 = input.lines().map(|line| { + let first = &line[..line.len()/2]; + let second = &line[line.len()/2..]; + let first: HashSet<char> = first.chars().collect(); + let second: HashSet<char> = second.chars().collect(); + let common = *first.intersection(&second).next().unwrap(); + let priority = match common { + 'a'..='z' => 1 + common as u32 - 'a' as u32, + 'A'..='Z' => 27 + common as u32 - 'A' as u32, + _ => unreachable!(), + }; + + priority + }).sum(); + + println!("{}", ans); +} + +fn part2() { + let input = include_str!("../input"); + + let mut lines = input.lines(); + + let mut sum = 0; + while let Ok([first, second, third]) = lines.next_chunk::<3>() { + let first: HashSet<char> = first.chars().collect(); + let second: HashSet<char> = second.chars().collect(); + let third: HashSet<char> = third.chars().collect(); + let common = *first + .intersection(&second) + .copied() + .collect::<HashSet<char>>() + .intersection(&third) + .next() + .unwrap(); + + let priority = match common { + 'a'..='z' => 1 + common as u32 - 'a' as u32, + 'A'..='Z' => 27 + common as u32 - 'A' as u32, + _ => unreachable!(), + }; + + sum += priority; + } + + println!("{}", sum); +} |