with open('data-2024-24.txt', 'r') as f:
inp = f.read().splitlines()
init = inp[:90]
log = inp[91:]Advent of Code 2024 Day 24
— Day 24: Crossed Wires —
There are some gates and some wires and some logic, and we want to produce a number.
Simulate the system of gates and wires. What decimal number does it output on the wires starting with z?
Create dictionaries to keep track of these things. Actually a dict and a list.
# here's another python cell for good luck
di = {}
for i in init:
a,b = i.split(': ')
di[a] = int(b)
dl = [i.split() for i in log]Now we need to go through with some smart order. Start with x and y?
Examples
['qgv', 'OR', 'dsf', '->', 'mjm']
['shj', 'AND', 'wsr', '->', 'ftr']
def combo(x):
if x[1] == 'XOR':
if di[x[0]] + di[x[2]] == 1:
return 1
else:
return 0
elif x[1] == 'OR':
if di[x[0]] + di[x[2]] >= 1:
return 1
else:
return 0
elif x[1] == 'AND':
if di[x[0]] + di[x[2]] == 2:
return 1
else:
return 0
else:
raise
while True:
for d in dl:
if d[0] in di.keys() and d[2] in di.keys():
di[d[4]] = combo(d)
break
len(di)
# actually just run this a bunch of times
# until the length doesn't change
# 312 length 181
get the z
zz = []
# only get the z, combined into list
for k,v in di.items():
if k[0] == 'z':
# print(k,v)
zz.append(' '.join([k,str(v)]))
# check
zz
# sort in order
zz.sort()
# split and get the numbers
znum = [x.split()[1] for x in zz]
# check
znum
# join and reverse the string
zbin = ''.join(znum)[::-1]
# convert binary to integer
int(zbin, 2)
# 5063606933169
# 388699843354324
— Part Two —
Actually, we are trying to add x digits and y digits, but the system isn’t working properly. What do you get if you sort the names of the eight wires involved in a swap and then join those names with commas?