Finish 2017.

This commit is contained in:
felixm 2024-06-09 11:52:25 -04:00
parent f62f5fcff2
commit dcfae2cb13
4 changed files with 233 additions and 2 deletions

108
2017/d23.py Normal file
View File

@ -0,0 +1,108 @@
import subprocess
from collections import defaultdict
def print_game(regs, i, insts):
print("\033[H\033[J", end="")
for j, inst in enumerate(insts):
start = " "
if j == i:
start = "> "
print(f"{start} {' '.join(inst)}")
for c in "abcdefgh":
print(c, regs[c])
def part_1(data):
regs = {c: 0 for c in "abcdefgh"}
insts = list(map(lambda line: line.split(), data.splitlines()))
i = 0
while i < len(insts):
inst = insts[i]
match inst:
case ["set", reg, val]:
regs[reg] = regs[val] if val in regs else int(val)
case ["sub", reg, val]:
regs[reg] -= regs[val] if val in regs else int(val)
case ["mul", reg, val]:
regs[reg] *= regs[val] if val in regs else int(val)
case ["jnz", reg, val]:
val = regs[val] if val in regs else int(val)
assert type(val) is int
if val != 0:
i += (val - 1)
case _:
assert False
i += 1
def to_c(data):
insts = list(map(lambda line: line.split(), data.splitlines()))
label_count = 0
labels = defaultdict(list)
for i, inst in enumerate(insts):
match inst:
case ["jnz", reg, val]:
label = f"label_{label_count}"
label_count += 1
inst[2] = label
label_i = i + int(val)
labels[label_i].append(label)
case _:
pass
i = 0
begin = ["#include <stdio.h>", "",
"int mul(int a, int b) {",
" return a * b;",
"}",
"",
"int main() {" ]
decls = " int "
for v in "abcdefgh":
decls += f"{v} = 0, "
decls = decls[:-2] + ";"
begin.append(decls)
body = []
end = [ " return 0;", "}", ]
for i, inst in enumerate(insts):
if i in labels:
for label in labels[i]:
body.append(label + ":")
match inst:
case ["set", reg, val]:
body.append(f" {reg} = {val};")
case ["sub", reg, val]:
body.append(f" {reg} -= {val};")
case ["mul", reg, val]:
body.append(f" {reg} = mul({reg}, {val});")
case ["jnz", reg, val]:
body.append(f" if ({reg} != 0) goto {val};")
case _:
assert False
body.append(labels[i + 1][0] + ":")
all = begin + body + end
with open("i23.c", "w") as f:
for line in all:
f.write(line + "\n")
def run_c():
subprocess.call(["gcc", "i23.c"])
subprocess.call(["./a.out"])
subprocess.call(["rm", "a.out"])
def main():
with open("i23.txt") as f:
data = f.read()
# to_c(data)
# part_1(data)
run_c()
if __name__ == "__main__":
main()

58
2017/d25.py Normal file
View File

@ -0,0 +1,58 @@
from lib import str_to_int
from collections import defaultdict
def part_1(data):
rules = {}
steps = None
start_state = None
current_state = None
current_value = None
for line in data.splitlines():
if line.startswith("In state"):
current_state = line[-2]
rules[current_state] = {}
elif "current value" in line:
current_value = str_to_int(line)
rules[current_state][current_value] = []
elif "- Write the value" in line:
rules[current_state][current_value].append(str_to_int(line))
elif "- Move one slot to the right" in line:
rules[current_state][current_value].append(1)
elif "- Move one slot to the left" in line:
rules[current_state][current_value].append(-1)
elif "Continue with state" in line:
rules[current_state][current_value].append(line[-2])
elif "Begin in state" in line:
start_state = line[-2]
elif "checksum after" in line:
steps = str_to_int(line)
elif line.strip() == "":
pass
else:
print(line)
assert False
assert type(steps) is int
assert start_state is not None
current_state = start_state
tape = defaultdict(int)
pos = 0
for _ in range(steps):
current_value = tape[pos]
write_value, move, next_state = rules[current_state][current_value]
tape[pos] = write_value
pos += move
current_state = next_state
print(sum(tape.values()))
def main():
with open("i25.txt") as f:
data = f.read()
part_1(data)
if __name__ == "__main__":
main()

65
2017/i23.c Normal file
View File

@ -0,0 +1,65 @@
#include <stdio.h>
unsigned int mul_count = 0;
int mul(int a, int b) {
mul_count++;
return a * b;
}
int main() {
for (int a = 0; a < 2; a++) {
int b = 0, c = 0, d = 0, e = 0, f = 0, g = 1, h = 0;
b = 67;
c = b;
if (a != 0) {
b = mul(b, 100);
b += 100000;
c = b;
c += 17000;
}
while (1) {
f = 1;
d = 2;
while (g != 0)
{
e = 2;
while (g != 0) {
if (b % d == 0) {
int e_required = b / d;
if (e_required >= 2 && e_required <= b) {
f = 0;
}
}
mul_count += (b - e);
g = 0;
}
d += 1;
g = d;
g -= b;
}
if (f == 0) {
h += 1;
}
g = b;
g -= c;
if (g != 0) {
b += 17;
} else {
break;
}
}
if (a == 0) {
printf("%d\n", mul_count);
} else {
printf("%d\n", h);
}
}
return 0;
}

View File

@ -83,6 +83,6 @@ written in Python.
- Day 20: 9:55 (would have been 10th)
- Day 21: 90:00
- Day 22: 25:00
- Day 23:
- Day 23: Multiple days... but super fun.
- Day 24: 15:45 (48th)
- Day 25:
- Day 25: 41:00