From 6e4b95af18818803aa2dd2ad1a6549325361cfa4 Mon Sep 17 00:00:00 2001 From: germax26 Date: Mon, 2 Dec 2024 19:31:20 +1100 Subject: [PATCH] 2015 Day 01 in Haskell --- 2015/day01/day01.hs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 2015/day01/day01.hs diff --git a/2015/day01/day01.hs b/2015/day01/day01.hs new file mode 100644 index 0000000..4c98e2b --- /dev/null +++ b/2015/day01/day01.hs @@ -0,0 +1,19 @@ +import Aoc ( aocMain ) + +parseFile :: [String] -> [Char] +parseFile lines = let [line] = lines in line + +part1 :: [Char] -> Int +part1 ps = count '(' - count ')' + where count c = length $ filter (==c) ps + +part2 :: [Char] -> Int +part2 = f 0 + where + f 0 (')':_) = 1 + f n (')':ps) = f (n-1) ps + 1 + f n ('(':ps) = f (n+1) ps + 1 + f _ _ = undefined + +main :: IO () +main = aocMain parseFile part1 part2 \ No newline at end of file