main
magical 2024-12-17 06:43:51 +00:00
parent 96f25dee8b
commit 7fcfd8fcb6
3 changed files with 149 additions and 0 deletions

5
day17/input 100644
View File

@ -0,0 +1,5 @@
Register A: 46337277
Register B: 0
Register C: 0
Program: 2,4,1,1,7,5,4,4,1,4,0,3,5,5,3,0

56
day17/sol.py 100644
View File

@ -0,0 +1,56 @@
sample = [0,1,5,4,3,0]
input = [2,4,1,1,7,5,4,4,1,4,0,3,5,5,3,0]
def simulate(a, prog, check=False):
b = 0
c = 0
ip = 0
out = []
while ip < len(prog):
op = prog[ip]
r = prog[ip+1]
if op in (0,2,5,6,7):
# combo op
if r == 4:
r = a
elif r == 5:
r = b
elif r == 6:
r = c
elif r == 7:
die
else:
pass # literal
if op == 0: # adv
a = a>>r
elif op == 1: # bxl
b ^= r
elif op == 2: # bst
b = r%8
elif op == 3: # jnz
if a != 0:
ip = r - 2
elif op == 4: # bxc
b ^= c
elif op == 5: # out
out.append(r%8)
if check and out[-1] != prog[len(out)-1]:
return out
elif op == 6:
b = a>>r
elif op == 7:
c = a>>r
ip += 2
print(",".join(str(x) for x in out))
return out
simulate(729, sample)
simulate(46337277, input)

88
day17/sol2.c 100644
View File

@ -0,0 +1,88 @@
#include <stdio.h>
int prog[] = {2,4,1,1,7,5,4,4,1,4,0,3,5,5,3,0};
int plen = sizeof(prog) / sizeof(int);
long reverse(int i, long init) {
long a = init;
// jnz,0
if (i >= plen) {
return init;
}
// in,5
int b = prog[plen-i-1];
// adv,3,
a <<= 3l;
// bxl,4
b ^= 4;
// cdv,5
// bxc,4
long q;
for (q = 0; q < 8; q++) {
if (((q^1) ^ ((((a|q)>>(q^1)))&7)) == b) {
// bst 4
// bxl,1
long x = reverse(i+1, a|q);
if (x >= 0) {
return x;
}
}
}
//error
if (a == 0) {
return -1;
}
return -a;
}
int run(long a) {
int b = 0;
int c = 0;
int i = 0;
// 4:a 5:b 6:c
do {
// bst 4
b = a%8;
//
// bxl,1
b ^= 1;
// cdv,5
c = (a&65535) >> b;
// bxc,4
b ^= c;
// bxl,4
b ^= 4;
// adv,3,
a >>= 3;
// out,5
printf("%d", b%8);
if (i >= plen || (b%8) != prog[i]) {
printf("!");
}
printf(" ");
i++;
// jnz,0
} while (a != 0);
printf("\n");
return 0;
}
int main() {
long a = reverse(0, 0);
printf("%ld\n", a);
run(a);
return 0;
}