Day 2 already had me leaning on the type system to help me keep things straight.

Representation

I started by defining a data type for waypoints, to keep things organised.

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

readWaypoint :: Char -> Waypoint
readWaypoint 'r' = Red
readWaypoint 'o' = Orange 
readWaypoint 'y' = Yellow
readWaypoint 'g' = Green 
readWaypoint 'b' = Blue
readWaypoint 'v' = Violet
readWaypoint _ = error "Invalid waypoint code"

That means reading the input is as simple as readWaypoint <$> text.

Part 1

The key is to find the step size between successively-visited waypoints. I could create an explicit list of all the waypoints (using cycle to create the infinite list), but I didn't need it. All I need is the step size, and I can get that from the ordering of the Waypoint type.

The step size is roughly (there - here), but wrapping around the cycle of waypoints. For example, the step size between Red and Green is Green - Red for 3 steps, but going from Blue to Yellow is the 4 steps wrapping around the list.

|--|
roygbv
--| |-

The Enum instance of Waypoint allows me to convert waypoints into integers, allowing the arithmetic to happen. There are two cases to deal with:

  1. If there > here, the difference is there - here
  2. Otherwise, the difference is 6 - here + there

For the examples above, Red to Green is the first case, for 3 - 0 = 3. Blue to Yellow is the second case, for 6 - 4 + 2 = 4.

To keep things generic, I use maxBound instead of hard-coding the 6.

That gives me the implementation of stepSize:

stepSize :: (Waypoint, Waypoint) -> Int
stepSize (a, b) 
  | b > a = bi - ai
  | otherwise = ui + 1 - ai + bi
  where ai = fromEnum a
        bi = fromEnum b
        ui = fromEnum (maxBound :: Waypoint)

Then I need to make the list of pairs for each step, starting with the initial Red. (that's a zip). I then apply stepSize to each step in turn and sum the sizes.

part1 :: [Waypoint] -> Int
part1 program = sum $ fmap stepSize $ zip (Red : program) $ program

Part 2

The overall structure of this smelt a bit like one of the recursion schemes. It's about converting one value (a set of waypoints to visit) to another value (a total length). A bit of thinking led me to recognise it as a hylomorphism, a combination of an anamorphism (i.e. an unfold) and a catamorphism (i.e. a fold). In this case, I unfold the set of visited waypoints into a list of step distances, then fold that list of distances into a total. Along the way, I trust the Haskell compiler's use of laziness and optimisation to never explicitly generate the full list, keeping things efficient. (I didn't actually profile this, so I didn't check.)

The first step is to convert the list of waypoints of the program into a multiset of waypoints: a set where each element is annotated with the number of occurrences of that element. That uses the fromList function from the multiset library.

Now the hard part, the unfold. This follows the logic of the problem description.

Given a current location and a multiset of remaining waypoints to visit:

  1. Find the list of all distinct remaining waypoints.
  2. Find the distance from here to each of them.
  3. Pick the waypoint with the largest distance. Call it there.
  4. Remove one instance of there from the multiset of waypoints.
  5. Return the distance, there and the remaining waypoints.

If the multiset is empty, return Nothing to stop the unfolding.

In Haskell terms, the thing being unfolded is the pair (here, waypoints) and it's being unfolded into a list of Int. That fixes the type signature to be furthestWaypoint :: (Waypoint, WaypointSet) -> Maybe (Int, (Waypoint, WaypointSet)). Relying on that type signature fixes a lot of the types of the intermediate steps, which was a great help in putting the function together. This is the result.

furthestWaypoint :: (Waypoint, WaypointSet) -> Maybe (Int, (Waypoint, WaypointSet))
furthestWaypoint (here, waypoints) 
  | MS.null waypoints = Nothing
  | otherwise = Just (distance, (there, waypoints'))                    -- 5 
  where availableDests = MS.distinctElems waypoints                     -- 1
        distances = fmap (\t -> (stepSize (here, t), t)) availableDests -- 2
        (distance, there) = head $ reverse $ sort distances             -- 3
        waypoints' = MS.delete there waypoints                          -- 4

All that's left is combining the unfold and fold.

part2 :: [Waypoint] -> Int
part2 program = sum $ unfoldr furthestWaypoint (Red, wps)
  where wps = MS.fromList program

All of this is a bit of a long way round to get the answer, but it's one using some general-purpose machinery to get there.

Code

You can get the code from Codeberg.