83 lines
1.9 KiB
Python
83 lines
1.9 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
rules = {}
|
|
data = []
|
|
|
|
# parse input
|
|
is_rule = True
|
|
input_file = open("input", "r")
|
|
|
|
for line in input_file:
|
|
line = line.strip()
|
|
if line == "":
|
|
is_rule = False
|
|
continue
|
|
if is_rule:
|
|
line = line.split("{")
|
|
ru = line[1].replace("}", "").split(",")
|
|
rules[line[0]] = ru
|
|
else:
|
|
da = {}
|
|
line = line.replace("{", "").replace("}", "").split(",")
|
|
for ze in line:
|
|
ze = ze.split("=")
|
|
if ze[0] == "x":
|
|
da["x"] = int(ze[1])
|
|
elif ze[0] == "m":
|
|
da["m"] = int(ze[1])
|
|
elif ze[0] == "a":
|
|
da["a"] = int(ze[1])
|
|
elif ze[0] == "s":
|
|
da["s"] = int(ze[1])
|
|
data.append(da)
|
|
input_file.close()
|
|
|
|
'''
|
|
# Ausgabe rules
|
|
for key in rules:
|
|
print(rules[key])
|
|
print("---------------------------------------------------------------------------")
|
|
# Ausgabe data
|
|
for da in data:
|
|
print(da)
|
|
print("---------------------------------------------------------------------------")
|
|
'''
|
|
|
|
|
|
def do_test(eq, dat):
|
|
eq = eq.split(":")[0]
|
|
if eq.count("<") == 1:
|
|
eq = eq.split("<")
|
|
if dat[eq[0]] < int(eq[1]):
|
|
return True
|
|
else:
|
|
return False
|
|
else:
|
|
eq = eq.split(">")
|
|
if dat[eq[0]] > int(eq[1]):
|
|
return True
|
|
else:
|
|
return False
|
|
|
|
|
|
def get_destination(k, werte):
|
|
if k == "R" or k == "A":
|
|
return k
|
|
for r in rules[k]:
|
|
if r.count(":") == 0:
|
|
return get_destination(r, werte)
|
|
else:
|
|
r = r.split(":")
|
|
if do_test(r[0], werte):
|
|
return get_destination(r[1], werte)
|
|
|
|
|
|
solution = 0
|
|
for i in range(len(data)):
|
|
r_or_a = get_destination("in", data[i])
|
|
if r_or_a == "A":
|
|
for key in data[i].keys():
|
|
solution += data[i][key]
|
|
|
|
print("Lösung:", solution)
|