parent
eaebcb736a
commit
df072761d9
@ -0,0 +1,36 @@
|
|||||||
|
lines = open('inputs/day4.txt').readlines()
|
||||||
|
|
||||||
|
def ticket_numbers(line: str) -> tuple:
|
||||||
|
line = line.split("|")
|
||||||
|
winning = [int(x) for x in line[0].replace(" ", " ").split(":")[1].strip().split(" ")]
|
||||||
|
ticket = [int(x) for x in line[1].replace(" ", " ").strip().split(" ")]
|
||||||
|
return winning, ticket
|
||||||
|
|
||||||
|
def wins2score(wins: list[int]) -> int:
|
||||||
|
total = 0
|
||||||
|
for win in wins:
|
||||||
|
if win > 0:
|
||||||
|
total += 2 ** (win-1)
|
||||||
|
return total
|
||||||
|
|
||||||
|
def count_wins(lines: list[str]) -> int:
|
||||||
|
scores = []
|
||||||
|
for line in lines:
|
||||||
|
winning, ticket = ticket_numbers(line)
|
||||||
|
wins = 0
|
||||||
|
for nr in winning:
|
||||||
|
if nr in ticket:
|
||||||
|
wins += 1
|
||||||
|
scores.append(wins)
|
||||||
|
return scores
|
||||||
|
|
||||||
|
def count_total_cards(wins: list[int]) -> int:
|
||||||
|
copies = [1 for x in wins]
|
||||||
|
for i in range(len(wins)):
|
||||||
|
win = wins[i]
|
||||||
|
for w in range(win):
|
||||||
|
copies[i+w+1] += copies[i]
|
||||||
|
return copies
|
||||||
|
|
||||||
|
print(f'Solution Part 1: {wins2score(count_wins(lines))}')
|
||||||
|
print(f'Solution Part 2: {sum(count_total_cards(count_wins(lines)))}')
|
Loading…
Reference in new issue