summaryrefslogtreecommitdiff
path: root/2021/rust/day08
diff options
context:
space:
mode:
authorcos <cos>2021-12-08 21:51:54 +0100
committercos <cos>2021-12-08 21:51:56 +0100
commitf0b8e240a7e1dcc79c3f2109c6365dbbf7c67b46 (patch)
tree78d0d593659f09249d6f534444d60b03c8a47fdb /2021/rust/day08
parentee2f7e90c29adfa222545bdbed275b94ceddcbcf (diff)
downloadadventofcode-f0b8e240a7e1dcc79c3f2109c6365dbbf7c67b46.zip
Simplify set operations of 2021day08
Performing operations on sets using operators, rather than the less ergonomic replaced methods, results readability improvements.
Diffstat (limited to '2021/rust/day08')
-rw-r--r--2021/rust/day08/src/main.rs21
1 files changed, 7 insertions, 14 deletions
diff --git a/2021/rust/day08/src/main.rs b/2021/rust/day08/src/main.rs
index f4e44bd..0eeb904 100644
--- a/2021/rust/day08/src/main.rs
+++ b/2021/rust/day08/src/main.rs
@@ -144,9 +144,7 @@ fn find_digits(input: &[Digit]) -> Result<Vec<Option<Digit>>> {
}
for unique_segments in [1, 4, 7, 8].into_iter() {
// Digit 1 is found by unique number of segments, as in part 1.
- // Digit 4 is found by unique number of segments, as in part 1.
- // Digit 7 is found by unique number of segments, as in part 1.
- // Digit 8 is found by unique number of segments, as in part 1.
+ // The same is true for Digit 4, Digit 7 and Digit 8.
unknown.clone().into_iter().map(|d| if d.inner.len() == segment_count[unique_segments] {
unknown.remove(&d);
digits[unique_segments] = Some(d);
@@ -168,15 +166,12 @@ fn find_digits(input: &[Digit]) -> Result<Vec<Option<Digit>>> {
digits[0] = unknown.clone().into_iter().filter(|d| {
if let (Some(one), Some(three), Some(four), Some(eight)) =
(&digits[1], &digits[3], &digits[4], &digits[8])
- {
- let set_a = four.inner.difference(&(one.inner)).cloned().collect();
- let set_b = three.inner.intersection(&set_a).cloned().collect();
- let set_c = eight.inner.difference(&set_b).cloned().collect();
- if d.inner == set_c {
- unknown.remove(d);
- return true
- }
+ {
+ if d.inner == &eight.inner - &(&three.inner & &(&four.inner - &one.inner)) {
+ unknown.remove(d);
+ return true
}
+ }
false
}).last();
digits[9] = unknown.clone().into_iter().filter(|d| {
@@ -200,9 +195,7 @@ fn find_digits(input: &[Digit]) -> Result<Vec<Option<Digit>>> {
// Digit 2 is the one with zero overlap with the difference of Digit 4 - Digit 3.
digits[2] = unknown.clone().into_iter().filter(|d| {
if let (Some(three), Some(four)) = (&digits[3], &digits[4]) {
- let set_a = four.inner.difference(&(three.inner)).cloned().collect();
- let set_b: HashSet<Segment> = d.inner.intersection(&set_a).cloned().collect();
- if set_b.is_empty() {
+ if (&d.inner & &(&four.inner - &three.inner)).is_empty() {
unknown.remove(d);
return true
}