diff --git a/2015/day03/day03.hs b/2015/day03/day03.hs new file mode 100644 index 0000000..f03cf24 --- /dev/null +++ b/2015/day03/day03.hs @@ -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 \ No newline at end of file diff --git a/aoc.hs b/aoc.hs index d1098aa..5f4d248 100644 --- a/aoc.hs +++ b/aoc.hs @@ -18,4 +18,10 @@ pair :: [a] -> (a, a) 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) \ No newline at end of file +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)