Implement 2015's day 3 because I'm a doofus

This commit is contained in:
Dominic Zimmer 2021-12-03 00:12:01 +01:00
parent 27c17c492e
commit 71bfba56e7
3 changed files with 31 additions and 0 deletions

29
3.hs Normal file
View 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)

1
3.in Normal file

File diff suppressed because one or more lines are too long

View File

@ -1,3 +1,4 @@
target:
ghc 1.hs
ghc 2.hs
ghc 3.hs