Skip to content

Instantly share code, notes, and snippets.

@anka-213
Created December 3, 2023 19:39
Show Gist options
  • Save anka-213/779bc938a65348d94546d54d3a53f8ad to your computer and use it in GitHub Desktop.
Save anka-213/779bc938a65348d94546d54d3a53f8ad to your computer and use it in GitHub Desktop.
Haskell solution of Advent of Code 2023 day 3
{-# LANGUAGE TypeApplications #-}
import Data.Char
import Data.Bifunctor (second)
import Data.Function
import Data.List
main = interact $ solution . lines
map3 f (a,b,c) = (f a, f b, f c)
splitMiddle ((a,b,c),(d,mid,f),(g,h,i)) = (mid, [a,b,c,d,f,g,h,i])
solution lns =
show . sum . concat
. map ( map (read @Int . map fst)
. filter (any snd)
. filter (isDigit.fst.head)
. groupBy ((==) `on` (isDigit.fst))
. (map.second) (any isValidSymbol)
)
. getNeighbors $ lns
getNumber xs = (map fst xs, any snd xs)
getNeighbors = map (map splitMiddle) . neighborLines . map neighborCols
filler = repeat ('.','.','.')
neighborLines lns = zipWith3 zip3 (filler : lns) lns (tail lns ++ [filler])
neighborCols line = zip3 ('.' : line) line (tail line ++ ".")
isValidSymbol c = c /= '.' && not (isDigit c)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment