-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathday19.3.py
155 lines (121 loc) · 3.6 KB
/
day19.3.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
### und nochmal Teil 1, jetzt mit eval Funktion
### leider immer noch UNERFOLGREICH
### Beispiel tut, Puzzel nicht
def einlesen(filename):
"""
liest alle Zeilen eines Textfiles ein und speichert jede einzelne Zeile in einer Liste
"""
i = 0
document = []
document2 = []
with open(filename, "r") as file:
# read file line by line and output the lines
for line in file:
line = line.strip()
if line == "":
i = 1
if i == 0:
document.append(line)
else:
document2.append(line)
document2.pop(0)
return document, document2
def part_read(string):
string_liste = []
dic = {}
# string = string[1: len(string)-1]
string = string.strip("{")
string = string.strip("}")
string_liste = string.split(",")
for eintrag in string_liste:
key, value = eintrag.split("=")
dic[key] = int(value)
return dic
def workflow_dic_aus_liste(data):
"""
erstellt ein Dictionary aus einer Liste mit den gegebenen Workflows
"""
dic = {}
for eintrag in data:
key, values = eintrag.split("{")
values = values.strip("}")
vals = values.split(",")
dic[key] = workflow_einlesen(vals)
return dic
def workflow_einlesen(wf):
"""
liest einen einzelnen flow aus einem Workflow ein und gibt eine Liste zurück
"""
liste = []
for bed in wf:
zeichen = ""
iliste = []
if ":" in bed:
cond, nwf = bed.split(":")
iliste.append(cond)
iliste.append(nwf)
liste.append(iliste)
else:
liste.append(bed)
return liste
def next_wf(workflow, material):
"""
ermittelt den nächsten WF aus der Materialliste und dem Workflow
"""
for key in material:
# print(key, material[key])
for bed in workflow[0:len(workflow)-1]:
x = m = a = s = 0
nwfl = workflow[-1]
if key in bed[0]:
if key == "x":
x = material[key]
elif key == "m":
m = material[key]
elif key == "a":
a = material[key]
elif key == "s":
s = material[key]
if eval(bed[0]) == True:
nwfl = bed[1]
break
if nwfl != workflow[-1]:
break
return nwfl
###
file = "AOC_day19_input_bsp.txt"
workflows_inp, materials_inp = einlesen(file)
workflow_dic = workflow_dic_aus_liste(workflows_inp)
materials = []
for line in materials_inp:
materials.append(part_read(line))
workflows = workflow_dic
#print("MAT: ", materials) #dictionary mit materials fertig
#print("WF: ", workflows) #workflows jetzt auch fertig
t_val = 0
for line in materials:
wf_no = "in"
while True:
key = ""
print("material", line)
print("workflow", wf_no, workflows[wf_no])
for key in line:
enth = False
nwf_no = next_wf(workflows[wf_no], line)
wf_no = nwf_no
if nwf_no == "A":
l_val = 0
for key in line:
l_val += line[key]
t_val = t_val + l_val
print("ACCEPTED", l_val, "für", line)
print(" ")
break
elif nwf_no == "R":
print("REJECTED")
print(" ")
break
print("nächster Workflow", wf_no)
print(" ")
print(" ")
print("total value", t_val)