finished day 6

This commit is contained in:
gabe 2022-09-13 17:44:46 -05:00
parent 5b996a5019
commit 23b6ce30b4
5 changed files with 2317 additions and 1 deletions

View file

@ -20,7 +20,7 @@ def example2():
# @pytest.mark.skip(reason="Not implemented")
def test_parse_example1(example1):
"""Test that input is parsed properly"""
assert example1 == [(44,5), (70,7), (14,7), (102, 4)]
assert example1 == {119, 357, 567, 820}
# @pytest.mark.skip(reason="Not implemented")
def test_part1_example1(example1):

47
day6/day6.py Executable file
View file

@ -0,0 +1,47 @@
#! /usr/bin/env python3
import pathlib
import sys
import re
from dataclasses import dataclass
from pprint import pprint
def parse(puzzle_input: str):
"""Parse input"""
records = [[list(y) for y in x.splitlines()] for x in puzzle_input.split('\n\n')]
return records
def part1(data):
"""Solve part 1"""
results = []
for group in data:
result = set()
for person in group:
result |= set(person)
results.append(len(result))
return sum(results)
def part2(data):
"""Solve part 2"""
results = []
for group in data:
result = set(group[0])
for person in group:
result &= set(person)
results.append(len(result))
return sum(results)
def solve(puzzle_input):
"""Solve the puzzle for the given input"""
data = parse(puzzle_input)
solution1 = part1(data)
solution2 = part2(data)
return solution1, solution2
if __name__ == "__main__":
for path in sys.argv[1:]:
print(f"{path}:")
puzzle_input = pathlib.Path(path).read_text().strip()
solutions = solve(puzzle_input)
print("\n".join(str(solution) for solution in solutions))

15
day6/example1 Normal file
View file

@ -0,0 +1,15 @@
abc
a
b
c
ab
ac
a
a
a
a
b

2199
day6/input Normal file

File diff suppressed because it is too large Load diff

55
day6/test_day6.py Normal file
View file

@ -0,0 +1,55 @@
#! /usr/bin/env python3
import pathlib
import pytest
import day6 as aoc
PUZZLE_DIR = pathlib.Path(__file__).parent
#these test fixtures setup the test, mainly by reading the filename into a string in this simple case.
@pytest.fixture
def example1():
puzzle_input = (PUZZLE_DIR / "example1").read_text().strip()
return aoc.parse(puzzle_input)
@pytest.fixture
def example2():
puzzle_input = (PUZZLE_DIR / "example2").read_text().strip()
return aoc.parse(puzzle_input)
# @pytest.mark.skip(reason="Not implemented")
def test_parse_example1(example1):
"""Test that input is parsed properly"""
assert example1 == [
[
['a', 'b', 'c']
],
[
['a'],
['b'],
['c']
],
[
['a', 'b'],
['a', 'c']
],
[
['a'],
['a'],
['a'],
['a']
],
[
['b']
]
]
# @pytest.mark.skip(reason="Not implemented")
def test_part1_example1(example1):
"""Test part 1 on example input"""
assert aoc.part1(example1) == 11
# @pytest.mark.skip(reason="Not implemented")
def test_part2_example2(example1):
"""Test part 2 on example input"""
assert aoc.part2(example1) == 6