Is this acmeism?
This commit is contained in:
42
aoc2020/py/day06/solution.py
Normal file
42
aoc2020/py/day06/solution.py
Normal file
@@ -0,0 +1,42 @@
|
||||
import sys
|
||||
import typing
|
||||
|
||||
|
||||
def main() -> int:
|
||||
counts_sum = sum([c for c in _iter_group_counts(sys.stdin)])
|
||||
print(f"counts_sum={counts_sum}")
|
||||
|
||||
return 0
|
||||
|
||||
|
||||
def _iter_group_counts(instream: typing.TextIO) -> typing.Generator[int, None, None]:
|
||||
for i, group in enumerate(_iter_groups(instream)):
|
||||
answers = set(list(group[0]))
|
||||
print(f"i={i} initial={answers}")
|
||||
|
||||
for answers_text in group[1:]:
|
||||
to_add = set(list(answers_text))
|
||||
answers = answers.intersection(set(list(answers_text)))
|
||||
print(f"i={i} added={to_add} result={answers}")
|
||||
|
||||
print(f"i={i} final={answers} n={len(answers)}")
|
||||
yield len(answers)
|
||||
|
||||
|
||||
def _iter_groups(instream):
|
||||
cur_group = []
|
||||
|
||||
for line in instream:
|
||||
line = line.strip()
|
||||
if line == "":
|
||||
yield cur_group
|
||||
cur_group = []
|
||||
continue
|
||||
|
||||
cur_group.append(line)
|
||||
|
||||
yield cur_group
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
15
aoc2020/py/day06/test-input
Normal file
15
aoc2020/py/day06/test-input
Normal file
@@ -0,0 +1,15 @@
|
||||
abc
|
||||
|
||||
a
|
||||
b
|
||||
c
|
||||
|
||||
ab
|
||||
ac
|
||||
|
||||
a
|
||||
a
|
||||
a
|
||||
a
|
||||
|
||||
b
|
1
aoc2020/py/day06/test-output
Normal file
1
aoc2020/py/day06/test-output
Normal file
@@ -0,0 +1 @@
|
||||
counts_sum=6
|
21
aoc2020/py/day06/test_solution.py
Normal file
21
aoc2020/py/day06/test_solution.py
Normal file
@@ -0,0 +1,21 @@
|
||||
import sys
|
||||
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
from solution import main
|
||||
|
||||
|
||||
HERE = Path(__file__).absolute().parent
|
||||
|
||||
|
||||
def test_solution(capsys):
|
||||
with (HERE / "test-input").open() as infile:
|
||||
sys.stdin = infile
|
||||
main()
|
||||
|
||||
expected_output = (HERE / "test-output").read_text().splitlines()
|
||||
assert expected_output == [
|
||||
l for l in capsys.readouterr().out.splitlines() if l.startswith("counts_sum")
|
||||
]
|
Reference in New Issue
Block a user