-
Notifications
You must be signed in to change notification settings - Fork 0
/
part_a.py
executable file
·145 lines (118 loc) · 4.05 KB
/
part_a.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/usr/bin/env python3
from dataclasses import dataclass
from itertools import count
from typing import Dict, Optional, Sized
from aox.challenge import Debugger
from utils import BaseChallenge
class Challenge(BaseChallenge):
def solve(self, _input, debugger: Debugger):
"""
>>> Challenge().default_solve()
1842613
"""
return NextGame.from_length_text(_input).solve(debugger=debugger)
class BaseGame:
position: int
@classmethod
def from_length_text(cls, length_text: str):
return cls.from_length(int(length_text))
@classmethod
def from_length(cls, length: int):
raise NotImplementedError()
def solve(self, debugger: Debugger = Debugger(enabled=False)) -> int:
return self.step_many(debugger=debugger).get_solution()
def get_solution(self) -> int:
if not self.has_finished():
raise Exception(f"Can't get solution before finishing")
winner, = self.get_remaining_positions()
return winner
def step_many(self, step_count: Optional[int] = None,
debugger: Debugger = Debugger(enabled=False)):
if step_count is None:
steps = count()
else:
steps = range(step_count)
debugger.reset()
for _ in debugger.stepping(steps):
self.step()
if self.has_finished():
break
if debugger.should_report():
debugger.default_report(
f"remaining: {len(self.get_remaining_positions())}, "
f"position: {self.position}")
return self
def step(self):
raise NotImplementedError()
def has_finished(self) -> bool:
return len(self.get_remaining_positions()) <= 1
def get_remaining_positions(self) -> Sized:
raise NotImplementedError()
@dataclass
class NextGame(BaseGame):
"""
>>> NextGame.from_length(5).solve()
3
>>> NextGame({1: 1}).solve()
1
>>> NextGame({1: 1}).get_solution()
1
"""
next_map: Dict[int, int]
position: int = 1
@classmethod
def from_length(cls, length: int):
"""
>>> NextGame.from_length(5)
NextGame(next_map={1: 2, 2: 3, 3: 4, 4: 5, 5: 1}, position=1)
"""
return cls({
position: cls.get_next_position(position, length)
for position in range(1, length + 1)
})
@classmethod
def get_next_position(cls, position: int, length: int) -> int:
"""
>>> # noinspection PyUnresolvedReferences
>>> [NextGame.get_next_position(p, 5) for p in range(1, 5 + 1)]
[2, 3, 4, 5, 1]
>>> # noinspection PyUnresolvedReferences
>>> [NextGame.get_next_position(p, 1) for p in range(1, 1 + 1)]
[1]
"""
return (position % length) + 1
def step(self):
"""
>>> NextGame.from_length(5).step()
NextGame(next_map={1: 3, 3: 4, 4: 5, 5: 1}, position=3)
>>> NextGame.from_length(1).step()
NextGame(next_map={1: 1}, position=1)
>>> NextGame({1: 1, 2: 1}).step()
Traceback (most recent call last):
...
Exception: Haven't finished, but current position pointed to itself: 1
"""
if self.has_finished():
return self
next_position = self.next_map[self.position]
if next_position == self.position:
raise Exception(
f"Haven't finished, but current position pointed to itself: "
f"{self.position}")
next_next_position = self.next_map[next_position]
self.next_map[self.position] = next_next_position
del self.next_map[next_position]
self.position = next_next_position
return self
def get_remaining_positions(self) -> Sized:
"""
>>> NextGame.from_length(5).has_finished()
False
>>> NextGame.from_length(5).step_many().has_finished()
True
>>> NextGame({1: 1}).has_finished()
True
"""
return self.next_map
Challenge.main()
challenge = Challenge()