2022 day3/rust: use bitwise arithmetic

This commit is contained in:
Xiretza 2022-12-03 06:46:00 +01:00
parent c4268a0093
commit d13125c4ad

View file

@ -1,6 +1,6 @@
use std::{ use std::{
collections::HashSet,
io::{stdin, Read}, io::{stdin, Read},
ops::{BitAnd, BitOr},
}; };
fn item_priority(item: char) -> u32 { fn item_priority(item: char) -> u32 {
@ -11,9 +11,48 @@ fn item_priority(item: char) -> u32 {
} }
} }
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
struct ItemSet(u64);
impl ItemSet {
pub fn priority_of_single_item(self) -> Option<u32> {
if self.0.is_power_of_two() {
Some(self.0.trailing_zeros())
} else {
None
}
}
}
impl FromIterator<char> for ItemSet {
fn from_iter<T: IntoIterator<Item = char>>(iter: T) -> Self {
Self(
iter.into_iter()
.map(item_priority)
.fold(0, |acc, i| acc | (1 << i)),
)
}
}
impl BitOr for ItemSet {
type Output = Self;
fn bitor(self, rhs: Self) -> Self::Output {
Self(BitOr::bitor(self.0, rhs.0))
}
}
impl BitAnd for ItemSet {
type Output = Self;
fn bitand(self, rhs: Self) -> Self::Output {
Self(BitAnd::bitand(self.0, rhs.0))
}
}
struct Rucksack { struct Rucksack {
left: HashSet<char>, left: ItemSet,
right: HashSet<char>, right: ItemSet,
} }
fn main() { fn main() {
@ -26,8 +65,8 @@ fn main() {
let (left, right) = line.split_at(line.len() / 2); let (left, right) = line.split_at(line.len() / 2);
assert_eq!(left.len(), right.len()); assert_eq!(left.len(), right.len());
let left: HashSet<_> = left.chars().collect(); let left = left.chars().collect();
let right: HashSet<_> = right.chars().collect(); let right = right.chars().collect();
Rucksack { left, right } Rucksack { left, right }
}) })
@ -35,21 +74,20 @@ fn main() {
let sum1: u32 = sacks let sum1: u32 = sacks
.iter() .iter()
.map(|sack| item_priority(*sack.left.intersection(&sack.right).next().unwrap())) .map(|sack| (sack.left & sack.right).priority_of_single_item().unwrap())
.sum(); .sum();
let sum2: u32 = sacks let sum2: u32 = sacks
.chunks(3) .chunks(3)
.into_iter() .into_iter()
.map(|sacks| { .map(|sacks| {
let common = sacks sacks
.iter() .iter()
.map(|sack| &sack.left | &sack.right) .map(|sack| sack.left | sack.right)
.reduce(|a, b| &a & &b) .reduce(BitAnd::bitand)
.unwrap(); .unwrap()
assert_eq!(common.len(), 1); .priority_of_single_item()
.unwrap()
item_priority(common.into_iter().next().unwrap())
}) })
.sum(); .sum();