84 lines
2.0 KiB
Python
84 lines
2.0 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
rules = {}
|
|
data = []
|
|
|
|
# parse input
|
|
is_rule = True
|
|
input_file = open("input-ex", "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)
|
|
|
|
|
|
def do_test(eq, dat):
|
|
print(eq)
|
|
eq = eq.split(":")[0]
|
|
if eq.count("<") == 1:
|
|
eq = eq.split("<")
|
|
print(eq[0], dat[eq[0]],"teeeee")
|
|
if dat[eq[0]] < int(dat[1]):
|
|
return True
|
|
else:
|
|
return False
|
|
else:
|
|
eq = eq.split(">")
|
|
if dat[eq[0]] > int(dat[1]):
|
|
return True
|
|
else:
|
|
return False
|
|
|
|
def get_destination(k, dat):
|
|
for r in rules[k]:
|
|
print(r, dat)
|
|
if r.count(":") == 0:
|
|
if r == "R":
|
|
return "R"
|
|
elif r == "A":
|
|
return "A"
|
|
else:
|
|
get_destination(r, dat)
|
|
else:
|
|
x, m, a, s = dat["x"], dat["m"], dat["s"], dat["a"]
|
|
r = r.split(":")
|
|
if do_test(r[0], dat):
|
|
if r[1] == "R":
|
|
return "R"
|
|
elif r[1] == "A":
|
|
return "A"
|
|
else:
|
|
get_destination(r[1], dat)
|
|
|
|
|
|
|
|
print(get_destination("in", data[0])) |