This repository was archived by the owner on May 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpart1.py
66 lines (47 loc) · 1.39 KB
/
part1.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
import argparse
import collections
import os.path
import pytest
from support import timing
INPUT_TXT = os.path.join(os.path.dirname(__file__), 'input.txt')
def compute(s: str) -> int:
cups = collections.deque(int(n) for n in list(s.strip()))
maximum = max(cups)
for _ in range(100):
current = cups[0]
cups.rotate(-1)
taken = [cups.popleft() for _ in range(3)]
current_label = (current - 1)
if current_label == 0:
current_label = maximum
while current_label in taken:
current_label = (current_label - 1)
if current_label == 0:
current_label = maximum
idx = cups.index(current_label)
cups.rotate(-1 * idx - 1)
cups.extend(taken)
cups.rotate(idx + 3 + 1)
cups.rotate(-cups.index(1))
cups.popleft()
return int(''.join(str(n) for n in cups))
INPUT_S = '''\
389125467
'''
@pytest.mark.parametrize(
('input_s', 'expected'),
(
(INPUT_S, 67384529),
),
)
def test(input_s: str, expected: int) -> None:
assert compute(input_s) == expected
def main() -> int:
parser = argparse.ArgumentParser()
parser.add_argument('data_file', nargs='?', default=INPUT_TXT)
args = parser.parse_args()
with open(args.data_file) as f, timing():
print(compute(f.read()))
return 0
if __name__ == '__main__':
exit(main())