64 lines
1.4 KiB
Python
64 lines
1.4 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
from time import time
|
|
|
|
start = time()
|
|
rules = {}
|
|
|
|
# parse input
|
|
input_file = open("input", "r")
|
|
|
|
for line in input_file:
|
|
line = line.strip()
|
|
if line == "":
|
|
break
|
|
line = line.split("{")
|
|
ru = line[1].replace("}", "").split(",")
|
|
rules[line[0]] = ru
|
|
input_file.close()
|
|
|
|
# Ausgabe rules
|
|
for key in rules:
|
|
print(rules[key])
|
|
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(1, 4001):
|
|
for j in range(1, 4001):
|
|
print(i, j, solution)
|
|
for m in range(1, 4001):
|
|
for n in range(1, 4001):
|
|
if get_destination("in", {"x": i, "m": j, "a": m, "s": n}) == "A":
|
|
solution += 1
|
|
print("Lösung:", solution)
|