19-1 fertig -- 19-2 kapituliert
This commit is contained in:
2
.idea/misc.xml
generated
2
.idea/misc.xml
generated
@@ -3,7 +3,7 @@
|
||||
<component name="Black">
|
||||
<option name="sdkName" value="Python 3.11 (AdventOfCode2023)" />
|
||||
</component>
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_21" project-jdk-name="Python 3.10 (AdventOfCode2023)" project-jdk-type="Python SDK">
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_21" project-jdk-name="Python 3.11 (AdventOfCode2023)" project-jdk-type="Python SDK">
|
||||
<output url="file://$PROJECT_DIR$/out" />
|
||||
</component>
|
||||
</project>
|
||||
42
19/19-1.py
42
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"
|
||||
return get_destination(r, werte)
|
||||
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)
|
||||
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]))
|
||||
print("Lösung:", solution)
|
||||
|
||||
63
19/19-2.py
Normal file
63
19/19-2.py
Normal file
@@ -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)
|
||||
Reference in New Issue
Block a user