Skip to content

Commit

Permalink
Solve Day 06 (#8)
Browse files Browse the repository at this point in the history
* Setup Day 06

* Solve part 1

* solve part 2

* update readme to include part 2

* Add trailing lines

* Update specs notes

* fix lint warning

* add tasks file

* Add hlint and prettierrc

* update readme
  • Loading branch information
manuphatak authored Dec 7, 2020
1 parent 734e54c commit d1cbbfe
Show file tree
Hide file tree
Showing 6 changed files with 2,411 additions and 0 deletions.
2 changes: 2 additions & 0 deletions AdventOfCode2020.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ library
Day03.Solution
Day04.Solution
Day05.Solution
Day06.Solution
Template.Solution
other-modules:
Paths_AdventOfCode2020
Expand Down Expand Up @@ -62,6 +63,7 @@ test-suite AdventOfCode2020-test
Day03.SolutionSpec
Day04.SolutionSpec
Day05.SolutionSpec
Day06.SolutionSpec
Template.SolutionSpec
Paths_AdventOfCode2020
hs-source-dirs:
Expand Down
92 changes: 92 additions & 0 deletions src/Day06/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
## Day 6: Custom Customs

As your flight approaches the regional airport where you'll switch to a much larger plane, [customs declaration forms][1] are distributed to the passengers.

The form asks a series of 26 yes-or-no questions marked `a` through `z` . All you need to do is identify the questions for which _anyone in your group_ answers "yes". Since your group is just you, this doesn't take very long.

However, the person sitting next to you seems to be experiencing a language barrier and asks if you can help. For each of the people in their group, you write down the questions for which they answer "yes", one per line. For example:

```
abcx
abcy
abcz
```

In this group, there are _`6`_ questions to which anyone answered "yes": `a` , `b` , `c` , `x` , `y` , and `z` . (Duplicate answers to the same question don't count extra; each question counts at most once.)

Another group asks for your help, then another, and eventually you've collected answers from every group on the plane (your puzzle input). Each group's answers are separated by a blank line, and within each group, each person's answers are on a single line. For example:

```
abc
a
b
c
ab
ac
a
a
a
a
b
```

This list represents answers from five groups:

- The first group contains one person who answered "yes" to _`3`_ questions: `a` , `b` , and `c` .
- The second group contains three people; combined, they answered "yes" to _`3`_ questions: `a` , `b` , and `c` .
- The third group contains two people; combined, they answered "yes" to _`3`_ questions: `a` , `b` , and `c` .
- The fourth group contains four people; combined, they answered "yes" to only _`1`_ question, `a` .
- The last group contains one person who answered "yes" to only _`1`_ question, `b` .

In this example, the sum of these counts is `3 + 3 + 3 + 1 + 1` \= _`11`_ .

For each group, count the number of questions to which anyone answered "yes". _What is the sum of those counts?_

## Part Two

As you finish the last group's customs declaration, you notice that you misread one word in the instructions:

You don't need to identify the questions to which _anyone_ answered "yes"; you need to identify the questions to which _everyone_ answered "yes"!

Using the same example as above:

```
abc
a
b
c
ab
ac
a
a
a
a
b
```

This list represents answers from five groups:

- In the first group, everyone (all 1 person) answered "yes" to _`3`_ questions: `a` , `b` , and `c` .
- In the second group, there is _no_ question to which everyone answered "yes".
- In the third group, everyone answered yes to only _`1`_ question, `a` . Since some people did not answer "yes" to `b` or `c` , they don't count.
- In the fourth group, everyone answered yes to only _`1`_ question, `a` .
- In the fifth group, everyone (all 1 person) answered "yes" to _`1`_ question, `b` .

In this example, the sum of these counts is `3 + 0 + 1 + 1 + 1` \= _`6`_ .

For each group, count the number of questions to which _everyone_ answered "yes". _What is the sum of those counts?_

## Link

[https://adventofcode.com/2020/day/6][2]

[1]: https://en.wikipedia.org/wiki/Customs_declaration
[2]: https://adventofcode.com/2020/day/6
34 changes: 34 additions & 0 deletions src/Day06/Solution.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
module Day06.Solution (part1, part2, parseGroups, anyYes, allYes, groupCounts) where

import Advent.Utils (rightToMaybe)
import Data.Maybe (fromJust)
import qualified Data.Set as Set
import Text.Parsec

part1 :: String -> String
part1 = show . sum . fromJust . groupCounts anyYes

part2 :: String -> String
part2 = show . sum . fromJust . groupCounts allYes

type Passenger = String

type Group = [Passenger]

parseGroups :: String -> Either ParseError [Group]
parseGroups = parse (groupParser `sepEndBy1` newline) ""

groupParser :: Parsec String () Group
groupParser = passengerParser `sepEndBy1` newline

passengerParser :: Parsec String () Passenger
passengerParser = many1 letter

anyYes :: Group -> String
anyYes = Set.toList . Set.unions . map Set.fromList

allYes :: Group -> String
allYes group = filter (\c -> all (elem c) group) $ anyYes group

groupCounts :: (Group -> String) -> String -> Maybe [Int]
groupCounts combineGroup = fmap (map (length . combineGroup)) . rightToMaybe . parseGroups
57 changes: 57 additions & 0 deletions test/Day06/SolutionSpec.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
module Day06.SolutionSpec (spec) where

import Data.Foldable (for_)
import Day06.Solution (allYes, anyYes, groupCounts, parseGroups, part1, part2)
import Test.Hspec

spec :: Spec
spec = parallel $ do
it "solves Part 1" $ do
input <- readFile "./test/Day06/input.txt"
part1 input `shouldBe` "6735"
it "solves Part 2" $ do
input <- readFile "./test/Day06/input.txt"
part2 input `shouldBe` "3221"

describe "parseGroups" $ do
it "parses the example into groups of passenger" $ do
input <- readFile "./test/Day06/example.txt"
parseGroups input
`shouldBe` Right
[ ["abc"],
["a", "b", "c"],
["ab", "ac"],
["a", "a", "a", "a"],
["b"]
]
describe "anyYes" $ do
let cases =
[ (["abc"], "abc"),
(["a", "b", "c"], "abc"),
(["ab", "ac"], "abc"),
(["a", "a", "a", "a"], "a"),
(["b"], "b")
]
test (input, expected) = it ("finds '" ++ expected ++ "' are answered 'yes' for ANY passengers within " ++ show input) $ do
anyYes input `shouldBe` expected
in for_ cases test
describe "allYes" $ do
let cases =
[ (["abc"], "abc"),
(["a", "b", "c"], ""),
(["ab", "ac"], "a"),
(["a", "a", "a", "a"], "a"),
(["b"], "b")
]
test (input, expected) = it ("finds '" ++ expected ++ "' are answered 'yes' for ALL passengers within " ++ show input) $ do
allYes input `shouldBe` expected
in for_ cases test
describe "groupCounts" $ do
context "when counting ANY 'yes's within a group" $ do
it "finds the count for each group" $ do
input <- readFile "./test/Day06/example.txt"
groupCounts anyYes input `shouldBe` Just [3, 3, 3, 1, 1]
context "when counting ALL 'yes's within a group" $ do
it "finds the count for each group" $ do
input <- readFile "./test/Day06/example.txt"
groupCounts allYes input `shouldBe` Just [3, 0, 1, 1, 1]
15 changes: 15 additions & 0 deletions test/Day06/example.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
abc

a
b
c

ab
ac

a
a
a
a

b
Loading

0 comments on commit d1cbbfe

Please sign in to comment.