AdventOfCode/2017/18/solution.py

196 lines
4.4 KiB
Python

#!/bin/python3
import sys,re
from pprint import pprint
sys.path.insert(0, '../../')
from fred import list2int
input_f = 'input'
part = 1
#########################################
# #
# Part 1 #
# #
#########################################
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(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
instructions = []
Sets = {}
last_sound = 0
if part == 1:
with open(input_f) as file:
for line in file:
instructions.append(list(parse_input(line.rstrip())))
x = 0
while True:
i = instructions[x]
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
#########################################
# #
# 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:
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()