2018: d09: ex2: add solution

This commit is contained in:
Bruno BELANYI 2024-12-29 19:40:37 -05:00
parent c282de0062
commit e9a904a960

39
2018/d09/ex2/ex2.py Executable file
View file

@ -0,0 +1,39 @@
#!/usr/bin/env python
import collections
import sys
def solve(input: str) -> int:
def parse(input: str) -> tuple[int, int]:
split = input.split()
return int(split[0]), int(split[6])
def play_game(players: int, last_marble: int) -> list[int]:
circle = collections.deque([0])
scores = [0] * players
for marble in range(1, last_marble + 1):
if marble % 23 == 0:
scores[marble % players] += marble
circle.rotate(7)
scores[marble % players] += circle.pop()
circle.rotate(-1)
else:
circle.rotate(-1)
circle.append(marble)
return scores
players, last_marble = parse(input)
scores = play_game(players, last_marble * 100)
return max(scores)
def main() -> None:
input = sys.stdin.read()
print(solve(input))
if __name__ == "__main__":
main()