aoc2021/3.hs
2021-12-03 00:15:24 +01:00

31 lines
910 B
Haskell

{-# LANGUAGE ScopedTypeVariables #-}
import Data.List
main = do
lines <- readFile "3.in"
putStrLn . show . solve . (!!0) . words $ lines
where
solve = length . Data.List.group . Data.List.sort . partB
-- solves all lines, returns a string
partA :: [Char] -> [(Int, Int)]
partA = getPositions
where
getPositions = snd . foldl updatePosition ((0,0), [(0,0)]) . map coords
-- updatePosition :: b -> a -> b
updatePosition ((x, y), positions) (a, b) = ((x + a, y + b), (x + a, y + b):positions)
-- (right, up)
coords '>' = (1, 0)
coords '<' = (-1, 0)
coords 'v' = (0, -1)
coords '^' = (0, 1)
coords _ = (0, 0)
partB :: [Char] -> [(Int, Int)]
partB l = id =<< [ partA . odds $ l, partA . evens $ l]
where
evens = odds . tail
odds [] = []
odds [x] = [x]
odds (x:y:r) = x:(odds r)