2024-12-11 12:55:40 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- encoding: utf-8; py-indent-offset: 4 -*-
|
|
|
|
|
|
|
|
# Author: Sebastian Mark
|
|
|
|
# CC-BY-SA (https://creativecommons.org/licenses/by-sa/4.0/deed.de)
|
|
|
|
|
|
|
|
# pylint: disable=missing-module-docstring,missing-function-docstring,consider-using-f-string
|
|
|
|
|
2024-12-12 21:05:14 +00:00
|
|
|
from collections import defaultdict
|
|
|
|
|
2024-12-11 12:55:40 +00:00
|
|
|
|
|
|
|
def readinput():
|
|
|
|
cards = {}
|
|
|
|
with open("input", "r", encoding="utf-8") as file:
|
|
|
|
i = 1
|
|
|
|
for line in file.readlines():
|
|
|
|
line = line.split(":")[1]
|
|
|
|
winners = line.strip("\n").split("|")[0].split()
|
|
|
|
mine = line.strip("\n").split("|")[1].split()
|
|
|
|
cards[i] = {"winners": winners, "mine": mine}
|
|
|
|
i += 1
|
|
|
|
return cards
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
cards = readinput()
|
|
|
|
|
|
|
|
# part 1
|
|
|
|
win_total = 0
|
|
|
|
for _, numbers in cards.items():
|
|
|
|
cardwin = len(set(numbers["winners"]) & set(numbers["mine"]))
|
|
|
|
win_total += int(2 ** (cardwin - 1))
|
|
|
|
print("Scratchpad worth %d points" % win_total)
|
|
|
|
|
2024-12-12 21:05:14 +00:00
|
|
|
# part 2
|
|
|
|
cardnumbers = {i: 1 for i in cards}
|
|
|
|
for cardnumber, count in cardnumbers.items():
|
|
|
|
for _ in range(count):
|
|
|
|
numbers = cards[cardnumber]
|
|
|
|
cardwins = len(set(numbers["winners"]) & set(numbers["mine"]))
|
|
|
|
for i in range(1, cardwins + 1):
|
|
|
|
cardnumbers[cardnumber + i] += 1
|
|
|
|
print("Scratchpads: %d" % sum(cardnumbers.values()))
|
|
|
|
|
2024-12-11 12:55:40 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|