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
|
Loading…
Reference in New Issue
Block a user