generated from public/aoc_template
64 lines
1.6 KiB
Python
64 lines
1.6 KiB
Python
from tools.aoc import AOCDay
|
|
from typing import Any
|
|
|
|
|
|
class Day(AOCDay):
|
|
inputs = [
|
|
[
|
|
(13, "input4_test"),
|
|
(24706, "input4_dennis"),
|
|
(25174, "input4_martin"),
|
|
(20829, "input4"),
|
|
],
|
|
[
|
|
(30, "input4_test"),
|
|
(13114317, "input4_dennis"),
|
|
(6420979, "input4_martin"),
|
|
(12648035, "input4"),
|
|
],
|
|
]
|
|
|
|
def parse_input(self) -> list:
|
|
ret = []
|
|
for line in self.getInput():
|
|
_, card = line.split(": ")
|
|
winner_string, card_string = card.split(" | ")
|
|
winner_numbers = list(map(int, winner_string.split()))
|
|
card_numbers = list(map(int, card_string.split()))
|
|
ret.append(len(set(winner_numbers) & set(card_numbers)))
|
|
|
|
return ret
|
|
|
|
def part1(self) -> Any:
|
|
score = 0
|
|
winners = self.parse_input()
|
|
for hits in winners:
|
|
if hits:
|
|
score += 2 ** (hits - 1)
|
|
|
|
return score
|
|
|
|
def part2(self) -> Any:
|
|
card_count = [1]
|
|
all_card_count = 0
|
|
winners = self.parse_input()
|
|
for hits in winners:
|
|
if len(card_count) == 0:
|
|
count = 1
|
|
else:
|
|
count = card_count.pop(0)
|
|
all_card_count += count
|
|
|
|
for i in range(hits):
|
|
try:
|
|
card_count[i] += count
|
|
except IndexError:
|
|
card_count.append(count + 1)
|
|
|
|
return all_card_count
|
|
|
|
|
|
if __name__ == "__main__":
|
|
day = Day(2023, 4)
|
|
day.run(verbose=True)
|