-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
1 parent
734e54c
commit d1cbbfe
Showing
6 changed files
with
2,411 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.