2020 day 6
This commit is contained in:
parent
e9dfbe80a6
commit
9ad0282e0c
3 changed files with 2246 additions and 0 deletions
2190
2020/day06/input
Normal file
2190
2020/day06/input
Normal file
File diff suppressed because it is too large
Load diff
28
2020/day06/part1.hs
Normal file
28
2020/day06/part1.hs
Normal file
|
@ -0,0 +1,28 @@
|
|||
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 union . 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, [])
|
28
2020/day06/part2.hs
Normal file
28
2020/day06/part2.hs
Normal file
|
@ -0,0 +1,28 @@
|
|||
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, [])
|
Loading…
Reference in a new issue