-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Lance-Drane <ldraneutk@gmail.com>
- Loading branch information
1 parent
6b4ce00
commit 4467dc8
Showing
9 changed files
with
262 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import sys | ||
from collections import defaultdict | ||
|
||
registers = defaultdict(int) | ||
instructions: list[list[str, str, str]] = [line.split() for line in sys.stdin.readlines()] | ||
last_sound = 0 | ||
|
||
|
||
def get_next_value(val: str) -> int: | ||
return int(val) if not val[0].isalpha() else registers[val] | ||
|
||
|
||
curr = 0 | ||
while curr < len(instructions): | ||
instruct = instructions[curr] | ||
|
||
if instruct[0] == 'snd': | ||
last_sound = get_next_value(instruct[1]) | ||
curr += 1 | ||
elif instruct[0] == 'set': | ||
registers[instruct[1]] = get_next_value(instruct[2]) | ||
curr += 1 | ||
elif instruct[0] == 'add': | ||
registers[instruct[1]] += get_next_value(instruct[2]) | ||
curr += 1 | ||
elif instruct[0] == 'mul': | ||
registers[instruct[1]] *= get_next_value(instruct[2]) | ||
curr += 1 | ||
elif instruct[0] == 'mod': | ||
registers[instruct[1]] %= get_next_value(instruct[2]) | ||
curr += 1 | ||
elif instruct[0] == 'rcv': | ||
next_val = get_next_value(instruct[1]) | ||
if next_val != 0: | ||
print(last_sound) | ||
break | ||
curr += 1 | ||
# jgz | ||
elif get_next_value(instruct[1]) > 0: | ||
curr += get_next_value(instruct[2]) | ||
else: | ||
curr += 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
9423 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import sys | ||
from collections import defaultdict, deque | ||
|
||
instructions: list[list[str, str, str]] = [line.split() for line in sys.stdin.readlines()] | ||
|
||
|
||
class Program: | ||
def __init__(self, pid: int): | ||
self.registers = defaultdict(int, {'p': pid}) | ||
self.curr = 0 | ||
self.msg_q = deque() | ||
self.next_receive: str | None = None | ||
self.sent_count = 0 | ||
|
||
def get_next_value(self, val: str) -> int: | ||
return int(val) if not val[0].isalpha() else self.registers[val] | ||
|
||
def register_message_endpoint(self, callback): | ||
""" | ||
allow for one program to send a message to another program | ||
""" | ||
self.message_endpoint_other = callback | ||
|
||
def message_endpoint(self, value: int): | ||
""" | ||
called from the other program when it sends a value, just add the value to the message queue | ||
for now. | ||
""" | ||
self.msg_q.append(value) | ||
|
||
def register_execute(self, callback): | ||
""" | ||
allow for one program to tell the other program to start executing its instructions | ||
""" | ||
self.execute_other = callback | ||
|
||
def execute(self): | ||
""" | ||
start executing instructions. call the other program's execution function by | ||
"self.execute_other()" | ||
""" | ||
if not self.msg_q: | ||
# deadlock | ||
return | ||
if self.next_receive: | ||
self.registers[self.next_receive] = self.msg_q.popleft() | ||
if not self.msg_q: | ||
# deadlock | ||
return | ||
self.next_receive = None | ||
self.execute_instructions() | ||
|
||
def execute_instructions(self): | ||
""" | ||
this is the main loop. normally called from "execute", but the initial back-and-forth | ||
needs to start from here. | ||
""" | ||
while self.curr < len(instructions): | ||
instruct = instructions[self.curr] | ||
|
||
if instruct[0] == 'snd': | ||
self.curr += 1 | ||
self.message_endpoint_other(self.get_next_value(instruct[1])) | ||
self.sent_count += 1 | ||
elif instruct[0] == 'set': | ||
self.registers[instruct[1]] = self.get_next_value(instruct[2]) | ||
self.curr += 1 | ||
elif instruct[0] == 'add': | ||
self.registers[instruct[1]] += self.get_next_value(instruct[2]) | ||
self.curr += 1 | ||
elif instruct[0] == 'mul': | ||
self.registers[instruct[1]] *= self.get_next_value(instruct[2]) | ||
self.curr += 1 | ||
elif instruct[0] == 'mod': | ||
self.registers[instruct[1]] %= self.get_next_value(instruct[2]) | ||
self.curr += 1 | ||
elif instruct[0] == 'rcv': | ||
self.curr += 1 | ||
if self.msg_q: | ||
self.registers[instruct[1]] = self.msg_q.popleft() | ||
else: | ||
self.next_receive = instruct[1] | ||
break | ||
# jgz | ||
elif self.get_next_value(instruct[1]) > 0: | ||
self.curr += self.get_next_value(instruct[2]) | ||
else: | ||
self.curr += 1 | ||
|
||
self.execute_other() | ||
|
||
|
||
program_0 = Program(0) | ||
program_1 = Program(1) | ||
|
||
program_0.register_execute(program_1.execute) | ||
program_1.register_execute(program_0.execute) | ||
program_0.register_message_endpoint(program_1.message_endpoint) | ||
program_1.register_message_endpoint(program_0.message_endpoint) | ||
|
||
program_0.execute_instructions() | ||
|
||
print(program_1.sent_count) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
7620 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
set i 31 | ||
set a 1 | ||
mul p 17 | ||
jgz p p | ||
mul a 2 | ||
add i -1 | ||
jgz i -2 | ||
add a -1 | ||
set i 127 | ||
set p 622 | ||
mul p 8505 | ||
mod p a | ||
mul p 129749 | ||
add p 12345 | ||
mod p a | ||
set b p | ||
mod b 10000 | ||
snd b | ||
add i -1 | ||
jgz i -9 | ||
jgz a 3 | ||
rcv b | ||
jgz b -1 | ||
set f 0 | ||
set i 126 | ||
rcv a | ||
rcv b | ||
set p a | ||
mul p -1 | ||
add p b | ||
jgz p 4 | ||
snd a | ||
set a b | ||
jgz 1 3 | ||
snd b | ||
set f 1 | ||
add i -1 | ||
jgz i -11 | ||
snd a | ||
jgz f -16 | ||
jgz a -19 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,6 @@ extend-select = [ | |
'C90', | ||
'I', | ||
'UP', | ||
'FBT', | ||
'B', | ||
'A', | ||
'COM', | ||
|