The hard part of day 5 was getting the basic machine working. After that, the rest was fairly straightforward.
Representation
We have to simulate a machine, which is a four-digit display and various operations that change that display. I decided to represent it as a list of digits, using the Data.Digits library to convert between the list and the number it represents.
I used another data type for the buttons, so I had somewhere to store the input.
newtype Display = Disp [Int]
deriving (Show, Eq, Ord)
data Button = Null
| Append Int
| Multiply Int
| Divide Int
| Replace Int Int
| Oct
| Backspace
deriving (Show, Eq)This includes a Null button to make the part 1 solution easier.
Parsing the data to generate my list of buttons was simple enough with the attoparsec library, but noting to attempt to read the Replace button before the Append button, to stop the parser getting confused.
buttonsP = buttonP `sepBy` space
buttonP = "[" *> (replaceP <|> appendP <|> multiplyP <|> divideP <|> octP <|> backspaceP) <* "]"
replaceP = Replace <$> decimal <* "->" <*> decimal
appendP = Append <$> decimal
multiplyP = Multiply <$> ("*" *> decimal)
divideP = Divide <$> ("/" *> decimal)
octP = Oct <$ "oct"
backspaceP = Backspace <$ "bksp"Driving the machine
The core of this is the pair of functions, buttonPress and unsafeButtonPress. Both take a Button and a Display, apply the button's operation, and return the updated Display. unsafeButtonPress does the work, while buttonPress wraps it with the validation that the problem specifies.
buttonPress, unsafeButtonPress :: Button -> Display -> Display
buttonPress button display
| null ds = Disp [0]
| length ds > 4 = display
| otherwise = Disp ds
where (Disp ds) = unsafeButtonPress button display
unsafeButtonPress Null d = d
unsafeButtonPress (Append a) (Disp ds) = Disp $ if ds == [0] then [a] else ds ++ [a]
unsafeButtonPress (Multiply a) (Disp ds) = Disp $ D.digits 10 $ a * (D.unDigits 10 ds)
unsafeButtonPress (Divide a) (Disp ds) = Disp $ D.digits 10 $ (D.unDigits 10 ds) `div` a
unsafeButtonPress (Replace a b) (Disp ds) = Disp $ go <$> ds
where go d = if d == a then b else d
unsafeButtonPress Oct (Disp ds) = Disp $ D.digits 8 $ D.unDigits 10 ds
unsafeButtonPress Backspace (Disp ds) = Disp $ init ds Note that the Data.Digits library handles the base conversion for me.
Part 1
This is a list comprehension, of three choices of button press. I include a Null button here, to reflect choosing fewer than three button presses. I generate all the results, de-duplicate them, and add the results.
part1 buttons = sum $ reveal <$> nub results
where results = [buttonPress a $ buttonPress b $ buttonPress c (Disp [0])
| a <- buttons'
, b <- buttons'
, c <- buttons'
]
buttons' = Null : buttons
reveal :: Display -> Int
reveal (Disp ds) = D.unDigits 10 ds Part 2
This finds all distinct displays that are reachable from the starting point, by any number of button presses.
I handle this by maintaining an open set and a closed set. I assume that several different sequences of button presses will lead to the same result (e.g. repeated applications of Append and Backspace). Therefore, once I've found a particular display state, there's no need to track other ways of getting to that state.
A display state becomes closed if I've generated the results of all possible button presses from that state. Each of those new states gets added to the open set, but only if they're not already in the closed set. I then pick another display state from the open set and generate its successors.
Processing continues until there are no more open states
part2 buttons = sum $ fmap reveal $ S.toList result
where closed = S.empty
open = S.singleton (Disp [0])
result = findAllResults buttons open closed
findAllResults buttons open closed
| S.null open = closed
| otherwise = findAllResults buttons open'' closed'
where (display, open') = S.deleteFindMin open
news = S.fromList [buttonPress b display | b <- buttons]
closed' = S.insert display closed
news' = news `S.difference` closed'
open'' = S.union open' news'That gives the answer is a fairly short space of time.
Code
You can get the code from Codeberg.