Compare commits

..

No commits in common. "63c6f24ef5028f9b0cf281111b515530c66f7c84" and "579076783102725d70541fc963bc2bc6c90e85ad" have entirely different histories.

6 changed files with 8 additions and 18 deletions

View file

@ -11,4 +11,3 @@ https://adventofcode.com/2020/
| 3 | `**` | `**` | | | 3 | `**` | `**` | |
| 4 | `**` | `**` | | | 4 | `**` | `**` | |
| 5 | | `**` | | | 5 | | `**` | |
| 6 | | `**` | |

View file

@ -1,7 +1,5 @@
module AoC where module AoC where
import Data.Function (on)
import Data.List (groupBy, intercalate)
import Text.ParserCombinators.ReadP import Text.ParserCombinators.ReadP
(.:) :: (c -> d) -> (a -> b -> c) -> a -> b -> d (.:) :: (c -> d) -> (a -> b -> c) -> a -> b -> d
@ -13,9 +11,6 @@ oneCompleteResult p s = case readP_to_S (p <* eof) s of
[(x, "")] -> Just x [(x, "")] -> Just x
_ -> Nothing _ -> Nothing
splitOnEmptyLines :: String -> [[String]]
splitOnEmptyLines = filter (not . any null) . groupBy ((==) `on` null) . lines
runAoC :: (Show r1, Show r2) => (String -> i) -> (i -> r1) -> (i -> r2) -> IO () runAoC :: (Show r1, Show r2) => (String -> i) -> (i -> r1) -> (i -> r2) -> IO ()
runAoC inputTransform part1 part2 = do runAoC inputTransform part1 part2 = do
contents <- inputTransform <$> getContents contents <- inputTransform <$> getContents

View file

@ -23,6 +23,6 @@ countHits = length . filterMap lookup . catMaybes .: compose2 (zipWith mzip) may
treeCharToBool :: Char -> Bool treeCharToBool :: Char -> Bool
treeCharToBool = (== '#') treeCharToBool = (== '#')
main = runAoC (map (cycle . map treeCharToBool) . lines) part1 part2 main = runAoC (fmap (cycle . map treeCharToBool) <$> lines) part1 part2
where part1 = countHits (3, 1) where part1 = countHits (3, 1)
part2 = product . (fmap countHits [(1,1), (3,1), (5,1), (7,1), (1,2)] ??) part2 = product . (fmap countHits [(1,1), (3,1), (5,1), (7,1), (1,2)] ??)

View file

@ -3,6 +3,8 @@ import AoC
import Control.Applicative ((<|>)) import Control.Applicative ((<|>))
import Control.Monad (guard, mfilter) import Control.Monad (guard, mfilter)
import Data.Char (isDigit) import Data.Char (isDigit)
import Data.Function (on)
import Data.List (groupBy, intercalate)
import Data.Maybe (mapMaybe) import Data.Maybe (mapMaybe)
import Text.ParserCombinators.ReadP import Text.ParserCombinators.ReadP
import Text.Read (readMaybe) import Text.Read (readMaybe)
@ -87,10 +89,13 @@ parseFieldNames = spaceSeparated parseFieldName
parsePassport :: ReadP [Field] parsePassport :: ReadP [Field]
parsePassport = spaceSeparated parseField parsePassport = spaceSeparated parseField
splitOnEmptyLines :: String -> [String]
splitOnEmptyLines = map (intercalate " ") . filter (not . any null) . groupBy ((==) `on` null) . lines
hasAllRequiredFields :: String -> Bool hasAllRequiredFields :: String -> Bool
hasAllRequiredFields = maybe False containsAllFields . fieldNames hasAllRequiredFields = maybe False containsAllFields . fieldNames
where requiredFieldNames = ["byr", "iyr", "eyr", "hgt", "hcl", "ecl", "pid"] where requiredFieldNames = ["byr", "iyr", "eyr", "hgt", "hcl", "ecl", "pid"]
containsAllFields fields = all (`elem` fields) requiredFieldNames containsAllFields fields = all (`elem` fields) requiredFieldNames
fieldNames = oneCompleteResult parseFieldNames fieldNames = oneCompleteResult parseFieldNames
main = runAoC (filter hasAllRequiredFields . map (intercalate " ") . splitOnEmptyLines) (length) (length . mapMaybe (oneCompleteResult parsePassport)) main = runAoC (filter hasAllRequiredFields . splitOnEmptyLines) (length) (length . mapMaybe (oneCompleteResult parsePassport))

View file

@ -18,6 +18,6 @@ findHole (x:y:ys) | y == next = findHole $ y:ys
where next = succ x where next = succ x
findHole _ = Nothing findHole _ = Nothing
main = runAoC (map (fromJust . binarify) . lines) part1 part2 main = runAoC (fmap (fromJust . binarify) <$> lines) part1 part2
where part1 = foldr1 max where part1 = foldr1 max
part2 = fromJust . findHole . sort part2 = fromJust . findHole . sort

View file

@ -1,9 +0,0 @@
import AoC
import qualified Data.Set as S
import Data.Set (Set)
main = runAoC ((fmap . fmap) S.fromList . splitOnEmptyLines) part1 part2
where part1 = countSetsFolded S.union
part2 = countSetsFolded S.intersection
countSetsFolded f = sum . map (S.size . foldr1 f)