+/AoC

advent of code

git clone https://git.julia.blue/+/AoC

Julia RyanDay 17d3fca86

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