Implement 2015's day 3 because I'm a doofus
This commit is contained in:
parent
27c17c492e
commit
71bfba56e7
29
3.hs
Normal file
29
3.hs
Normal file
@ -0,0 +1,29 @@
|
||||
{-# 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 . odds . tail $ l]
|
||||
where
|
||||
odds [] = []
|
||||
odds [x] = [x]
|
||||
odds (x:y:r) = x:(odds r)
|
Loading…
Reference in New Issue
Block a user