2023/day04: Complete!

main
earnest ma 2023-12-06 17:40:24 -05:00
parent 9304a38628
commit f3359f2d72
Signed by: earnest ma
GPG Key ID: A343F43342EB6E2A
2 changed files with 69 additions and 0 deletions

58
2023/day04.py Normal file
View File

@ -0,0 +1,58 @@
import re
nums: list[list[int]] = []
wins: list[list[int]] = []
copies: dict[int, int] = {}
def part1() -> int:
points: int = 0
for n in range(len(nums)):
x = list(filter(lambda y: y in wins[n], nums[n]))
if len(x) != 0:
points += 2 ** (len(x) - 1)
return points
def part2(c: int) -> int:
# base case
if len(nums) == c:
return sum(copies.values())
else:
if c not in copies:
copies[c] = 1
else:
copies[c] += 1
x = list(filter(lambda y: y in wins[c], nums[c]))
if len(x) != 0:
for y in range(1, len(x) + 1):
if c + y not in copies:
copies[c + y] = 1 * copies[c]
else:
copies[c + y] += 1 * copies[c]
return part2(c + 1)
def proc() -> None:
with open("day04.txt") as f:
for line in f:
line = line.strip()
n = line.split(": ")[1].split(" | ")[0]
w = line.split(": ")[1].split(" | ")[1]
nums.append(re.findall(r"\d+", n))
wins.append(re.findall(r"\d+", w))
if __name__ == "__main__":
proc()
print(part1())
print(part2(0))

11
2023/day04_test.py Normal file
View File

@ -0,0 +1,11 @@
from day04 import *
proc()
def test_part1() -> None:
assert part1() == 24542
def test_part2() -> None:
assert part2(0) == 8736438