2015 Day 08 in Haskell

This commit is contained in:
germax26 2024-12-03 05:52:55 +11:00
parent 5d2559af06
commit e46ff8585d
Signed by: germax26
SSH Key Fingerprint: SHA256:N3w+8798IMWBt7SYH8G1C0iJlIa2HIIcRCXwILT5FvM

20
2015/day08/day08.hs Normal file
View File

@ -0,0 +1,20 @@
import Aoc
part1 :: [String] -> Int
part1 = sum . map (\line -> length line - actualSize line)
where
actualSize' :: Int -> String -> Int
actualSize' n ('\\':'x':_:_:rest) = actualSize' (n + 1) rest
actualSize' n ('\\':_:rest) = actualSize' (n + 1) rest
actualSize' n ('"':_) = n
actualSize' n (_:rest) = actualSize' (n+1) rest
actualSize = actualSize' 0 . tail
part2 :: [String] -> Int
part2 = sum . map sizeDiff
where
count c = length . filter (==c)
sizeDiff line = 2 + count '"' line + count '\\' line
main = aocMain id part1 part2