summaryrefslogtreecommitdiff
path: root/2018/aoc2018-d16.py
blob: 3bb0dc15ba54f7105475b5077966c5830c8f4510 (plain)
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
#advent of code 2018 
#day 16
#part 1 and part 2

#when doing this puzzle I learned that I can make a list of functions and it just works
#code could use a bit of clean up, 
#lots of multiple lines because of 16 functions 
#(I could save ~32 lines just by modifying the functions)
#lots of work-arounds because I don't know to implement it efficently
#but still, I got the results

f = open("input.txt",'r');
Testing = False;
#Testing = True; 
if Testing:
	f.close();
	f = open("testinput.txt",'r');

input_part1 = [];
input_part2 = [];
sublist = [];

for l in f:
	l = l.replace("\n","");
	if (len(l) == 0):
		input_part1.append( sublist.copy() );
		sublist = [];
		continue;
	elif (l == "###"):
		break;
	elif (len(l) < 20):
		sublist.append( [int(x) for x in l.split(" ")] );
	elif (l[6] == ":"):
		sublist.append( eval(l[7:]) );
	elif (l[5] == ":"):
		sublist.append( eval(l[7:]) );
'''
for i in input_part1:
	print(i[0], "\t", i[1], "\t", i[2], "\t")
'''

for l in f: 
	l = l.replace("\n","");
	input_part2.append( [int(x) for x in l.split(" ")] );

'''
print();
print();
print();
for i in input_part2:
	print(i);
'''

###opcode functions
def addr(before, op, aft):
	bef = before.copy();
	C = bef[op[1]] + bef[op[2]];
	bef[op[3]] = C;
	return bef;
def addi(before, op, aft):
	bef = before.copy();
	C = bef[op[1]] + op[2];
	bef[op[3]] = C;
	return bef;

def mulr(before, op, aft):
	bef = before.copy();
	C = bef[op[1]] * bef[op[2]];
	bef[op[3]] = C;
	return bef;
def muli(before, op, aft):
	bef = before.copy();
	C = bef[op[1]] * op[2];
	bef[op[3]] = C;
	return bef;

def banr(before, op, aft):
	bef = before.copy();
	C = int(bef[op[1]] & bef[op[2]]);
	bef[op[3]] = C;
	return bef;
def bani(before, op, aft):
	bef = before.copy();
	C = int(bef[op[1]] & op[2]);
	bef[op[3]] = C;
	return bef;

def borr(before, op, aft):
	bef = before.copy();
	C = int(bef[op[1]] | bef[op[2]]);
	bef[op[3]] = C;
	return bef;
def bori(before, op, aft):
	bef = before.copy();
	C = int(bef[op[1]] | op[2]);
	bef[op[3]] = C;
	return bef;

def setr(before, op, aft):
	bef = before.copy();
	C = bef[op[1]];
	bef[op[3]] = C;
	return bef;
def seti(before, op, aft):
	bef = before.copy();
	C = op[1];
	bef[op[3]] = C;
	return bef;

def gtir(before, op, aft):
	bef = before.copy();
	C = 0 + int( op[1] > bef[op[2]] );
	bef[op[3]] = C;
	return bef;
def gtri(before, op, aft):
	bef = before.copy();
	C = 0 + int( op[2] < bef[op[1]] );
	bef[op[3]] = C;
	return bef;
def gtrr(before, op, aft):
	bef = before.copy();
	C = 0 + int( bef[op[1]] > bef[op[2]] );
	bef[op[3]] = C;
	return bef;

def eqir(before, op, aft):
	bef = before.copy();
	C = 0 + int( op[1] == bef[op[2]] );
	bef[op[3]] = C;
	return bef;
def eqri(before, op, aft):
	bef = before.copy();
	C = 0 + int( op[2] == bef[op[1]] );
	bef[op[3]] = C;
	return bef;
def eqrr(before, op, aft):
	bef = before.copy();
	C = 0 + int( bef[op[1]] == bef[op[2]] );
	bef[op[3]] = C;
	return bef;


def threeormore(bef, op, aft):
	fun = 1;
	counter = 0;
	out = aft.copy();
	counter += int( out == addr(bef,op,aft) );
	counter += int( out == addi(bef,op,aft) );
	counter += int( out == mulr(bef,op,aft) );
	counter += int( out == muli(bef,op,aft) );
	counter += int( out == banr(bef,op,aft) );
	counter += int( out == bani(bef,op,aft) );
	counter += int( out == borr(bef,op,aft) );
	counter += int( out == bori(bef,op,aft) );
	counter += int( out == setr(bef,op,aft) );
	counter += int( out == seti(bef,op,aft) );
	counter += int( out == gtir(bef,op,aft) );
	counter += int( out == gtri(bef,op,aft) );
	counter += int( out == gtrr(bef,op,aft) );
	counter += int( out == eqir(bef,op,aft) );
	counter += int( out == eqri(bef,op,aft) );
	counter += int( out == eqrr(bef,op,aft) );
	
	return counter;
	'''
	if (counter >= 3):
		return 1;
	else:
		return 0;
'''

part1 = 0;
for sample in input_part1:
	before = sample[0];
	instruction = sample[1];
	after = sample[2];
	part1 += 1*(threeormore(before, instruction, after ) >=3);
	
print("part 1 = ", part1);
	
#776 yoo high
#588 correct
#my mistake - misunderstood bitwise AND and bitwise OR
#they dont return a boolean, just a normal integer
#my function was int(bool(value)), which was reducing the correct value to 1



#identifying single matches
'''
singles = [];
doubles = [];
triples = [];

for sample in input_part1:
	before = sample[0];
	instruction = sample[1];
	after = sample[2];
	if (threeormore(before, instruction, after ) == 1):
		singles.append(instruction[0]);
	if (threeormore(before, instruction, after ) == 2):
		doubles.append(instruction[0]);
	if (threeormore(before, instruction, after ) >= 3):
		triples.append(instruction[0]);
	#part1 += 1*(threeormore(before, instruction, after ) >=3);

singles = list(dict.fromkeys(singles));
doubles = list(dict.fromkeys(doubles));
triples = list(dict.fromkeys(triples));

print("singles\t",len(singles), "\t", singles);
print("doubles\t",len(doubles), "\t", doubles);
print("triples\t",len(triples), "\t", triples);
'''



def matchingfunctions(bef, op, aft):
	functionlist = "";
	out = aft.copy();
	functionlist += "addr"*( out == addr(bef,op,aft) );
	functionlist += "addi"*( out == addi(bef,op,aft) );
	functionlist += "mulr"*( out == mulr(bef,op,aft) );
	functionlist += "muli"*( out == muli(bef,op,aft) );
	functionlist += "banr"*( out == banr(bef,op,aft) );
	functionlist += "bani"*( out == bani(bef,op,aft) );
	functionlist += "borr"*( out == borr(bef,op,aft) );
	functionlist += "bori"*( out == bori(bef,op,aft) );
	functionlist += "setr"*( out == setr(bef,op,aft) );
	functionlist += "seti"*( out == seti(bef,op,aft) );
	functionlist += "gtir"*( out == gtir(bef,op,aft) );
	functionlist += "gtri"*( out == gtri(bef,op,aft) );
	functionlist += "gtrr"*( out == gtrr(bef,op,aft) );
	functionlist += "eqir"*( out == eqir(bef,op,aft) );
	functionlist += "eqri"*( out == eqri(bef,op,aft) );
	functionlist += "eqrr"*( out == eqrr(bef,op,aft) );
	
	return (op[0],functionlist);

singles = [];
doubles = [];
triples = [];

for sample in input_part1:
	before = sample[0];
	instruction = sample[1];
	after = sample[2];
	if (threeormore(before, instruction, after ) == 1):
		singles.append(matchingfunctions(before, instruction, after ));
	if (threeormore(before, instruction, after ) == 2):
		doubles.append(matchingfunctions(before, instruction, after ));
	if (threeormore(before, instruction, after ) >= 3):
		triples.append(matchingfunctions(before, instruction, after ));
	#part1 += 1*(threeormore(before, instruction, after ) >=3);

'''
print("singles\t",len(singles), "\t", singles);
print("doubles\t",len(doubles), "\t", doubles);
print("triples\t",len(triples), "\t", triples);
'''

singles = list(dict.fromkeys(singles));
doubles = list(dict.fromkeys(doubles));
triples = list(dict.fromkeys(triples));

'''
for s in singles:
	print(s);
print();
print();
for s in doubles:
	print(s);
print();
print();
for s in triples:
	print(s);
print();
print();
'''

PossibleFunctions = {};
total = singles + doubles + triples;

for t in total:
	PossibleFunctions.update({t[0] : t[1]});

'''
for pf in PossibleFunctions:
	print(pf,"\t", PossibleFunctions[pf]);
	'''

IdentifiedFunctions = {};


while (len(IdentifiedFunctions) < 16):
	for found in IdentifiedFunctions:
		for possible in PossibleFunctions:
			if (len(PossibleFunctions[possible]) > 4 ):
				s = PossibleFunctions[possible];
				s = s.replace(IdentifiedFunctions[found], "");
				PossibleFunctions.update({possible : s});
			
	for pf in PossibleFunctions:
		if (len(PossibleFunctions[pf]) == 4 ):
			IdentifiedFunctions.update({pf : PossibleFunctions[pf]});
			
	'''
	for pf in PossibleFunctions:
		print(pf,"\t", PossibleFunctions[pf]);
	input();
	'''

'''
print();
print();
print();
print("Identified Functions");
for pf in IdentifiedFunctions:
	print(pf,"\t", IdentifiedFunctions[pf]);
'''
	
MatchFunctions = {};
MatchFunctions.update({"addr":addr});
MatchFunctions.update({"addi":addi});
MatchFunctions.update({"mulr":mulr});
MatchFunctions.update({"muli":muli});
MatchFunctions.update({"banr":banr});
MatchFunctions.update({"bani":bani});
MatchFunctions.update({"borr":borr});
MatchFunctions.update({"bori":bori});
MatchFunctions.update({"setr":setr});
MatchFunctions.update({"seti":seti});
MatchFunctions.update({"gtir":gtir});
MatchFunctions.update({"gtri":gtri});
MatchFunctions.update({"gtrr":gtrr});
MatchFunctions.update({"eqir":eqir});
MatchFunctions.update({"eqri":eqri});
MatchFunctions.update({"eqrr":eqrr});
	
for i in IdentifiedFunctions:
	MatchFunctions.update({i:MatchFunctions[IdentifiedFunctions[i]]});

'''
print();
print();
print();
print("Matched Functions");
for pf in MatchFunctions:
	print(pf,"\t", MatchFunctions[pf]);
'''

reg = [0,0,0,0];
for i in input_part2:
	f_i = MatchFunctions[i[0]];
	reg = f_i(reg, i, [] );

part2 = reg[0];
#print(reg);
print("part 2 = ", part2);