1
0
Fork 0

2020 day 6 - combine both parts

This commit is contained in:
Terrana Ninetailed 2020-12-06 11:17:23 +00:00
parent 9ad0282e0c
commit dc729c40a8
2 changed files with 8 additions and 31 deletions

View file

@ -4,12 +4,17 @@ import Data.List
main :: IO ()
main = do
input <- readFile "input"
print . sum . map length . groups $ input
putStrLn . formatOutput . map (sum . map length) . transpose . groups . dlfsplit $ input
formatOutput :: Show a => [a] -> String
formatOutput (part1:part2:_) = "Part 1: " ++ show part1 ++ "\nPart 2: " ++ show part2
-- Collect each group's answers
groups :: String -> [String]
groups = map (foldr1 union . lines) . dlfsplit
groups :: [String] -> [[String]]
groups [] = []
groups (g:gs) = [foldr1 union g', foldr1 intersect g'] : groups gs
where g' = lines g
-- Split a string into a list across all the double newlines in it
dlfsplit :: String -> [String]

View file

@ -1,28 +0,0 @@
import Data.List
main :: IO ()
main = do
input <- readFile "input"
print . sum . map length . groups $ input
-- Collect each group's answers
groups :: String -> [String]
groups = map (foldr1 intersect . lines) . dlfsplit
-- Split a string into a list across all the double newlines in it
dlfsplit :: String -> [String]
dlfsplit str = a : case b of
[] -> []
b -> dlfsplit b
where (a,b) = dlfsplitonce str
-- Split a string into two across the first double newline found
dlfsplitonce :: String -> (String, String)
dlfsplitonce [] = ([], [])
dlfsplitonce (ca:cb:rest)
| ca == '\n' && cb == '\n' = ([], rest)
| otherwise = (ca:before, after)
where (before, after) = dlfsplitonce (cb : rest)
dlfsplitonce c = (c, [])