Part 1

This part is just string counting. The output section of each line consists of four words separated by spaces, and we are asked to find the total number of words corresponding to a one-digit (two segments), a four-digit (four segments), a seven-digit (three segments), or an eight-digit (seven segments). We don’t even need to keep separate counts, since the problem only calls for the total number of these digit-coding strings.

Since we can do it concisely, here’s a specification of the solution. Let outputs : [[String]] be the list of all the output sections (each element corresponding to the output section of a line of the puzzle input). Then:

total = length (concat (list (filter p) outputs))

p s = (length s) ∈ {2, 3, 4, 7}

Fusing total into a single traversal is left as an exercise.

AOC 2021 Index