Day 4 is about combining colours, and therefore is about monoids. But first a few words on how we get there.

We're given a bunch of coloured barcodes, like ??bg?v?? and have to find the shortest repeating sequence of colours that matches the whole barcode (truncating the repeats when we run out of barcode). Where only ? occurs in our repeating sequence, we can use whatever colour we want.

If we used a sequence of length three, the elements would have to be compatible with [Unknown,Green,Unknown] , [Unknown,Unknown,Unknown] , and [Blue,Violet]. The last doesn't work. The elements could combine into Green, Unknown, and Clash.

If we used a sequence of length four, the elements would have to be compatible with [Unknown,Unknown] , [Unknown,Violet], [Blue,Unknown],and [Green,Unknown]. Those all work.

That gives us two problems. One is to find the constraints on each element of a particular sequence. The other is how to combine them.

The first is some simple list manipulation. The Data.List.Split library has chunksOf that splits a list into a list of chunks of a given size. transpose swaps rows and columns, so transpose $ chunksOf 4 splits [Unknown, Unknown, Blue, Green, Unknown, Violet, Unknown, Unknown] into [[Unknown, Unknown], [Unknown, Violet], [Blue, Unknown], [Green, Unknown]].

The second part is where monoids come in.

Monoids

A monoid is the name for the class of "things that can be combined". The idea if that if you can combine any to things of a certain type, and produce another thing of the same type, those things are a monoid.

In Haskell, a lot of generic functions are defined to work over any monoid, using the specifics of the monoid for combining values. If you can declare a type to be an instance of Monoid, you get all of those for fee.

That's what I did.

I have a slightly expanded set of Colour:

data Colour = Unknown
            | Red
            | Orange 
            | Yellow
            | Green 
            | Blue
            | Violet
            | Clash
            deriving (Show, Eq, Ord, Enum, Bounded)

Defining a monoid is strangely two-step process (I blame excessive mathematical purity in some standards committtee). First, a Semigroup definition shows how to combine two colours. Two of the same colours combine into that colour, Unknown disappears, and two different fixed colours form a Clash. <> is the name of the generic "combining" operation.

instance Semigroup Colour where 
  Unknown <> x = x
  x <> Unknown = x
  x <> y 
    | x == y = x
    | otherwise = Clash

The Monoid definition shows what we mean by no colours. mempty is the generic name for "no things".

instance Monoid Colour where
  mempty = Unknown 

Now Colour is a Monoid, I get mconcat for free. mconcat collapses a list of things into one, in exactly the way I wanted to at the beginning of this post.

Which is a very long explanation for what turns out to be a very short piece of code.

The solution

I can use transpose and chunksOf to find the constraints on a sequence of a particular length, and fmap mconcat to summarise those constraints. A sequence length is invalid if that summary contains a Clash.

inValidInstructionLength :: Barcode -> Int -> Bool
inValidInstructionLength barcode k = Clash `elem` colours
  where everyKth = transpose $ chunksOf k barcode
        colours = fmap mconcat everyKth

The shortest sequence (program) is the first one that survives after dropping all the invalid ones.

shortestProgram :: Barcode -> Int
shortestProgram barcode = head $ dropWhile (inValidInstructionLength barcode) [1..]

Part 1 is sum the lengths over all the barcodes. Part 2 is find the sequence length over the combined barcode.

part1, part2 :: [Barcode] -> Int
part1 barcodes = sum $ fmap shortestProgram barcodes
part2 barcodes =shortestProgram $ concat barcodes

And that's it.

Code

You can get the code from Codeberg.