r/haskell • • 12h ago

Advent of Code 2026 Day 5

https://adventofcode.com/2026/day/5
1 Upvotes

2 comments sorted by

2

u/Apterygiformes 2h ago

Had some weirdness with haskell Set type where unions didn't behave as expected, so I was converting back and forth between Set and List to achieve a full union. But anyways, seems to work

import qualified Data.Set as S
import Numeric (showOct)

type Button = Int -> Int

append :: Int -> Button
append a n = read $ (show n) ++ show a

multiply :: Int -> Button
multiply a n = a * n

divide :: Int -> Button
divide a n = n `div` a

replace :: Int -> Int -> Button
replace a b n = read $ concat $ map (\c -> if [c] == (show a) then show b else [c]) (show n)

oct :: Button
oct n = read $ showOct n ""

bksp :: Button
bksp = read . ('0' :) . reverse . drop 1 . reverse . show

limit :: Button -> Button
limit b n | n' > 9999 = n
          | otherwise = n'
          where
            n' = b n

-- [*9] [0->3] [/3] [*5] [5] [oct] [1->9] [9] [bksp]
buttons :: [Button]
buttons = map (limit) [
                multiply 9, replace 0 3, divide 3,
                multiply 5, append 5, oct,
                replace 1 9, append 9, bksp ]

apply :: Int -> S.Set Int
apply n = S.fromList $ map (\b -> b n) buttons

applyN :: S.Set Int -> S.Set Int
applyN ns = S.fromList $ concat $ map (S.toList . apply) $ S.toList ns

unionN :: [S.Set Int] -> S.Set Int
unionN ss = S.fromList $ concat $ map (S.toList) ss

combinations :: S.Set Int -> S.Set Int -> S.Set Int
combinations acc ns | S.null ns = acc
                    | otherwise = combinations acc' ns''
                where
                  ns' = applyN ns
                  acc' = unionN [acc, ns, ns']
                  ns'' = S.difference ns' acc

main :: IO ()
main = do
  let result = sum $ combinations S.empty (S.singleton 0)
  putStrLn $ show result
  return ()

1

u/Atijohn 1h ago

I love (<*>)

{-# LANGUAGE LambdaCase #-}
import Data.Char
import Data.Function
import Data.Bifunctor
import Data.List
import Control.Monad
import Debug.Trace
import Data.Maybe

main = print
     . join bimap
       (sum
      . uniq
      . concat)
      . ((,) <$> take 4
             <*> map snd . takeWhile (\(a, b) -> union a b /= a) . (zip <*> drop 1))
     . flip iterate [0]
     . (filter (<=9999) .)
     . (uniq .)
     . (<*>)
     . map ((\case '*' : n -> (* read n)
                   '/' : n -> (`div` read n)
                   'o' : _ -> oct
                   'b' : _ -> (`div` 10)
                   xs -> case break (not . isDigit) xs of
                           (n, '-':'>':m) -> read
                                           . foldr (\x xs -> maybe (x:xs) (m++)
                                                           $ stripPrefix n (x:xs)) []
                                           . show
                           (n, _) -> read . flip shows n)
        . init
        . tail)
     . words
   =<< getContents
  where oct 0 = 0
        oct n = n `mod` 8 + 10 * oct (n `div` 8)
        uniq = map head . group . sort