From c9c0feb1bc0e40422ef6c0982f4ded177b2a9ef2 Mon Sep 17 00:00:00 2001 From: tebarius Date: Tue, 19 Dec 2023 18:42:09 +0100 Subject: [PATCH] 19-1 fertig -- 19-2 kapituliert --- .idea/misc.xml | 2 +- 19/19-1.py | 42 ++++++++++++++++----------------- 19/19-2.py | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 84 insertions(+), 23 deletions(-) create mode 100644 19/19-2.py diff --git a/.idea/misc.xml b/.idea/misc.xml index c4ce182..9313b0a 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -3,7 +3,7 @@ - + \ No newline at end of file diff --git a/19/19-1.py b/19/19-1.py index 378cc7b..a8f0425 100644 --- a/19/19-1.py +++ b/19/19-1.py @@ -5,7 +5,7 @@ data = [] # parse input is_rule = True -input_file = open("input-ex", "r") +input_file = open("input", "r") for line in input_file: line = line.strip() @@ -32,6 +32,7 @@ for line in input_file: data.append(da) input_file.close() +''' # Ausgabe rules for key in rules: print(rules[key]) @@ -39,46 +40,43 @@ print("------------------------------------------------------------------------- # Ausgabe data for da in data: print(da) +print("---------------------------------------------------------------------------") +''' 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]): + if dat[eq[0]] < int(eq[1]): return True else: return False else: eq = eq.split(">") - if dat[eq[0]] > int(dat[1]): + if dat[eq[0]] > int(eq[1]): return True else: return False -def get_destination(k, dat): + +def get_destination(k, werte): + if k == "R" or k == "A": + return k 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) + return get_destination(r, werte) 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) + 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(get_destination("in", data[0])) \ No newline at end of file +print("Lösung:", solution) diff --git a/19/19-2.py b/19/19-2.py new file mode 100644 index 0000000..5826e8c --- /dev/null +++ b/19/19-2.py @@ -0,0 +1,63 @@ +#!/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)