2024-11-26 19:42:34 +01:00
|
|
|
#!/bin/python3
|
|
|
|
import sys,re
|
|
|
|
from pprint import pprint
|
|
|
|
sys.path.insert(0, '../../')
|
|
|
|
from fred import list2int
|
|
|
|
|
2024-11-27 16:45:03 +01:00
|
|
|
input_f = 'input'
|
2024-11-26 19:42:34 +01:00
|
|
|
|
|
|
|
part = 1
|
|
|
|
#########################################
|
|
|
|
# #
|
|
|
|
# Part 1 #
|
|
|
|
# #
|
|
|
|
#########################################
|
|
|
|
|
|
|
|
def parse_input(input_str):
|
2024-11-27 06:33:28 +01:00
|
|
|
pattern = r"^([a-zA-Z]{3})\s{1}(\w)?\s?(-?\w+)$"
|
2024-11-26 19:42:34 +01:00
|
|
|
|
|
|
|
match = re.match(pattern, input_str)
|
|
|
|
if match:
|
|
|
|
if match.group(2):
|
2024-11-27 06:33:28 +01:00
|
|
|
return match.group(1),match.group(2),match.group(3)
|
|
|
|
else:
|
|
|
|
return match.group(1),match.group(3)
|
|
|
|
|
2024-11-26 19:42:34 +01:00
|
|
|
return None
|
|
|
|
|
2024-11-27 16:45:03 +01:00
|
|
|
def sets_return(x):
|
|
|
|
if x in Sets:
|
|
|
|
value = Sets[x]
|
|
|
|
if isinstance(value, str) and not x.lstrip('-').isdigit():
|
|
|
|
return sets_return(value)
|
|
|
|
return value
|
|
|
|
elif x.isdigit() or x.lstrip('-').isdigit():
|
|
|
|
return int(x)
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
2024-11-27 06:33:28 +01:00
|
|
|
instructions = []
|
|
|
|
|
2024-11-27 16:45:03 +01:00
|
|
|
Sets = {}
|
2024-11-27 06:33:28 +01:00
|
|
|
|
|
|
|
last_sound = 0
|
|
|
|
|
2024-11-26 19:42:34 +01:00
|
|
|
if part == 1:
|
|
|
|
with open(input_f) as file:
|
|
|
|
for line in file:
|
2024-11-27 06:33:28 +01:00
|
|
|
instructions.append(list(parse_input(line.rstrip())))
|
|
|
|
|
|
|
|
x = 0
|
|
|
|
|
|
|
|
while True:
|
|
|
|
i = instructions[x]
|
2024-11-27 16:45:03 +01:00
|
|
|
|
|
|
|
if isinstance(i[1], str):
|
|
|
|
if i[1] not in Sets:
|
|
|
|
Sets[i[1]] = 0
|
2024-11-27 06:33:28 +01:00
|
|
|
|
|
|
|
if i[0] == 'set':
|
2024-11-27 16:45:03 +01:00
|
|
|
Sets[i[1]] = int(sets_return(i[2]))
|
2024-11-27 06:38:22 +01:00
|
|
|
x += 1
|
2024-11-27 06:33:28 +01:00
|
|
|
|
|
|
|
elif i[0] == 'add':
|
2024-11-27 16:45:03 +01:00
|
|
|
Sets[i[1]] += sets_return(i[2])
|
2024-11-27 06:38:22 +01:00
|
|
|
x += 1
|
2024-11-27 06:33:28 +01:00
|
|
|
|
|
|
|
elif i[0] == 'mul':
|
2024-11-27 16:45:03 +01:00
|
|
|
Sets[i[1]] *= sets_return(i[2])
|
2024-11-27 06:38:22 +01:00
|
|
|
x += 1
|
2024-11-27 06:33:28 +01:00
|
|
|
|
|
|
|
elif i[0] == 'mod':
|
2024-11-27 16:45:03 +01:00
|
|
|
Sets[i[1]] %= sets_return(i[2])
|
2024-11-27 06:38:22 +01:00
|
|
|
x += 1
|
2024-11-27 06:33:28 +01:00
|
|
|
|
|
|
|
elif i[0] == 'snd':
|
2024-11-27 16:45:03 +01:00
|
|
|
last_sound = sets_return(i[1])
|
2024-11-27 06:38:22 +01:00
|
|
|
x += 1
|
2024-11-27 06:33:28 +01:00
|
|
|
|
|
|
|
elif i[0] == 'rcv':
|
2024-11-27 16:45:03 +01:00
|
|
|
if sets_return(i[1]) != 0:
|
|
|
|
Sets[i[1]] = sets_return(i[1])
|
|
|
|
print(last_sound)
|
|
|
|
exit()
|
2024-11-27 06:38:22 +01:00
|
|
|
x += 1
|
|
|
|
|
2024-11-27 06:33:28 +01:00
|
|
|
|
2024-11-27 16:45:03 +01:00
|
|
|
elif i[0] == 'jgz':
|
|
|
|
if sets_return(i[1]) > 0:
|
|
|
|
x += sets_return(i[2])
|
2024-11-27 06:33:28 +01:00
|
|
|
else:
|
|
|
|
x += 1
|
2024-11-27 16:45:03 +01:00
|
|
|
|
2024-11-26 19:42:34 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#########################################
|
|
|
|
# #
|
|
|
|
# Part 2 #
|
|
|
|
# #
|
|
|
|
#########################################
|
|
|
|
if part == 2:
|
|
|
|
exit()
|