Finished 2017/18
This commit is contained in:
+8
-4
@@ -76,8 +76,6 @@ with a non-zero value?
|
||||
|
||||
Your puzzle answer was `8600`.
|
||||
|
||||
The first half of this puzzle is complete! It provides one gold star: \*
|
||||
|
||||
## \-\-- Part Two \-\-- {#part2}
|
||||
|
||||
As you congratulate yourself for a job well done, you notice that the
|
||||
@@ -134,7 +132,13 @@ executed even its first instruction.
|
||||
Once both of your programs have terminated (regardless of what caused
|
||||
them to do so), *how many times did program `1` send a value*?
|
||||
|
||||
Answer:
|
||||
Your puzzle answer was `7239`.
|
||||
|
||||
Although it hasn\'t changed, you can still [get your puzzle
|
||||
Both parts of this puzzle are complete! They provide two gold stars:
|
||||
\*\*
|
||||
|
||||
At this point, you should [return to your Advent calendar](/2017) and
|
||||
try another puzzle.
|
||||
|
||||
If you still want to see it, you can [get your puzzle
|
||||
input](18/input).
|
||||
|
||||
+94
-2
@@ -96,7 +96,99 @@ if part == 1:
|
||||
#########################################
|
||||
# #
|
||||
# Part 2 #
|
||||
# #
|
||||
# in seperate file #
|
||||
#########################################
|
||||
def parse_input(input_str):
|
||||
pattern = r"^([a-zA-Z]{3})\s{1}(\w)?\s?(-?\w+)$"
|
||||
|
||||
match = re.match(pattern, input_str)
|
||||
if match:
|
||||
if match.group(2):
|
||||
return match.group(1),match.group(2),match.group(3)
|
||||
else:
|
||||
return match.group(1),match.group(3)
|
||||
return None
|
||||
|
||||
def sets_1_return(x):
|
||||
if x in Sets_1:
|
||||
value = Sets_1[x]
|
||||
if isinstance(value, str) and not x.lstrip('-').isdigit():
|
||||
return sets_1_return(value)
|
||||
return value
|
||||
elif x.isdigit() or x.lstrip('-').isdigit():
|
||||
return int(x)
|
||||
else:
|
||||
return None
|
||||
|
||||
def sets_0_return(x):
|
||||
if x in Sets_0:
|
||||
value = Sets_0[x]
|
||||
if isinstance(value, str) and not x.lstrip('-').isdigit():
|
||||
return sets_0_return(value)
|
||||
return value
|
||||
elif x.isdigit() or x.lstrip('-').isdigit():
|
||||
return int(x)
|
||||
else:
|
||||
return None
|
||||
|
||||
instructions = []
|
||||
|
||||
Sets_1 = {}
|
||||
Sets_0 = {}
|
||||
|
||||
p_1 = []
|
||||
p_0 = []
|
||||
|
||||
last_sound = 0
|
||||
|
||||
if part == 2:
|
||||
exit()
|
||||
with open(input_f) as file:
|
||||
for line in file:
|
||||
instructions.append(list(parse_input(line.rstrip())))
|
||||
|
||||
x = 0
|
||||
|
||||
while True:
|
||||
i = instructions[x]
|
||||
print(i)
|
||||
if isinstance(i[1], str):
|
||||
if i[1] not in Sets:
|
||||
Sets[i[1]] = 0
|
||||
|
||||
if i[0] == 'set':
|
||||
Sets[i[1]] = int(sets_return(i[2]))
|
||||
x += 1
|
||||
|
||||
elif i[0] == 'add':
|
||||
Sets[i[1]] += sets_return(i[2])
|
||||
x += 1
|
||||
|
||||
elif i[0] == 'mul':
|
||||
Sets[i[1]] *= sets_return(i[2])
|
||||
x += 1
|
||||
|
||||
elif i[0] == 'mod':
|
||||
Sets[i[1]] %= sets_return(i[2])
|
||||
x += 1
|
||||
|
||||
elif i[0] == 'snd':
|
||||
last_sound = sets_return(i[1])
|
||||
x += 1
|
||||
|
||||
elif i[0] == 'rcv':
|
||||
if sets_return(i[1]) != 0:
|
||||
Sets[i[1]] = sets_return(i[1])
|
||||
print(last_sound)
|
||||
exit()
|
||||
x += 1
|
||||
|
||||
|
||||
elif i[0] == 'jgz':
|
||||
if sets_return(i[1]) > 0:
|
||||
x += sets_return(i[2])
|
||||
else:
|
||||
x += 1
|
||||
|
||||
input()
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,138 @@
|
||||
import sys,re,asyncio
|
||||
|
||||
input_f = 'input'
|
||||
|
||||
def parse_input(input_str):
|
||||
pattern = r"^([a-zA-Z]{3})\s{1}(\w)?\s?(-?\w+)$"
|
||||
match = re.match(pattern, input_str)
|
||||
if match:
|
||||
if match.group(2):
|
||||
return match.group(1),match.group(2),match.group(3)
|
||||
else:
|
||||
return match.group(1),match.group(3)
|
||||
return None
|
||||
|
||||
def sets_return(value, sets):
|
||||
if value in sets:
|
||||
result = sets[value]
|
||||
if isinstance(result, str) and not value.lstrip('-').isdigit():
|
||||
return sets_return(result, sets)
|
||||
return result
|
||||
elif value.isdigit() or value.lstrip('-').isdigit():
|
||||
return int(value)
|
||||
return None
|
||||
|
||||
async def loop0(instructions,p_0, p_1,sets_0,state):
|
||||
x = 0
|
||||
while 0 <= x < len(instructions):
|
||||
i = instructions[x]
|
||||
if i[0] == 'set':
|
||||
sets_0[i[1]] = sets_return(i[2],sets_0)
|
||||
x += 1
|
||||
elif i[0] == 'add':
|
||||
sets_0[i[1]] += sets_return(i[2],sets_0)
|
||||
x += 1
|
||||
elif i[0] == 'mul':
|
||||
sets_0[i[1]] *= sets_return(i[2],sets_0)
|
||||
x += 1
|
||||
elif i[0] == 'mod':
|
||||
sets_0[i[1]] %= sets_return(i[2],sets_0)
|
||||
x += 1
|
||||
elif i[0] == 'snd':
|
||||
await p_1.put(sets_return(i[1],sets_0))
|
||||
#print('P0 send', sets_0_return(i[1]),sets_0)#,'p_1',p_1)
|
||||
x += 1
|
||||
elif i[0] == 'rcv':
|
||||
if p_0.empty():
|
||||
state["loop0_waiting"] = True
|
||||
if state["loop1_waiting"]: # Both loops are stuck
|
||||
print(f"Program exiting. Loop1 'snd' count: {state['count']}")
|
||||
return
|
||||
await asyncio.sleep(0.1)
|
||||
else:
|
||||
state["loop0_waiting"] = False
|
||||
sets_0[i[1]] = await p_0.get()
|
||||
#print('P0 recv', sets_0[i[1]],sets_0)#,'p_0',p_0)
|
||||
x += 1
|
||||
elif i[0] == 'jgz':
|
||||
if sets_return(i[1],sets_0) > 0:
|
||||
x += sets_return(i[2],sets_0)
|
||||
else:
|
||||
x += 1
|
||||
await asyncio.sleep(0.01)
|
||||
|
||||
async def loop1(instructions,p_0, p_1,sets_1,state):
|
||||
x = 0
|
||||
while 0 <= x < len(instructions):
|
||||
i = instructions[x]
|
||||
if i[0] == 'set':
|
||||
sets_1[i[1]] = sets_return(i[2],sets_1)
|
||||
x += 1
|
||||
elif i[0] == 'add':
|
||||
sets_1[i[1]] += sets_return(i[2],sets_1)
|
||||
x += 1
|
||||
elif i[0] == 'mul':
|
||||
sets_1[i[1]] *= sets_return(i[2],sets_1)
|
||||
x += 1
|
||||
elif i[0] == 'mod':
|
||||
sets_1[i[1]] %= sets_return(i[2],sets_1)
|
||||
x += 1
|
||||
elif i[0] == 'snd':
|
||||
await p_0.put(sets_return(i[1],sets_1))
|
||||
state["count"] += 1
|
||||
print(state["count"])
|
||||
x += 1
|
||||
elif i[0] == 'rcv':
|
||||
if p_1.empty():
|
||||
state["loop1_waiting"] = True
|
||||
if state["loop0_waiting"]: # Both loops are stuck
|
||||
print(f"Program exiting. Loop1 'snd' count: {state['count']}")
|
||||
return
|
||||
await asyncio.sleep(0.1)
|
||||
|
||||
else:
|
||||
state["loop1_waiting"] = False
|
||||
sets_1[i[1]] = await p_1.get()
|
||||
#print('P1 recv', sets_1[i[1]],sets_1) #,'p_1',p_1)
|
||||
#condition.notify_all()
|
||||
x += 1
|
||||
|
||||
|
||||
|
||||
|
||||
elif i[0] == 'jgz':
|
||||
if sets_return(i[1],sets_1) > 0:
|
||||
x += sets_return(i[2],sets_1)
|
||||
else:
|
||||
x += 1
|
||||
await asyncio.sleep(0.01)
|
||||
|
||||
async def main(instructions):
|
||||
p_0 = asyncio.Queue()
|
||||
p_1 = asyncio.Queue()
|
||||
sets_0 = {'p': 0}
|
||||
sets_1 = {'p': 1}
|
||||
state = {
|
||||
"loop0_waiting": False,
|
||||
"loop1_waiting": False,
|
||||
"count": 0
|
||||
}
|
||||
# Create tasks for the two loops
|
||||
#task1 = asyncio.create_task(loop0(instructions[:],p_0,p_1))
|
||||
#task2 = asyncio.create_task(loop1(count,instructions[:],p_0,p_1))
|
||||
|
||||
|
||||
|
||||
await asyncio.gather(
|
||||
loop0(instructions, p_0, p_1, sets_0, state),
|
||||
loop1(instructions, p_0, p_1, sets_1, state)
|
||||
)
|
||||
|
||||
instructions = []
|
||||
|
||||
with open(input_f) as file:
|
||||
for line in file:
|
||||
instructions.append(list(parse_input(line.rstrip())))
|
||||
|
||||
|
||||
asyncio.run(main(instructions))
|
||||
@@ -0,0 +1,7 @@
|
||||
snd 1
|
||||
snd 2
|
||||
snd p
|
||||
rcv a
|
||||
rcv b
|
||||
rcv c
|
||||
rcv d
|
||||
@@ -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 735
|
||||
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
|
||||
Reference in New Issue
Block a user