2015 Day 01 in Haskell

This commit is contained in:
germax26 2024-12-02 19:31:20 +11:00
parent dde31570ed
commit 6e4b95af18
Signed by: germax26
SSH Key Fingerprint: SHA256:N3w+8798IMWBt7SYH8G1C0iJlIa2HIIcRCXwILT5FvM

19
2015/day01/day01.hs Normal file
View File

@ -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