2015 Day 03 in Haskell

This commit is contained in:
germax26 2024-12-02 22:09:59 +11:00
parent 6ca8baf1b5
commit dbc69a8a6d
Signed by: germax26
SSH Key Fingerprint: SHA256:N3w+8798IMWBt7SYH8G1C0iJlIa2HIIcRCXwILT5FvM
2 changed files with 31 additions and 1 deletions

24
2015/day03/day03.hs Normal file
View 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

6
aoc.hs
View File

@ -19,3 +19,9 @@ pair as = let [a1, a2] = as in (a1, a2)
triplet :: [a] -> (a, a, a)
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)