stanosphere

Advent of Code 2026 Day 5

Puzzle: 2026 day 5 · Code: Day5.hs

Initial Thoughts

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

Initial Thoughts on Part 2

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

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 ' '