Advent of Code 2026 Day 5
Puzzle: 2026 day 5 · Code: Day5.hs
Initial Thoughts
- I think this is the first one this year where it seems reasonable to create a
Parser - So I can define some ADT for the various operations, something like
data Operation = Append Int | Multiply Int | Divide Int | Replace Int Int | Octal | Backspace - and then I can define a function like
operate :: Operation -> Int -> Int - maybe
Intwon’t be the right choice for the state but that’s fine - once I have my parser defined I just need a simple function to get all triples of functions and then we’re pretty much done
Code for Part 1
module Day5 where
import Control.Monad (replicateM)
import Data.Char (intToDigit)
import Data.List.Extra (dropEnd, nub)
import Numeric (showOct)
import Text.Parsec as P (char, choice, digit, many1, sepBy, string, try)
import Text.ParserCombinators.Parsec (Parser, parse)
-- still not sure whether using `Int` or `String` is best to represent the state here tbh
data Operation
= Append Int
| Multiply Int
| Divide Int
| Replace Int Int
| Octal
| Backspace
deriving (Show)
part1 :: IO Int
part1 = sum . nub . map (foldl' operate 0) . allTriples <$> getInput
-- I looked this up, I would have done a silly list comp otherwise
allTriples :: [a] -> [[a]]
allTriples = replicateM 3
operateMany :: Int -> [Operation] -> Int
operateMany = foldl' operate
-- a lot of this is basically `read . f . show` which perhaps suggests I should use a string rep...
operate :: Int -> Operation -> Int
operate s (Append x) = read (show s ++ show x)
operate s (Multiply x) = s * x
operate s (Divide x) = s `div` x
operate s (Replace x y) = read . map (\q -> if q == intToDigit x then intToDigit y else q) . show $ s
operate s (Octal) = octal s
operate s (Backspace) = (\x -> if x == "" then 0 else read x) . dropEnd 1 . show $ s
-- feels like cheating tbh
octal :: Int -> Int
octal = read . (`showOct` "")
operationParser :: Parser Operation
operationParser =
P.choice . map P.try $
[ appendParser,
multParser,
divParser,
replaceParser,
octParser,
bkspParser
]
where
-- "[A]" -> Append A
appendParser = Append <$ P.char '[' <*> intParser <* P.char ']'
-- "[*A]" -> Multiply A
multParser = Multiply <$ P.char '[' <* P.char '*' <*> intParser <* P.char ']'
-- "[/A]" -> Multiply A
divParser = Divide <$ P.char '[' <* P.char '/' <*> intParser <* P.char ']'
-- "[A->B]" -> Replace A B
replaceParser = Replace <$ P.char '[' <*> intParser <* P.string "->" <*> intParser <* P.char ']'
-- "[oct]" -> Octal
octParser = Octal <$ P.string "[oct]"
-- "[bksp]" -> Backspace
bkspParser = Backspace <$ P.string "[bksp]"
-- "34" -> 34
intParser :: Parser Int
intParser = read <$> P.many1 P.digit
unsafeParse :: Parser a -> String -> a
unsafeParse p s = case parse p "" s of
Left res -> error . show $ res
Right res -> res
getInput :: IO [Operation]
getInput = unsafeParse inputParser <$> readFile "./fixtures/input5.txt"
where
inputParser = operationParser `P.sepBy` (P.char ' ')
Reflections on Part 1
- this was pretty mechanical
- I also found it a bit fiddly for some reason, although not too hard!
- I feel my state rep maybe isn’t quite right
- I perhaps leveraged lib functions like
showOctandreplicateMtoo much? - Did I really need to write a parser too? probs not
Initial Thoughts on Part 2
- we could definitely get cycles, for example backspace + append would give a cycle I think
- so we need to make sure that we terminate if we’ve already found a number and explored the button presses
- ok so I think I have a fairly simple “algorithm” idea for this
- keep track of two
Sets- candidate numbers we want to check
- numbers we’ve seen already
- for all candidates
- attempt all button presses (being careful to remove any numbers of more than 4 chars) and
unionthe result with ourSetof seen numbers - check which numbers we’ve not yet seen and add them to our candidate
Set
- attempt all button presses (being careful to remove any numbers of more than 4 chars) and
- keep track of two
- I think the above will terminate reasonably quickly and the
Setsizes will stay below 10,000 elems so I should be ok - So I can basically brute force this
Code for Part 2
data SearchState = SS {_candidates :: S.Set Int, _found :: S.Set Int}
part2 :: IO (Maybe Int)
part2 = do
operations <- S.fromList <$> getInput
return
. fmap (sum . _found)
. find (S.null . _candidates)
. iterate (`runIteration` operations)
$ SS (S.singleton 0) (S.singleton 0)
runIteration :: SearchState -> S.Set Operation -> SearchState
runIteration (SS candidates alreadyFound) operations = (SS newCandidates allFound)
where
newlyFound = S.filter (<= 9999) . S.map (uncurry operate) . S.cartesianProduct candidates $ operations
newCandidates = S.difference newlyFound alreadyFound
allFound = S.union newlyFound alreadyFound
Final Thoughts
- I’m quite glad my approach to part 2 just worked straight away
- It was just a fairly simple breadth-first search
- I discovered
Preludeactually has anuntilfunction and this is very useful, better than usingfind, which is what I’ve been doing for years lol
Final Full Solution
module Day5 where
import Control.Monad (replicateM)
import Data.Char (intToDigit)
import Data.List.Extra (dropEnd, nub)
import qualified Data.Set as S
import Numeric (showOct)
import Text.Parsec as P (char, choice, digit, many1, sepBy, string, try)
import Text.ParserCombinators.Parsec (Parser, parse)
-- still not sure whether using `Int` or `String` is best to represent the state here tbh
data Operation
= Append Int
| Multiply Int
| Divide Int
| Replace Int Int
| Octal
| Backspace
deriving (Show, Eq, Ord)
data SearchState = SS {_candidates :: S.Set Int, _found :: S.Set Int}
part1 :: IO Int
part1 = sum . nub . map (foldl' operate 0) . allTriples <$> getInput
part2 :: IO Int
part2 = do
operations <- S.fromList <$> getInput
return
. sum
. _found
. until (S.null . _candidates) (`runIteration` operations)
$ SS (S.singleton 0) (S.singleton 0)
runIteration :: SearchState -> S.Set Operation -> SearchState
runIteration (SS candidates alreadyFound) operations = (SS newCandidates allFound)
where
newlyFound = S.filter (<= 9999) . S.map (uncurry operate) . S.cartesianProduct candidates $ operations
newCandidates = S.difference newlyFound alreadyFound
allFound = S.union newlyFound alreadyFound
-- I looked this up, I would have done a silly list comp otherwise
allTriples :: [a] -> [[a]]
allTriples = replicateM 3
-- a lot of this is basically `read . f . show` which perhaps suggests I should use a string rep...
operate :: Int -> Operation -> Int
operate s (Append x) = read (show s ++ show x)
operate s (Multiply x) = s * x
operate s (Divide x) = s `div` x
operate s (Replace x y) = read . map (\q -> if q == intToDigit x then intToDigit y else q) . show $ s
operate s (Octal) = octal s
operate s (Backspace) = (\x -> if x == "" then 0 else read x) . dropEnd 1 . show $ s
-- feels like cheating tbh
octal :: Int -> Int
octal = read . (`showOct` "")
operationParser :: Parser Operation
operationParser =
P.choice . map P.try $
[ appendParser,
multParser,
divParser,
replaceParser,
octParser,
bkspParser
]
where
-- "[A]" -> Append A
appendParser = Append <$ P.char '[' <*> intParser <* P.char ']'
-- "[*A]" -> Multiply A
multParser = Multiply <$ P.char '[' <* P.char '*' <*> intParser <* P.char ']'
-- "[/A]" -> Divide A
divParser = Divide <$ P.char '[' <* P.char '/' <*> intParser <* P.char ']'
-- "[A->B]" -> Replace A B
replaceParser = Replace <$ P.char '[' <*> intParser <* P.string "->" <*> intParser <* P.char ']'
-- "[oct]" -> Octal
octParser = Octal <$ P.string "[oct]"
-- "[bksp]" -> Backspace
bkspParser = Backspace <$ P.string "[bksp]"
-- "34" -> 34
intParser :: Parser Int
intParser = read <$> P.many1 P.digit
unsafeParse :: Parser a -> String -> a
unsafeParse p s = case parse p "" s of
Left res -> error . show $ res
Right res -> res
getInput :: IO [Operation]
getInput = unsafeParse inputParser <$> readFile "./fixtures/input5.txt"
where
inputParser = operationParser `P.sepBy` P.char ' '