Created
December 3, 2023 19:39
-
-
Save anka-213/779bc938a65348d94546d54d3a53f8ad to your computer and use it in GitHub Desktop.
Haskell solution of Advent of Code 2023 day 3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# 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