Cleaned up 2024/24 P1 code
This commit is contained in:
parent
5bed995d01
commit
fed92e2e99
@ -20,6 +20,19 @@ def loadGates(input_f):
|
|||||||
instructions.append(tmp)
|
instructions.append(tmp)
|
||||||
return gates, instructions
|
return gates, instructions
|
||||||
|
|
||||||
|
def returnLogic(x,logic,y,z,gates):
|
||||||
|
# `AND` gates output `1` if *both* inputs are `1`; if either input is `0`, these gates output `0`.
|
||||||
|
if logic == 'AND':
|
||||||
|
return 1 if gates[x] and gates[y] else 0
|
||||||
|
|
||||||
|
# `OR` gates output `1` if *one or both* inputs is `1`; if both inputs are `0`, these gates output `0`.
|
||||||
|
if logic == 'OR':
|
||||||
|
return 1 if gates[x] or gates[y] else 0
|
||||||
|
|
||||||
|
# `XOR` gates output `1` if the inputs are *different*; if the inputs are the same, these gates output `0`.
|
||||||
|
if logic == 'XOR':
|
||||||
|
return 1 if gates[x] != gates[y] else 0
|
||||||
|
|
||||||
#########################################
|
#########################################
|
||||||
# #
|
# #
|
||||||
# Part 1 #
|
# Part 1 #
|
||||||
@ -36,61 +49,26 @@ def part1():
|
|||||||
x,logic,y,z = inst
|
x,logic,y,z = inst
|
||||||
|
|
||||||
if x not in gates or y not in gates:
|
if x not in gates or y not in gates:
|
||||||
#print(inst)
|
|
||||||
wait_for.append(inst)
|
wait_for.append(inst)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
for wdx, w in enumerate(wait_for):
|
for wdx, w in enumerate(wait_for):
|
||||||
x,logic,y,z = w
|
x,logic,y,z = w
|
||||||
if x in gates and y in gates:
|
if x in gates and y in gates:
|
||||||
#print(wait_for,w,wdx)
|
gates[z] = returnLogic(x,logic,y,z,gates)
|
||||||
# `AND` gates output `1` if *both* inputs are `1`; if either input is `0`, these gates output `0`.
|
|
||||||
if logic == 'AND':
|
|
||||||
gates[z] = 1 if gates[x] and gates[y] else 0
|
|
||||||
|
|
||||||
# `OR` gates output `1` if *one or both* inputs is `1`; if both inputs are `0`, these gates output `0`.
|
|
||||||
if logic == 'OR':
|
|
||||||
gates[z] = 1 if gates[x] or gates[y] else 0
|
|
||||||
|
|
||||||
# `XOR` gates output `1` if the inputs are *different*; if the inputs are the same, these gates output `0`.
|
|
||||||
if logic == 'XOR':
|
|
||||||
gates[z] = 1 if gates[x] != gates[y] else 0
|
|
||||||
wait_for.pop(wdx)
|
wait_for.pop(wdx)
|
||||||
#input()
|
|
||||||
|
|
||||||
x,logic,y,z = inst
|
x,logic,y,z = inst
|
||||||
|
|
||||||
# `AND` gates output `1` if *both* inputs are `1`; if either input is `0`, these gates output `0`.
|
gates[z] = returnLogic(x,logic,y,z,gates)
|
||||||
if logic == 'AND':
|
|
||||||
gates[z] = 1 if gates[x] and gates[y] else 0
|
|
||||||
|
|
||||||
# `OR` gates output `1` if *one or both* inputs is `1`; if both inputs are `0`, these gates output `0`.
|
|
||||||
if logic == 'OR':
|
|
||||||
gates[z] = 1 if gates[x] or gates[y] else 0
|
|
||||||
|
|
||||||
# `XOR` gates output `1` if the inputs are *different*; if the inputs are the same, these gates output `0`.
|
|
||||||
if logic == 'XOR':
|
|
||||||
gates[z] = 1 if gates[x] != gates[y] else 0
|
|
||||||
|
|
||||||
while len(wait_for) > 0:
|
while len(wait_for) > 0:
|
||||||
for wdx, w in enumerate(wait_for):
|
for wdx, w in enumerate(wait_for):
|
||||||
x,logic,y,z = w
|
x,logic,y,z = w
|
||||||
if x in gates and y in gates:
|
if x in gates and y in gates:
|
||||||
#print(wait_for,w,wdx)
|
gates[z] = returnLogic(x,logic,y,z,gates)
|
||||||
# `AND` gates output `1` if *both* inputs are `1`; if either input is `0`, these gates output `0`.
|
|
||||||
if logic == 'AND':
|
|
||||||
gates[z] = 1 if gates[x] and gates[y] else 0
|
|
||||||
|
|
||||||
# `OR` gates output `1` if *one or both* inputs is `1`; if both inputs are `0`, these gates output `0`.
|
|
||||||
if logic == 'OR':
|
|
||||||
gates[z] = 1 if gates[x] or gates[y] else 0
|
|
||||||
|
|
||||||
# `XOR` gates output `1` if the inputs are *different*; if the inputs are the same, these gates output `0`.
|
|
||||||
if logic == 'XOR':
|
|
||||||
gates[z] = 1 if gates[x] != gates[y] else 0
|
|
||||||
wait_for.pop(wdx)
|
wait_for.pop(wdx)
|
||||||
|
|
||||||
#dprint(gates)
|
|
||||||
result = ''
|
result = ''
|
||||||
|
|
||||||
countZ = 0
|
countZ = 0
|
||||||
@ -100,13 +78,11 @@ def part1():
|
|||||||
countZ += 1
|
countZ += 1
|
||||||
|
|
||||||
for i in range(countZ-1,-1,-1):
|
for i in range(countZ-1,-1,-1):
|
||||||
#print(i)
|
|
||||||
#print(g,gates[g])
|
|
||||||
if i < 10:
|
if i < 10:
|
||||||
result += str(gates['z0'+str(i)])
|
result += str(gates['z0'+str(i)])
|
||||||
else:
|
else:
|
||||||
result += str(gates['z'+str(i)])
|
result += str(gates['z'+str(i)])
|
||||||
print(result)
|
#print(result)
|
||||||
return int(result, 2)
|
return int(result, 2)
|
||||||
|
|
||||||
start_time = time.time()
|
start_time = time.time()
|
||||||
|
Loading…
Reference in New Issue
Block a user