Day 6 (Python)
This commit is contained in:
parent
e9cb627d25
commit
b81f7c7235
8 changed files with 2269 additions and 1 deletions
2
.github/README.md
vendored
2
.github/README.md
vendored
|
@ -10,7 +10,7 @@ Go solutions are near direct copies of the Python solutions and may be added a f
|
|||
|----|-------------------------|-------------------------|----|-------------------------|-------------------------|
|
||||
| 1 | ![Completed][check] | ![Completed][check] | 2 | ![Completed][check] | ![Completed][check] |
|
||||
| 3 | ![Completed][check] | ![Completed][check] | 4 | ![Completed][check] | ![Completed][check] |
|
||||
| 5 | ![Completed][check] | ![Completed][check] | 6 | | |
|
||||
| 5 | ![Completed][check] | ![Completed][check] | 6 | ![Completed][check] | ![Completed][check] |
|
||||
| 7 | | | 8 | | |
|
||||
| 9 | | | 10 | | |
|
||||
| 11 | | | 12 | | |
|
||||
|
|
113
06-customCustoms/README.md
Normal file
113
06-customCustoms/README.md
Normal file
|
@ -0,0 +1,113 @@
|
|||
# Day 6: Custom Customs
|
||||
|
||||
<details><summary>Challenge description</summary>
|
||||
|
||||
## Part One
|
||||
|
||||
As your flight approaches the regional airport where you'll switch to a much larger plane, customs declaration forms 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?
|
||||
|
||||
Your puzzle answer was `6310`.
|
||||
|
||||
## 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?
|
||||
|
||||
Your puzzle answer was `3193`.
|
||||
|
||||
</details>
|
||||
|
||||
<details><summary>Script output</summary>
|
||||
|
||||
```
|
||||
❯ python .\python\
|
||||
AoC 2020: day 6 - Custom Customs
|
||||
Python 3.8.5
|
||||
|
||||
Test cases
|
||||
1.1 pass
|
||||
2.1 pass
|
||||
|
||||
Answers
|
||||
Part 1: 6310
|
||||
Part 2: 3193
|
||||
```
|
||||
|
||||
</details>
|
19
06-customCustoms/info.json
Normal file
19
06-customCustoms/info.json
Normal file
|
@ -0,0 +1,19 @@
|
|||
{
|
||||
"year": "2020",
|
||||
"day": "6",
|
||||
"title": "Custom Customs",
|
||||
"testCases": {
|
||||
"one": [
|
||||
{
|
||||
"input": "abc\n\na\nb\nc\n\nab\nac\n\na\na\na\na\n\nb\n",
|
||||
"expected": 11
|
||||
}
|
||||
],
|
||||
"two": [
|
||||
{
|
||||
"input": "abc\n\na\nb\nc\n\nab\nac\n\na\na\na\na\n\nb\n",
|
||||
"expected": 6
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
2001
06-customCustoms/input.txt
Normal file
2001
06-customCustoms/input.txt
Normal file
File diff suppressed because it is too large
Load diff
64
06-customCustoms/python/__main__.py
Normal file
64
06-customCustoms/python/__main__.py
Normal file
|
@ -0,0 +1,64 @@
|
|||
import json
|
||||
import platform
|
||||
import sys
|
||||
|
||||
from rich import print
|
||||
|
||||
from partOne import partOne
|
||||
from partTwo import partTwo
|
||||
|
||||
def run_tests(test_cases):
|
||||
do_tests = True
|
||||
if len(test_cases) == 0:
|
||||
do_tests = False
|
||||
elif len(test_cases["one"]) == 0 and len(test_cases["two"]) == 0:
|
||||
do_tests = False
|
||||
|
||||
if not do_tests:
|
||||
print("Info: no test cases specified. Skipping tests\n")
|
||||
return
|
||||
|
||||
print("Test cases")
|
||||
|
||||
def rt(tcs, f, n):
|
||||
for i, tc in enumerate(tcs):
|
||||
print(f"{n}.{i+1} ", end="")
|
||||
expectedInt = tc["expected"]
|
||||
result = f(str(tc["input"]))
|
||||
if result == expectedInt:
|
||||
print("[green]pass[/green]")
|
||||
else:
|
||||
print(f"[red]fail[/red] (got {result}, expected {expectedInt})")
|
||||
|
||||
rt(test_cases["one"], partOne, 1)
|
||||
rt(test_cases["two"], partTwo, 2)
|
||||
|
||||
print()
|
||||
|
||||
if __name__ == "__main__":
|
||||
try:
|
||||
info = open("info.json").read()
|
||||
except FileNotFoundError:
|
||||
print("Error: could not open info.json")
|
||||
sys.exit(-1)
|
||||
|
||||
info = json.loads(info)
|
||||
|
||||
year = info["year"]
|
||||
day = info["day"]
|
||||
title = info["title"]
|
||||
|
||||
print(f"[yellow]AoC {year}[/yellow]: day {day} - {title}")
|
||||
print(f"Python {platform.python_version()}\n")
|
||||
|
||||
try:
|
||||
challenge_input = open("input.txt").read()
|
||||
except FileNotFoundError:
|
||||
print("Error: could not open input.txt")
|
||||
sys.exit(-1)
|
||||
|
||||
run_tests(info["testCases"])
|
||||
|
||||
print("Answers")
|
||||
print("Part 1:", partOne(challenge_input))
|
||||
print("Part 2:", partTwo(challenge_input))
|
4
06-customCustoms/python/common.py
Normal file
4
06-customCustoms/python/common.py
Normal file
|
@ -0,0 +1,4 @@
|
|||
from typing import List
|
||||
|
||||
def parse(instr:str) -> List[str]:
|
||||
return instr.strip().split("\n\n")
|
26
06-customCustoms/python/partOne.py
Normal file
26
06-customCustoms/python/partOne.py
Normal file
|
@ -0,0 +1,26 @@
|
|||
from common import *
|
||||
|
||||
class Group:
|
||||
questions: List[str]
|
||||
num_pax: int
|
||||
|
||||
def __init__(self, instr:str) -> None:
|
||||
individual_pax = instr.split("\n")
|
||||
self.num_pax = len(individual_pax)
|
||||
|
||||
self.questions = []
|
||||
|
||||
for pax in individual_pax:
|
||||
for char in pax:
|
||||
if char not in self.questions:
|
||||
self.questions.append(char)
|
||||
|
||||
def partOne(instr:str) -> int:
|
||||
groups = [Group(x) for x in parse(instr)]
|
||||
|
||||
question_total = 0
|
||||
|
||||
for group in groups:
|
||||
question_total += len(group.questions)
|
||||
|
||||
return question_total
|
41
06-customCustoms/python/partTwo.py
Normal file
41
06-customCustoms/python/partTwo.py
Normal file
|
@ -0,0 +1,41 @@
|
|||
from common import *
|
||||
from typing import Dict
|
||||
|
||||
def check_char(aq:Dict[int, List[str]], char:str) -> bool:
|
||||
is_in_all = True
|
||||
for key in aq:
|
||||
val = aq[key]
|
||||
if char not in val:
|
||||
is_in_all = False
|
||||
break
|
||||
return is_in_all
|
||||
|
||||
class Group:
|
||||
questions: List[str]
|
||||
num_pax: int
|
||||
|
||||
def __init__(self, instr:str) -> None:
|
||||
individual_pax = instr.split("\n")
|
||||
self.num_pax = len(individual_pax)
|
||||
self.questions = []
|
||||
|
||||
pax_questions = {}
|
||||
for i, pax in enumerate(individual_pax):
|
||||
pax_questions[i] = [char for char in pax]
|
||||
|
||||
for key in pax_questions:
|
||||
val = pax_questions[key]
|
||||
for char in val:
|
||||
if check_char(pax_questions, char):
|
||||
if char not in self.questions:
|
||||
self.questions.append(char)
|
||||
|
||||
def partTwo(instr:str) -> int:
|
||||
groups = [Group(x) for x in parse(instr)]
|
||||
|
||||
question_total = 0
|
||||
|
||||
for group in groups:
|
||||
question_total += len(group.questions)
|
||||
|
||||
return question_total
|
Loading…
Add table
Add a link
Reference in a new issue