AdventOfCode/2017/18/solution.py

196 lines
4.4 KiB
Python
Raw Normal View History

2024-11-29 20:44:25 +01:00
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 #
2024-11-27 23:20:51 +01:00
# in seperate file #
2024-11-26 19:42:34 +01:00
#########################################
2024-11-27 23:20:51 +01:00
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
2024-11-26 19:42:34 +01:00
if part == 2:
2024-11-27 23:20:51 +01:00
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()