adventofcode-2020/day6/main.hs

73 lines
1.7 KiB
Haskell
Raw Permalink Normal View History

2020-12-07 11:13:02 +01:00
#! /usr/bin/env -S"ANSWER=42" nix-shell
#! nix-shell -p ghcid
#! nix-shell -p "haskellPackages.ghcWithPackages (p: with p; [shower])"
#! nix-shell -i "ghcid -c 'ghci -Wall' -T main"
{-# LANGUAGE OverloadedStrings #-}
2020-12-07 15:54:34 +01:00
-- import Debug.Trace (trace)
import Data.List (nub)
exampleData :: [String]
exampleData =
[ "abc"
, ""
, "a"
, "b"
, "c"
, ""
, "ab"
, "ac"
, ""
, "a"
, "a"
, "a"
, "a"
, ""
, "b"
]
-- Count how many different chars per paragraph and sum them
countUniqueAnswers :: [String] -> Int
countUniqueAnswers = length . nub . concat
paragraphs :: [String] -> [[String]]
paragraphs strs = go (strs, [])
where
go :: ([String], [[String]]) -> [[String]]
go ([], ys) = ys
go (x:xs, []) = go (xs, [x] : [])
go ("":xs, ys) = go (xs, [] : ys)
go (x:xs, y:ys) = go (xs, (x:y) : ys)
solvePart1 :: [String] -> Int
solvePart1 = sum . (map countUniqueAnswers) . paragraphs
2020-12-07 19:32:40 +01:00
allAnsweredX :: Char -> [[String]] -> [Bool]
allAnsweredX c = map (all (any (== c)))
2020-12-07 15:54:34 +01:00
2020-12-07 19:32:40 +01:00
solvePart2 :: [String] -> Int
solvePart2 strs = length . (filter (== True)) $ concat $ map (\x -> (allAnsweredX x . paragraphs) strs) ['a'..'z']
2020-12-07 15:54:34 +01:00
2020-12-07 11:13:02 +01:00
main :: IO ()
main = do
putStrLn "Day 6 - Part 1"
2020-12-07 15:54:34 +01:00
print $ exampleData
print $ countUniqueAnswers [""]
print $ countUniqueAnswers ["abc"]
print $ countUniqueAnswers ["a", "b", "c"]
print $ countUniqueAnswers ["ab", "ac"]
putStrLn "Day 6 - Part 1 : solving test"
print $ solvePart1 exampleData
putStrLn "Day 6 - Part 1 : solving input"
input <- lines <$> readFile "day6/input"
print $ solvePart1 input
putStrLn "Day 6 - Part 2 : solving test"
2020-12-07 19:32:40 +01:00
print $ paragraphs exampleData
2020-12-07 15:54:34 +01:00
print $ solvePart2 exampleData
2020-12-07 19:32:40 +01:00
putStrLn "Day 6 - Part 2 : solving input"
print $ solvePart2 input
2020-12-07 15:54:34 +01:00