This repository has been archived by the owner on May 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path25_clock_signal.py
121 lines (100 loc) · 4.97 KB
/
25_clock_signal.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
################################
# --- Day 25: Clock Signal --- #
################################
import AOCUtils
class VM:
def __init__(self, program):
self.program = program[:]
self.pc = 0
self.registers = {"a": 0, "b": 0, "c": 0, "d": 0}
self.lastOutput = 1
self.outputLength = 0
self.loops = False
def run(self):
while self.pc < len(self.program):
cmd = self.program[self.pc].split()
inst = cmd[0]
x = cmd[1]
xVal = int(x) if not x.isalpha() else self.registers[x]
if len(cmd) > 2:
y = cmd[2]
yVal = int(y) if not y.isalpha() else self.registers[y]
if inst == "cpy":
self.registers[y] = xVal
elif inst == "inc":
self.registers[x] += 1
elif inst == "dec":
self.registers[x] -= 1
elif inst == "jnz":
if xVal != 0:
self.pc += yVal - 1
elif inst == "out":
if xVal == self.lastOutput:
break
self.lastOutput = xVal
self.outputLength += 1
# Assume that the clock loops forever if it keeps oscillating after 100 cycles
if self.outputLength > 100:
self.loops = True
break
self.pc += 1
################################
program = AOCUtils.loadInput(25)
# i = 0
# while True:
# vm = VM(program)
# vm.registers["a"] = i
# vm.run()
# if vm.loops:
# print("Part 1: {}".format(i))
# break
# i += 1
X = int(program[1].split()[1])
Y = int(program[2].split()[1])
n = 1
while True:
repeat = int("10"*n, 2)
if repeat > X * Y: break
n += 1
a = repeat - X * Y
print("Part 1: {}".format(a))
AOCUtils.printTimeTaken()
# Part 1: Smallest a such that a + x*y = 0b10...10
"""
| | <<<<<<<<<<<<<<<<<<<<<<<<<<<<<< | while True: | loop forever:
0 | cpy a d | d = a ^ | d = a |
1 | cpy 7 c | c = 7 ^ | |
| | ^ | |
2 | cpy 362 b | b = 362 <<<<<<<<<<<<< ^ | d += 362 * 7 |
3 | inc d | d += 1 <<<<<<<<<<< ^ ^ | |
4 | dec b | b -= 1 ^ ^ ^ | |
5 | jnz b -2 | while b != 0: >>>> ^ ^ | |
6 | dec c | c -= 1 ^ ^ | |
7 | jnz c -5 | while c != 0: >>>>>>> ^ | |
| | ^ | |
8 | cpy d a | a = d ^ | a = d | a = a + 362 * 7
| | ^ | |
9 | jnz 0 0 | <<<<<<<<<<<<<<<<<<<<<<<<<<< ^ | while a != 0: | while a != 0:
10 | cpy a b | b = a ^ ^ | b = a |
11 | cpy 0 a | a = 0 ^ ^ | a = 0 |
12 | cpy 2 c | c = 2 <<<<<<<<<<<<<<< ^ ^ | c = 2 |
13 | jnz b 2 | if b != 0: >>>> << ^ ^ ^ | while b != 0 and c != 0: |
14 | jnz 1 6 | >>>>>>>>>>>>. v ^ ^ .v ^ ^ | |
15 | dec b | b -= 1 <<<<<<<< ^ ^ v ^ ^ | b -= 1 |
16 | dec c | c -= 1 ^ ^ v ^ ^ | c -= 1 |
17 | jnz c -4 | while c != 0: >>>> ^ v ^ ^ | |
18 | inc a | a += 1 ^ v ^ ^ | a += 1 | a = b // 2
19 | jnz 1 -7 | >>>>>>>>>>>>>>>>>>>>> v ^ ^ | | c = 2 - (a % 2)
20 | cpy 2 b | <<<<<<<<<<<<<<<<<<<<<<<< ^ ^ | |
| | ^ ^ | |
21 | jnz c 2 | if c != 0: >>>>>>>v << ^ ^ | while c != 0: | b = 2 - c = a % 2
22 | jnz 1 4 | >>>>>>>>>>>>>>>> v ^ ^ ^ | |
23 | dec b | b -= 1 <<<<<<. v .v ^ ^ ^ | b -= 1 |
24 | dec c | c -= 1 v ^ ^ ^ | c -= 1 |
25 | jnz 1 -4 | >>>>>>>>>>>>>. v .>>>> ^ ^ | |
26 | jnz 0 0 | <<<<<<<<<<<<<<<< ^ ^ | |
| | ^ ^ | |
27 | out b | out b ^ ^ | out b | print(b)
28 | jnz a -19 | if a != 0: >>>>>>>>>>>>>>>> ^ | |
29 | jnz 1 -21 | >>>>>>>>>>>>>>>>>>>>>>>>>>>>>> | |
"""