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 add2sus(sus, g, s): if s in g: sus.add(s) add2sus(sus, g, g[s][0]) add2sus(sus, g, g[s][2]) def sus(z1, z2, g): sus_g = set() 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)) def gen_n(x,y): n={} for c in range(45): n[f"x{c:02}"] = int(0) n[f"y{c:02}"] = int(0) xbin = list(bin(x)[2:]) ybin = list(bin(y)[2:]) xbin.reverse() ybin.reverse() 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 test(x, y, z, g): n = gen_n(x, y) z = part_one(n, g.copy()) return z == x+y def check_l_rec(s, g, visited): if s in g: for ss in g[s][0]: if ss in visited: return True if check_l_rec(ss, g, set(list(visited)+[ss])): return True for ss in g[s][2]: if ss in visited: return True if check_l_rec(ss, g, set(list(visited)+[ss])): return True return False def check4loop(g): for i in g: if 'z' in i: if check_l_rec(i, g, set(i)): return True else: return False def part_two(n, g): # import graphviz # dot = graphviz.Digraph(comment='The Round Table') # for i in g: # dot.node(i) # for i in g: # color = "blue" # if g[i][1] == "AND": # color = "red" # elif g[i][1] == "OR": # color = "green" # dot.edge(g[i][0], i, g[i][1], color=color) # dot.edge(g[i][2], i, g[i][1], color=color ) # dot.render('round-table.gv', view=True) 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()