From 754b296abfd64adb36aad4d942675d527f2d2ce2 Mon Sep 17 00:00:00 2001 From: Joseph Montanaro Date: Thu, 9 Dec 2021 15:38:15 -0800 Subject: [PATCH] add lookup table --- 2021/src/day8.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/2021/src/day8.rs b/2021/src/day8.rs index 039b430..c90d224 100644 --- a/2021/src/day8.rs +++ b/2021/src/day8.rs @@ -58,6 +58,14 @@ impl SegmentSet { fn len(&self) -> u8 { self.len } + + fn union(&self, other: &Self) -> Self { + self.data | other.data + } + + fn intersect(&self, other: &Self) -> Self { + self.data & other.data + } } impl FromStr for SegmentSet { @@ -95,6 +103,21 @@ impl FromStr for Screen { } +const DIGIT_SEGMENTS = { + let segment_strs = [ + "abcefg", "cf", "acdeg", "acdfg", + "bcdf", "abdfg", "abdefg", "acf", + "abcdefg", "abcdfg", + ] + + let sets = [SegmentSet::default(), 10] + for (i, s) in segment_strs.iter().enumerate() { + sets[i] = s.parse::().unwrap() + } + sets +} + + fn part1(screens: &[Screen]) -> usize { screens.iter() .flat_map(|d| d.output.iter())