2015 Day 03 in Haskell
This commit is contained in:
parent
6ca8baf1b5
commit
dbc69a8a6d
24
2015/day03/day03.hs
Normal file
24
2015/day03/day03.hs
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
import Aoc
|
||||||
|
import Data.List
|
||||||
|
|
||||||
|
parseFile :: [String] -> [Char]
|
||||||
|
parseFile = single
|
||||||
|
|
||||||
|
move :: (Int, Int) -> Char -> (Int, Int)
|
||||||
|
move (x, y) 'v' = (x, y - 1)
|
||||||
|
move (x, y) '^' = (x, y + 1)
|
||||||
|
move (x, y) '<' = (x - 1, y)
|
||||||
|
move (x, y) '>' = (x + 1, y)
|
||||||
|
|
||||||
|
visited :: [Char] -> [(Int, Int)]
|
||||||
|
visited = uncurry (:) . foldl (\(loc, locs) c -> (move loc c, loc:locs)) ((0, 0), [])
|
||||||
|
|
||||||
|
part1 :: [Char] -> Int
|
||||||
|
part1 = length . nub . visited
|
||||||
|
|
||||||
|
part2 :: [Char] -> Int
|
||||||
|
part2 = length . nub . uncurry (++) . both visited . split
|
||||||
|
where split cs = foldr (\(c, i) (evens, odds) -> if even i then (c:evens, odds) else (evens, c:odds)) ([], []) (zip cs [0..])
|
||||||
|
|
||||||
|
main :: IO ()
|
||||||
|
main = aocMain parseFile part1 part2
|
8
aoc.hs
8
aoc.hs
@ -18,4 +18,10 @@ pair :: [a] -> (a, a)
|
|||||||
pair as = let [a1, a2] = as in (a1, a2)
|
pair as = let [a1, a2] = as in (a1, a2)
|
||||||
|
|
||||||
triplet :: [a] -> (a, a, a)
|
triplet :: [a] -> (a, a, a)
|
||||||
triplet as = let [a1, a2, a3] = as in (a1, a2, a3)
|
triplet as = let [a1, a2, a3] = as in (a1, a2, a3)
|
||||||
|
|
||||||
|
single :: [a] -> a
|
||||||
|
single as = let [a] = as in a
|
||||||
|
|
||||||
|
both :: (a -> b) -> (a, a) -> (b, b)
|
||||||
|
both f (a, b) = (f a, f b)
|
||||||
|
Loading…
Reference in New Issue
Block a user