diff --git a/progress.png b/progress.png index 0408b86..d44bc71 100644 Binary files a/progress.png and b/progress.png differ diff --git a/progress.yaml b/progress.yaml index 4efa9f4..31d7af7 100644 --- a/progress.yaml +++ b/progress.yaml @@ -27,4 +27,8 @@ 14: stars: 2 15: - stars: 2 \ No newline at end of file + stars: 2 +16: + stars: 0 +17: + stars: 1 \ No newline at end of file diff --git a/res/examples/day17.txt b/res/examples/day17.txt new file mode 100644 index 0000000..36fbf8d --- /dev/null +++ b/res/examples/day17.txt @@ -0,0 +1,5 @@ +Register A: 729 +Register B: 0 +Register C: 0 + +Program: 0,1,5,4,3,0 \ No newline at end of file diff --git a/src/day17/puzzle1.typ b/src/day17/puzzle1.typ new file mode 100644 index 0000000..6cda181 --- /dev/null +++ b/src/day17/puzzle1.typ @@ -0,0 +1,85 @@ +#import "/src/utils.typ": * + +#let ADV = 0 +#let BXL = 1 +#let BST = 2 +#let JNZ = 3 +#let BXC = 4 +#let OUT = 5 +#let BDV = 6 +#let CDV = 7 + +#let get-combo(regs, value) = { + if value >= 7 { + panic() + } + if value <= 3 { + return value + } + return regs.at(value - 4) +} + +#let solve(input) = { + let (registers, program) = input.split("\n\n") + + let regs = () + for line in registers.split("\n") { + regs.push( + int( + line.split(": ") + .last() + ) + ) + } + program = program.split(": ") + .last() + .split(",") + .map(int) + + let out = () + let pc = 0 + while pc < program.len() { + let op = program.at(pc) + let val = program.at(pc + 1) + if op == ADV { + let num = regs.at(0) + let den = get-combo(regs, val) + let res = num.bit-rshift(den) + regs.at(0) = res + } else if op == BXL { + regs.at(1) = regs.at(1).bit-xor(val) + } else if op == BST { + regs.at(1) = get-combo(regs, val).bit-and(0b111) + } else if op == JNZ { + if regs.at(0) != 0 { + pc = val + continue + } + } else if op == BXC { + regs.at(1) = regs.at(1).bit-xor(regs.at(2)) + } else if op == OUT { + out.push(get-combo(regs, val).bit-and(0b111)) + } else if op == BDV { + let num = regs.at(0) + let den = get-combo(regs, val) + let res = num.bit-rshift(den) + regs.at(1) = res + } else if op == CDV { + let num = regs.at(0) + let den = get-combo(regs, val) + let res = num.bit-rshift(den) + regs.at(2) = res + } else { + panic("Unknown instruction " + str(op)) + } + pc += 2 + } + + return out.map(str).join(",") +} + +#show-puzzle( + 17, 1, + solve, + example: "4,6,3,5,6,3,5,2,1,0" +) \ No newline at end of file diff --git a/src/day17/puzzle2.typ b/src/day17/puzzle2.typ new file mode 100644 index 0000000..e69de29