1+ #!/usr/bin/env python
2+ # -*- coding: utf-8 -*-
3+
4+ def parse (data ):
5+ wires = {}
6+
7+ section_1 , section_2 = data .strip ().split ('\n \n ' )
8+ for line in section_1 .split ('\n ' ):
9+ wire , output = line .split (': ' )
10+ wires [wire ] = int (output )
11+
12+ queue = []
13+
14+ for line in section_2 .split ('\n ' ):
15+ inp_1 , op , inp_2 , _ , wire = line .split ()
16+ wires [wire ] = [inp_1 , inp_2 , op ]
17+ if wire [0 ] == 'z' :
18+ queue .append (wire )
19+
20+ return wires , queue
21+
22+ split_data = parse
23+ completed = 1
24+ raw_data = None # Not To be touched
25+
26+ def part1 (data ):
27+ wires , z_wires = data
28+
29+ def determine (wire ):
30+ if type (wires [wire ]) == int :
31+ return wires [wire ]
32+ inp_1 , inp_2 , op = wires [wire ]
33+ inp_1 = determine (inp_1 )
34+ inp_2 = determine (inp_2 )
35+ if op == 'AND' :
36+ out = inp_1 & inp_2
37+ elif op == 'OR' :
38+ out = inp_1 | inp_2
39+ else :
40+ out = inp_1 ^ inp_2
41+ wires [wire ] = out
42+ return out
43+
44+ number = '' .join (str (determine (wire )) for wire in sorted (z_wires , reverse = True ))
45+ print (number )
46+ return int (number , 2 )
47+
48+
49+ def part2 (data ):
50+ # We need to study a total of 8 eight swaps. Going based on combinatorics that is 221!/(213)! ~= 2*10^16
51+ # So we need to come up with a smarted way. We know that the operations are supposed to perform addition
52+ # We also know they start with x## and y## and we perform addition.
53+
54+ # Since we know it is addition. We can start by matching the least significant bits...
55+ # Their mistake can tell us which section the fault lies in, which section it doesn't and so on...
56+
57+ # We could even try and compare the wiring presented to the actual bit adder diagrams and go from there
58+
59+
60+
61+
62+ wires , z_wires = data
63+ x_wires = sorted ([wire for wire in wires if wire [0 ] == 'x' ], reverse = True )
64+ y_wires = sorted ([wire for wire in wires if wire [0 ] == 'y' ], reverse = True )
65+ x_number = int ('' .join (str (wires [wire ]) for wire in x_wires ), 2 )
66+ y_number = int ('' .join (str (wires [wire ]) for wire in y_wires ), 2 )
67+
68+ print ('' .join (str (wires [wire ]) for wire in x_wires ))
69+ print ('' .join (str (wires [wire ]) for wire in y_wires ))
70+ print (bin (x_number + y_number )[2 :])
71+
72+ print (x_number , y_number , x_number + y_number )
73+
74+
75+ # Get me all the x and y wires...
76+ ...
0 commit comments