Skip to content

Commit ba20d6b

Browse files
committed
dec 17
1 parent 68fdb0f commit ba20d6b

File tree

4 files changed

+135
-0
lines changed

4 files changed

+135
-0
lines changed

dec17/input.txt

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Register A: 41644071
2+
Register B: 0
3+
Register C: 0
4+
5+
Program: 2,4 ,1,2 ,7,5 ,1,7 ,4,4 ,0,3 ,5,5 ,3,0

dec17/part1.py

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
lines = []
2+
with open("dec17/input.txt", "r") as file:
3+
for line in file.readlines():
4+
lines.append(line.strip())
5+
6+
total = 0
7+
a = int(lines[0][lines[0].index(":") + 2:])
8+
b = int(lines[1][lines[1].index(":") + 2:])
9+
c = int(lines[2][lines[2].index(":") + 2:])
10+
ins = [int(i) for i in lines[4][lines[4].index(":") + 2:].split(",")]
11+
12+
def combo(operand):
13+
if operand < 4:
14+
return operand
15+
else:
16+
return [a, b, c][operand - 4]
17+
18+
pointer = 0
19+
output = []
20+
21+
while pointer < len(ins):
22+
opcode = ins[pointer]
23+
operand = ins[pointer + 1]
24+
match opcode:
25+
case 0:
26+
a = a // 2**combo(operand)
27+
pass
28+
case 1:
29+
b = b ^ operand
30+
pass
31+
case 2:
32+
b = combo(operand) % 8
33+
pass
34+
case 3:
35+
if a != 0:
36+
pointer = operand
37+
pointer -= 2
38+
pass
39+
case 4:
40+
b = b ^ c
41+
pass
42+
case 5:
43+
output.append(str(combo(operand) % 8))
44+
pass
45+
case 6:
46+
b = a // 2**combo(operand)
47+
pass
48+
case 7:
49+
c = a // 2**combo(operand)
50+
pass
51+
52+
pointer += 2
53+
print(output)
54+
55+
56+
print(",".join(output))

dec17/part2.py

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
lines = []
2+
with open("dec17/input.txt", "r") as file:
3+
for line in file.readlines():
4+
lines.append(line.strip())
5+
6+
total = 0
7+
a = int(lines[0][lines[0].index(":") + 2:])
8+
b = int(lines[1][lines[1].index(":") + 2:])
9+
c = int(lines[2][lines[2].index(":") + 2:])
10+
ins = [int(i) for i in lines[4][lines[4].index(":") + 2:].split(",")]
11+
12+
def runbits(inita, numbits):
13+
result = [-1 for i in range(numbits)]
14+
a = inita
15+
16+
for outbit in range(numbits):
17+
18+
b = a % 8
19+
b ^= 2
20+
c = a // (2**b)
21+
b ^= 7
22+
b ^= c
23+
result[outbit] = b % 8
24+
a //= 8
25+
if a == 0:
26+
break
27+
return tuple(result)
28+
29+
bitsfound = []
30+
foundcount = 0
31+
currentnums = [0]
32+
33+
while foundcount < len(ins):
34+
newnums = []
35+
for currentnum in currentnums:
36+
foundbits = []
37+
38+
binf = ""
39+
remf = currentnum
40+
while len(binf) < (foundcount + 1) * 3:
41+
binf = str(remf % 2) + binf
42+
remf //= 2
43+
print("testing:", currentnum, binf)
44+
45+
for tbits in range(8):
46+
thisresult = runbits(tbits + currentnum * 8, foundcount + 1)
47+
for check in range(foundcount + 1):
48+
insind = len(ins) - foundcount - 1 + check
49+
if thisresult[check] != -1 and insind < len(ins) and thisresult[check] != ins[insind]:
50+
break
51+
else:
52+
foundbits.append(tbits)
53+
54+
print(len(foundbits))
55+
for f in foundbits:
56+
binf = ""
57+
remf = f + currentnum * 8
58+
while len(binf) < (foundcount + 1) * 3:
59+
binf = str(remf % 2) + binf
60+
remf //= 2
61+
print(f, binf, str(runbits(f + currentnum * 8, foundcount + 1)))
62+
newnums.append(currentnum * 8 + f)
63+
64+
currentnums = newnums
65+
foundcount += 1
66+
print(currentnums)
67+
print(min(currentnums))
68+
69+

dec17/sample.txt

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Register A: 729
2+
Register B: 0
3+
Register C: 0
4+
5+
Program: 0,1,5,4,3,0

0 commit comments

Comments
 (0)