+/AoC
advent of code
git clone https://git.julia.blue/+/AoC
d3fca86
master
1def run (a ,b ,c ,instructions ): 2out = [] 3 4def combo (n ): 5match n : 6case n if n <= 3 : 7return n 8case 4 : 9return a 10case 5 : 11return b 12case 6 : 13return c 14case _: 15exit ("unsupported" ) 16 17ip = 0 18while True : 19if ip > (len (instructions )- 2 ): 20break 21i ,n = instructions [ip :ip + 2 ] 22# print(ip, i, n) 23ip += 2 24match i : 25case 0 : 26a >>= combo (n ) 27case 1 : 28b ^= n 29case 2 : 30b = combo (n )% 8 31case 3 : 32if a != 0 : 33ip = n 34case 4 : 35b ^= c 36case 5 : 37out .append (combo (n )% 8 ) 38case 6 : 39b = a >> combo (n ) 40case 7 : 41c = a >> combo (n ) 42return out 43 44 45def dot (instructions ): 46nodes = [] 47edges = [] 48 49def combo (n ): 50match n : 51case n if n <= 3 : 52return n 53case 4 : 54return "a" 55case 5 : 56return "b" 57case 6 : 58return "c" 59case _: 60return "illegal" 61 62for ip in range (len (instructions )- 1 ): 63i ,n = instructions [ip :ip + 2 ] 64if ip >= 2 : 65edges .append ((ip - 2 ,ip )) 66c = combo (n ) 67match i : 68case 0 : 69temp = (2 ** c )if type (c )is int else f"2^ { c } " 70# nodes.append(f"ADV {temp}") 71nodes .append (f"a>>= { c } " ) 72case 1 : 73# nodes.append(f"BXL {n}") 74nodes .append (f"b^= { n } " ) 75case 2 : 76temp = (c % 8 )if type (c )is int else f" { c } %8" 77# nodes.append(f"BST {temp}") 78nodes .append (f"b= { temp } " ) 79case 3 : 80nodes .append (f"JNZ { n } " ) 81edges .append ((ip ,n )) 82# TODO: don't make node here 83case 4 : 84# nodes.append("BXC") 85nodes .append ("b^=c" ) 86case 5 : 87temp = (c % 8 )if type (c )is int else f" { c } %8" 88nodes .append (f"OUT { temp } " ) 89case 6 : 90# temp = (2**c) if type(c) is int else f"2^{c}" 91# nodes.append(f"BDV {temp}") 92nodes .append (f"b=a>> { c } " ) 93case 7 : 94# temp = (2**c) if type(c) is int else f"2^{c}" 95# nodes.append(f"CDV {temp}") 96nodes .append (f"c=a>> { c } " ) 97 98"digraph {" ) 99for name ,label in enumerate (nodes ): 100f'i { name } [label=" { label } "]' ) 101for l ,r in edges : 102f"i { l } -> i { r } " ) 103f"i { len ( instructions ) - 2 } -> END" ) 104f"i { len ( instructions ) - 3 } -> END" ) 105"START -> i0" ) 106"}" ) 107exit () 108 109 110with open ("input" )as f : 111raw = f .read () 112 113regs ,program = raw .split ("\n\n" ) 114(a ,b ,c )= [int (s .split ()[- 1 ])for s in regs .splitlines ()] 115instructions = [int (i )for i in program .split ()[- 1 ].split ("," )] 116ans = "," .join ((map (str ,run (a ,b ,c ,instructions )))) 117ans ) 118 119# dot(instructions) 120 121 122def p2 (cur ,instructions ): 123out = instructions [- 1 ] 124# print((16 - len(instructions)) * " ", cur, out) 125for a in range (8 ): 126b = a ^ 0b101 127c = ((cur | a )>> b )& 0b111 128temp = b ^ c ^ 0b110 129# print((16 - len(instructions)) * " ", f"{a:0b} {temp:0b}") 130if temp == out : 131# print(f"{out}: {cur|a:048b}") 132if len (instructions )== 1 : 133return cur | a 134if ans := p2 ((cur | a )<< 3 ,instructions [:- 1 ]): 135return ans 136return None 137 138 139ans = p2 (0 ,instructions ) 140# print(run(ans, 0, 0, instructions)) 141# print(instructions) 142ans )