part1
This commit is contained in:
parent
02f61c5687
commit
ae5f68418f
98
2024/day24/main.py
Normal file
98
2024/day24/main.py
Normal file
@ -0,0 +1,98 @@
|
||||
import sys
|
||||
import os
|
||||
import random
|
||||
sys.path.insert(1, os.path.abspath('../../'))
|
||||
from python_tools.aoc_utils import *
|
||||
|
||||
def resolve(i, n, g):
|
||||
if i in n:
|
||||
return n[i]
|
||||
else:
|
||||
a = resolve(g[i][0], n, g)
|
||||
b = resolve(g[i][2], n, g)
|
||||
if g[i][1] == "AND":
|
||||
return a & b
|
||||
elif g[i][1] == "OR":
|
||||
return a | b
|
||||
elif g[i][1] == "XOR":
|
||||
return a ^ b
|
||||
|
||||
|
||||
def part_one(n, g):
|
||||
p1 = 0
|
||||
for i in g:
|
||||
if "z" in i:
|
||||
p1 = p1 | (resolve(i, n, g) << int(i.replace("z", "")))
|
||||
return p1
|
||||
|
||||
def combine(n, s):
|
||||
num = 0
|
||||
for i in n:
|
||||
if s in i:
|
||||
num = num | n[i] << int(i.replace(s, ""))
|
||||
return num
|
||||
|
||||
def add2sus(sus, g, s):
|
||||
if s in g:
|
||||
sus[s] = []
|
||||
add2sus(sus, g, g[s][0])
|
||||
add2sus(sus, g, g[s][2])
|
||||
|
||||
def sus(z1, z2, g):
|
||||
sus_g = {}
|
||||
binz = list(bin(z1 ^ z2)[2:])
|
||||
binz.reverse()
|
||||
for c, i in enumerate(binz):
|
||||
if i == '1':
|
||||
add2sus(sus_g, g, f"z{c:02}")
|
||||
print(len(sus_g))
|
||||
print(len(g))
|
||||
|
||||
|
||||
pass
|
||||
|
||||
def gen_n(x,y):
|
||||
xbin = list(bin(x)[2:])
|
||||
ybin = list(bin(y)[2:])
|
||||
xbin.reverse()
|
||||
ybin.reverse()
|
||||
n={}
|
||||
for c, x in enumerate(xbin):
|
||||
n[f"x{c:02}"] = int(x)
|
||||
for c, y in enumerate(ybin):
|
||||
n[f"y{c:02}"] = int(y)
|
||||
return n
|
||||
|
||||
def part_two(n, g):
|
||||
x = random.getrandbits(56)
|
||||
y = random.getrandbits(56)
|
||||
n = gen_n(x, y)
|
||||
x = combine(n, "x")
|
||||
y = combine(n, "y")
|
||||
z = part_one(n.copy(), g.copy())
|
||||
print("y, ", bin(y))
|
||||
print("x, ", bin(x))
|
||||
print("z, ", bin(z))
|
||||
print("W, ", bin(x+y))
|
||||
print("^, ", bin(z^(x+y)))
|
||||
sus(z, x+y, g)
|
||||
pass
|
||||
|
||||
def main():
|
||||
contents = file2list("input.txt")
|
||||
n = {}
|
||||
g = {}
|
||||
for i in contents:
|
||||
if ": " in i:
|
||||
x = i.split(": ")
|
||||
n[x[0]] = int(x[1])
|
||||
elif "->" in i:
|
||||
x = i.split(" -> ")
|
||||
g[x[1]] = x[0].split(" ")
|
||||
|
||||
print("p1: ", part_one(n.copy(), g.copy()))
|
||||
part_two(n.copy(), g.copy())
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
Loading…
Reference in New Issue
Block a user