Advent Of Code

Chapter 05: Microwave Mania

What kind of microwave is that

We’re now stopping at some kind of factory to load some kind of magic smoke.

For once, the elves don’t need my help for anything, so I can finally relax!

…

Ok, weirdly enough, relaxing is quite boring. So I decide to look around. I stumble into a room filled with elves trying to operate some kind of microwave. Maybe I can help them?

Part one

The elves are trying to input the appropriate clock time for their lunch, but the problem is that they’re microwave is fancy (a fancy word to say it is really stupid). The buttons are all mathematical operations:

I can easily parse that as Haskell functions!

type Input = [Operation]

type Output = Int

type Operation = (Int -> Int)

getOperation :: String -> Operation
getOperation ('[' : "oct]") = read . (`showOct` "")
getOperation ('[' : "bksp]") = (`div` 10)
getOperation ('[' : '*' : num) = (* read (init num))
getOperation ('[' : '/' : num) = (`div` read (init num))
getOperation ('[' : xs)
  | '-' `elem` xs = let [a, b] = map pack $ splitOn "->" (init xs) in read . unpack . replace a b . pack . show
  | otherwise = \x -> x * 10 + read (init xs)
getOperation _ = error "Unsupported operation"

parseInput :: String -> Input
parseInput = map getOperation . splitOn " "

Now, the elves want to know all the digits they can write using only three buttons.

How about just bruteforcing the solution? I can just get all possible combinations of operations up to 3 operations, and then perform the operations by applying the one by one, starting with the number 0. There aren’t that many combinations (we have 9 buttons, so 9 ^ 3 + 9 ^ 2 + 9 + 1 = 820)

Getting all the combinations up to n is something that might come useful later, so how about I add it to my Utils library:

-- Get all combinations of length at most n
combinationsTo :: Int -> [a] -> [[a]]
combinationsTo n xs = [] : concat [mapM (const xs) [1 .. k] | k <- [1 .. n]]

(There are Haskell shenanigans here with the mapM and const, I might come back and explain it later, but I am a bit too tired for that right now)

Now, let’s just bruteforce!

partOne :: Input -> Output
partOne = sum . nub . map (foldl (&) 0) . combinationsTo 3

Part two, the part where I need to be smart

Actually, three buttons isn’t enough. How about infinitely many presses ? That’s a bummer, I need to be smart now.

Fortunately, there is one more constrain about this microwave that plays into our favor: we can’t have more than 4 digits in the input number, and any operation that would yield more than that becomes a no-op.

So, in order to find all the numbers we can form, we can simply find a Spanning Tree of all the possible numbers. The idea is simple: we start with number 0, then the next possible numbers can be found by trying all the buttons with input 0. Then we traverse all these nexts input in the same way.

We could use either a dfs or bfs for that. Both would work, but I’d prefer using a dfs here (no need for a queue, that’s just a fold in Haskell). A bfs would be a good idea to use if we wanted to re-do part one in the same way, as we could put a breadth limit of 3 and get all the possible combinations.

The idea is simple: we have a set of already visited nodes that start as singleton 0. Then to get all the next states we get all the results of each operation with the current input, and we remove the ones that are greater than or equal to 10000 (more than 4 digits) or are in the set of already visited input. We mark the resulting inputs as visited, and we go on with them:

partTwo :: Input -> Int
partTwo input = sum . toList $ dfs 0 (singleton 0)
  where
    dfs n seen = foldr dfs seen' nexts
      where
        nexts = filter (`notMember` seen) . filter (< 10000) $ map ($ n) input
        seen' = foldr insert seen nexts

This dfs is so simple and useful that I could maybe add it to my utils library (although there probably is already some function that finds a spanning tree somewhere). I’ll do that if I need something like that again!

Chapter epilogue

You know what? All this microwave talks made me hungry. Time to eat!