aoc-2021-rust

Advent of Code 2021 Solutions in Rust
git clone https://git.sinitax.com/sinitax/aoc-2021-rust
Log | Files | Refs | README | sfeed.txt

part1 (5979B)


      1--- Day 8: Seven Segment Search ---
      2
      3You barely reach the safety of the cave when the whale smashes into the cave mouth, collapsing it.
      4Sensors indicate another exit to this cave at a much greater depth, so you have no choice but to
      5press on.
      6
      7As your submarine slowly makes its way through the cave system, you notice that the four-digit
      8seven-segment displays in your submarine are malfunctioning; they must have been damaged during the
      9escape. You'll be in a lot of trouble without them, so you'd better figure out what's wrong.
     10
     11Each digit of a seven-segment display is rendered by turning on or off any of seven segments named a
     12through g:
     13
     14  0:      1:      2:      3:      4:
     15 aaaa    ....    aaaa    aaaa    ....
     16b    c  .    c  .    c  .    c  b    c
     17b    c  .    c  .    c  .    c  b    c
     18 ....    ....    dddd    dddd    dddd
     19e    f  .    f  e    .  .    f  .    f
     20e    f  .    f  e    .  .    f  .    f
     21 gggg    ....    gggg    gggg    ....
     22
     23  5:      6:      7:      8:      9:
     24 aaaa    aaaa    aaaa    aaaa    aaaa
     25b    .  b    .  .    c  b    c  b    c
     26b    .  b    .  .    c  b    c  b    c
     27 dddd    dddd    ....    dddd    dddd
     28.    f  e    f  .    f  e    f  .    f
     29.    f  e    f  .    f  e    f  .    f
     30 gggg    gggg    ....    gggg    gggg
     31
     32So, to render a 1, only segments c and f would be turned on; the rest would be off. To render a 7,
     33only segments a, c, and f would be turned on.
     34
     35The problem is that the signals which control the segments have been mixed up on each display. The
     36submarine is still trying to display numbers by producing output on signal wires a through g, but
     37those wires are connected to segments randomly. Worse, the wire/segment connections are mixed up
     38separately for each four-digit display! (All of the digits within a display use the same
     39connections, though.)
     40
     41So, you might know that only signal wires b and g are turned on, but that doesn't mean
     42segments b and g are turned on: the only digit that uses two segments is 1, so it must mean segments
     43c and f are meant to be on. With just that information, you still can't tell which wire (b/g) goes
     44to which segment (c/f). For that, you'll need to collect more information.
     45
     46For each display, you watch the changing signals for a while, make a note of all ten unique signal
     47patterns you see, and then write down a single four digit output value (your puzzle input). Using
     48the signal patterns, you should be able to work out which pattern corresponds to which digit.
     49
     50For example, here is what you might see in a single entry in your notes:
     51
     52acedgfb cdfbe gcdfa fbcad dab cefabd cdfgeb eafb cagedb ab |
     53cdfeb fcadb cdfeb cdbaf
     54(The entry is wrapped here to two lines so it fits; in your notes, it will all be on a single line.)
     55
     56Each entry consists of ten unique signal patterns, a | delimiter, and finally the four digit output
     57value. Within an entry, the same wire/segment connections are used (but you don't know what the
     58connections actually are). The unique signal patterns correspond to the ten different ways the
     59submarine tries to render a digit using the current wire/segment connections. Because 7 is the only
     60digit that uses three segments, dab in the above example means that to render a 7, signal lines d,
     61a, and b are on. Because 4 is the only digit that uses four segments, eafb means that to render a 4,
     62signal lines e, a, f, and b are on.
     63
     64Using this information, you should be able to work out which combination of signal wires corresponds
     65to each of the ten digits. Then, you can decode the four digit output value. Unfortunately, in the
     66above example, all of the digits in the output value (cdfeb fcadb cdfeb cdbaf) use five segments and
     67are more difficult to deduce.
     68
     69For now, focus on the easy digits. Consider this larger example:
     70
     71be cfbegad cbdgef fgaecd cgeb fdcge agebfd fecdb fabcd edb |
     72fdgacbe cefdb cefbgd gcbe
     73edbfga begcd cbg gc gcadebf fbgde acbgfd abcde gfcbed gfec |
     74fcgedb cgb dgebacf gc
     75fgaebd cg bdaec gdafb agbcfd gdcbef bgcad gfac gcb cdgabef |
     76cg cg fdcagb cbg
     77fbegcd cbd adcefb dageb afcb bc aefdc ecdab fgdeca fcdbega |
     78efabcd cedba gadfec cb
     79aecbfdg fbg gf bafeg dbefa fcge gcbea fcaegb dgceab fcbdga |
     80gecf egdcabf bgf bfgea
     81fgeab ca afcebg bdacfeg cfaedg gcfdb baec bfadeg bafgc acf |
     82gebdcfa ecba ca fadegcb
     83dbcfg fgd bdegcaf fgec aegbdf ecdfab fbedc dacgb gdcebf gf |
     84cefg dcbef fcge gbcadfe
     85bdfegc cbegaf gecbf dfcage bdacg ed bedf ced adcbefg gebcd |
     86ed bcgafe cdgba cbgef
     87egadfb cdbfeg cegd fecab cgb gbdefca cg fgcdab egfdb bfceg |
     88gbdfcae bgc cg cgb
     89gcafb gcf dcaebfg ecagb gf abcdeg gaef cafbge fdbac fegbdc |
     90fgae cfgab fg bagce
     91
     92Because the digits 1, 4, 7, and 8 each use a unique number of segments, you should be able to tell
     93which combinations of signals correspond to those digits. Counting only digits in the output
     94values (the part after | on each line), in the above example, there are 26 instances of digits that
     95use a unique number of segments (highlighted above).
     96
     97In the output values, how many times do digits 1, 4, 7, or 8 appear?
     98
     99