Add 2015 day 8

This commit is contained in:
Xavier Morel
2020-12-07 18:47:22 +01:00
parent cb79a32fd5
commit 062789f430
4 changed files with 335 additions and 0 deletions

View File

@@ -0,0 +1,29 @@
module Y2015.Day08 (y15day08) where
import Data.List
decodeStr :: String -> String
decodeStr = dropFirstLast . removeStuff
where dropFirstLast = tail . init
removeStuff s@(h:t)
| "\\x" `isPrefixOf` s = removeStuff (drop 3 s)
| "\\\\" `isPrefixOf` s = h:(removeStuff (drop 1 t))
| "\\" `isPrefixOf` s = removeStuff t
| otherwise = h:(removeStuff t)
removeStuff "" = ""
encodeStr :: String -> String
encodeStr input = "\"" ++ (encode' input) ++ "\""
where encode' (h:t)
| h == '\\' = "\\\\" ++ (encode' t)
| h == '"' = "\\\"" ++ (encode' t)
| otherwise = h:(encode' t)
encode' "" = ""
y15day08 :: [String] -> (String, String)
y15day08 input = (part1, part2)
where part1 = show $ (raw_length - decoded_length)
part2 = show $ (encoded_length - raw_length)
decoded_length = sum $ map (length . decodeStr) input
encoded_length = sum $ map (length . encodeStr) input
raw_length = sum $ map length input

View File

@@ -7,6 +7,7 @@ import Y2015.Day04
import Y2015.Day05
import Y2015.Day06
import Y2015.Day07
import Y2015.Day08
year2015 :: String -> [String] -> (String, String)
year2015 "01" = y15day01
@@ -16,3 +17,4 @@ year2015 "04" = y15day04
year2015 "05" = y15day05
year2015 "06" = y15day06
year2015 "07" = y15day07
year2015 "08" = y15day08