mirror of
https://github.com/mx42/adventofcode.git
synced 2026-01-14 13:59:51 +01:00
Add 2015 day 3
This commit is contained in:
28
2015/day3.hs
Normal file
28
2015/day3.hs
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
import Data.List
|
||||||
|
|
||||||
|
move :: [(Int, Int)] -> Char -> [(Int, Int)]
|
||||||
|
move ((x, y):xs) '>' = (x + 1, y):((x, y):xs)
|
||||||
|
move ((x, y):xs) '<' = (x - 1, y):((x, y):xs)
|
||||||
|
move ((x, y):xs) '^' = (x, y + 1):((x, y):xs)
|
||||||
|
move ((x, y):xs) 'v' = (x, y - 1):((x, y):xs)
|
||||||
|
|
||||||
|
houses :: [Char] -> [(Int, Int)]
|
||||||
|
houses input = foldl move [(0, 0)] input
|
||||||
|
|
||||||
|
foldlBoth :: (a -> b -> a) -> (a, a) -> [b] -> (a, a)
|
||||||
|
foldlBoth f (l1, l2) (x1:(x2:xs)) = foldlBoth f (f l1 x1, f l2 x2) xs
|
||||||
|
foldlBoth f (l1, l2) (x1:_) = foldlBoth f (f l1 x1, l2) []
|
||||||
|
foldlBoth f (l1, l2) [] = (l1, l2)
|
||||||
|
|
||||||
|
housesWithBot :: [Char] -> [(Int, Int)]
|
||||||
|
housesWithBot input = santaHouses ++ botHouses
|
||||||
|
where (santaHouses, botHouses) = foldlBoth move ([(0, 0)], [(0, 0)]) input
|
||||||
|
|
||||||
|
uniqueHouses :: [(Int, Int)] -> [((Int, Int), Int)]
|
||||||
|
uniqueHouses input = (map (\xs -> (head xs, length xs)) . group . sort) input
|
||||||
|
|
||||||
|
day3part1 :: [Char] -> Int
|
||||||
|
day3part1 input = length (uniqueHouses (houses input))
|
||||||
|
|
||||||
|
day3part2 :: [Char] -> Int
|
||||||
|
day3part2 input = length (uniqueHouses (housesWithBot input))
|
||||||
Reference in New Issue
Block a user