mirror of
https://github.com/mx42/adventofcode.git
synced 2026-01-14 13:59:51 +01:00
Add 2015 day 8
This commit is contained in:
29
haskellAoC/src/Y2015/Day08.hs
Normal file
29
haskellAoC/src/Y2015/Day08.hs
Normal 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
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user