aoc

advent of code
git clone git://source.orangerot.dev:/aoc.git
Log | Files | Refs

Main.hs (1121B)


      1 import Data.Char
      2 import Data.List
      3 
      4 -- findBegin :: ((Int, Int), Char) -> [((Int, Int), Char)] -> ((Int, Int), Char)
      5 findBegin s@((x,y), c) dict = case filter (\(p,_) -> p == (x,y-1)) dict of
      6   [] -> s
      7   n:xs -> findBegin n dict
      8 
      9 main :: IO ()
     10 main = do
     11   inputLines <- lines <$> getContents
     12 
     13   let distances = concat (map (\x -> (map (\y -> (x,y)) [-1..1])) [-1..1])
     14 
     15   let numInputLines = zip [0..] (map (zip [0..]) inputLines)
     16   let charCoords = concat (map (\(x, l) -> (map (\(y, c) -> ((x,y),c)) l)) numInputLines)
     17   let symbols = map (\(p, c) -> p) (filter (\(_,c) -> not (c == '.' || isDigit c)) charCoords)
     18   let digits = filter (\(_,c) -> isDigit c) charCoords
     19   let serialChars = filter (\((x,y),_) -> any (\x -> elem x symbols) (map (\(x1,y2) -> (x + x1, y + y2)) distances)) digits
     20   let serialBegins = nub $ map (\x -> findBegin x digits) serialChars
     21   let serialNumbers = map (\((x,y), c) -> read (takeWhile isDigit (drop y (inputLines!!x))) :: Integer) serialBegins
     22   -- print charCoords 
     23   -- print symbols
     24   print (sum serialNumbers)
     25   -- convert input to [((x,y), Char)]
     26 
     27   -- mapM_ putStrLn inputLines
     28