aoc/year2021/
day08.rs

1//! # Seven Segment Search
2//!
3//! Listing each digit and the number of segments that are lit when that digit is displayed:
4//!
5//! | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
6//! |---|---|---|---|---|---|---|---|---|---|
7//! | 6 | 2 | 5 | 5 | 4 | 5 | 6 | 3 | 7 | 6 |
8//!
9//! shows that 3 digits share 5 segments and another 3 share 6 segments so we don't have enough
10//! information just yet. Listing the total occurrences of each segment summing across all 10 digits:
11//!
12//! | a | b | c | d | e | f | g |
13//! |---|---|---|---|---|---|---|
14//! | 8 | 6 | 8 | 7 | 4 | 9 | 7 |
15//!
16//! shows that 2 segments share 7 occurrences and 2 share 8 occurrences so this is still not quite enough
17//! information. However if we combine these 2 tables by *summing* the segment occurrences for each
18//! digit, for example `1` has segments `c` and `f` for a total of 17, then the table looks like:
19//!
20//! | 0  |  1 |  2 |  3 |  4 |  5 |  6 |  7 |  8 |  9 |
21//! |----|----|----|----|----|----|----|----|----|----|
22//! | 42 | 17 | 34 | 39 | 30 | 37 | 41 | 25 | 49 | 45 |
23//!
24//! Now each digit can be uniquely identified. Our algorithm is as follows:
25//! * Calculate the occurrences of each scrambled segment letter before the `|` symbol. Since the
26//!   cardinality of the set is fixed, we can use an array instead of a `HashMap` for speed.
27//! * Add the occurrences of each scrambled segment for each digit after the `|` symbol, then
28//!   lookup the total and map directly to the unscrambled digit.
29use crate::util::iter::*;
30
31type Input = Vec<[u32; 4]>;
32
33pub fn parse(input: &str) -> Input {
34    input.lines().map(descramble).collect()
35}
36
37pub fn part1(input: &Input) -> usize {
38    input.iter().flatten().filter(|&&d| d == 1 || d == 4 || d == 7 || d == 8).count()
39}
40
41pub fn part2(input: &Input) -> u32 {
42    input.iter().map(|[a, b, c, d]| 1000 * a + 100 * b + 10 * c + d).sum()
43}
44
45fn descramble(line: &str) -> [u32; 4] {
46    let mut frequency = [0_u8; 104];
47    let bytes = line.as_bytes();
48    bytes[0..58].iter().for_each(|&b| frequency[b as usize] += 1);
49    bytes[61..]
50        .split(|&b| b == b' ')
51        .map(|scrambled| to_digit(scrambled.iter().map(|&b| frequency[b as usize]).sum()))
52        .chunk::<4>()
53        .next()
54        .unwrap()
55}
56
57fn to_digit(total: u8) -> u32 {
58    match total {
59        42 => 0,
60        17 => 1,
61        34 => 2,
62        39 => 3,
63        30 => 4,
64        37 => 5,
65        41 => 6,
66        25 => 7,
67        49 => 8,
68        45 => 9,
69        _ => unreachable!(),
70    }
71}