advent-of-code/2015/day03/day03.hs
2024-12-02 22:09:59 +11:00

24 lines
678 B
Haskell

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