-
Notifications
You must be signed in to change notification settings - Fork 0
/
part_a.py
executable file
·111 lines (93 loc) · 1.88 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
#!/usr/bin/env python3
from aox.challenge import Debugger
from utils import BaseChallenge
class Challenge(BaseChallenge):
def solve(self, _input, debugger: Debugger):
"""
>>> Challenge().default_solve()
170
"""
return simulate_program(0)
def simulate_program(a: int) -> int:
b = 0
if a == 1:
value = get_constant_for_1()
elif a == 0:
value = get_constant_for_0()
else:
raise Exception(f"Cannot get constant for a='{a}'")
while value != 1:
b += 1
if value % 2 == 0:
value /= 2
else:
value = (value * 3) + 1
return b
def get_constant_for_0() -> int:
"""
>>> get_constant_for_0()
4591
"""
return get_constant_for_program("""
inc a
inc a
tpl a
tpl a
tpl a
inc a
inc a
tpl a
inc a
inc a
tpl a
tpl a
tpl a
inc a
""", a=0)
def get_constant_for_1() -> int:
"""
>>> get_constant_for_1()
113383
"""
return get_constant_for_program("""
tpl a
inc a
inc a
tpl a
inc a
inc a
tpl a
tpl a
inc a
inc a
tpl a
inc a
tpl a
inc a
tpl a
inc a
inc a
tpl a
inc a
tpl a
tpl a
inc a
""", a=1)
def get_constant_for_program(program_text: str, a: int) -> int:
data = {
"a": a,
}
def increase_a():
data["a"] += 1
def triple_a():
data["a"] *= 3
instruction_mapping = {
"inc a": increase_a,
"tpl a": triple_a,
}
lines = filter(None, map(str.strip, program_text.splitlines()))
for line in lines:
instruction_mapping[line]()
return data["a"]
Challenge.main()
challenge = Challenge()