|
|
@ -1,10 +1,10 @@ |
|
|
|
use std::{io::Result, fs::read_to_string, collections::HashSet}; |
|
|
|
use std::{collections::HashSet, fs::read_to_string, io::Result}; |
|
|
|
|
|
|
|
|
|
|
|
pub fn priority(x: u8) -> u32 { |
|
|
|
pub fn priority(x: u8) -> u32 { |
|
|
|
(match x { |
|
|
|
(match x { |
|
|
|
0x61..=0x7a => x - 0x61 + 1, |
|
|
|
0x61..=0x7a => x - 0x61 + 1, |
|
|
|
0x41..=0x5a => x - 0x41 + 27, |
|
|
|
0x41..=0x5a => x - 0x41 + 27, |
|
|
|
_ => unreachable!() |
|
|
|
_ => unreachable!(), |
|
|
|
}) as u32 |
|
|
|
}) as u32 |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -16,19 +16,30 @@ fn main() -> Result<()> { |
|
|
|
let mut elves: Vec<HashSet<_>> = Vec::new(); |
|
|
|
let mut elves: Vec<HashSet<_>> = Vec::new(); |
|
|
|
|
|
|
|
|
|
|
|
for line in read_to_string("input.txt")?.split("\n") { |
|
|
|
for line in read_to_string("input.txt")?.split("\n") { |
|
|
|
if line.is_empty() { continue; } |
|
|
|
if line.is_empty() { |
|
|
|
|
|
|
|
continue; |
|
|
|
|
|
|
|
} |
|
|
|
let bytes = line.as_bytes(); |
|
|
|
let bytes = line.as_bytes(); |
|
|
|
|
|
|
|
|
|
|
|
let (first_half, second_half) = bytes.split_at(bytes.len() / 2); |
|
|
|
let (first_half, second_half) = bytes.split_at(bytes.len() / 2); |
|
|
|
let in_first_half = first_half.iter().copied().collect::<HashSet<_>>(); |
|
|
|
let in_first_half = first_half.iter().copied().collect::<HashSet<_>>(); |
|
|
|
let in_second_half = second_half.iter().copied().collect::<HashSet<_>>(); |
|
|
|
let in_second_half = second_half.iter().copied().collect::<HashSet<_>>(); |
|
|
|
let in_both_halves = in_first_half.intersection(&in_second_half).copied().next().unwrap(); |
|
|
|
let in_both_halves = in_first_half |
|
|
|
|
|
|
|
.intersection(&in_second_half) |
|
|
|
|
|
|
|
.copied() |
|
|
|
|
|
|
|
.next() |
|
|
|
|
|
|
|
.unwrap(); |
|
|
|
p1_priority += priority(in_both_halves); |
|
|
|
p1_priority += priority(in_both_halves); |
|
|
|
|
|
|
|
|
|
|
|
let items = bytes.iter().copied().collect::<HashSet<_>>(); |
|
|
|
let items = bytes.iter().copied().collect::<HashSet<_>>(); |
|
|
|
elves.push(items.clone()); |
|
|
|
elves.push(items.clone()); |
|
|
|
if i == 2 { |
|
|
|
if i == 2 { |
|
|
|
let badge = items.iter().filter(|item| elves.iter().filter(|elf| elf.contains(item)).count() == 3).copied().next().unwrap(); |
|
|
|
let badge = items |
|
|
|
|
|
|
|
.iter() |
|
|
|
|
|
|
|
.filter(|item| elves.iter().filter(|elf| elf.contains(item)).count() == 3) |
|
|
|
|
|
|
|
.copied() |
|
|
|
|
|
|
|
.next() |
|
|
|
|
|
|
|
.unwrap(); |
|
|
|
p2_priority += priority(badge); |
|
|
|
p2_priority += priority(badge); |
|
|
|
elves.clear(); |
|
|
|
elves.clear(); |
|
|
|
i = 0; |
|
|
|
i = 0; |
|
|
|