-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d03d5c2
commit 9576ed2
Showing
4 changed files
with
166 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
Register A: 729 | ||
Register B: 0 | ||
Register C: 0 | ||
|
||
Program: 0,1,5,4,3,0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { expect, describe, test } from "bun:test"; | ||
import { part1, part2 } from "."; | ||
import { getInputs } from "../../utils/get-inputs"; | ||
|
||
const { exampleInput, puzzleInput } = await getInputs("2024/17"); | ||
|
||
describe("part 1", () => { | ||
test("example", () => { | ||
expect(part1(exampleInput)).toBe("4,6,3,5,6,3,5,2,1,0"); | ||
}); | ||
|
||
test("puzzle", () => { | ||
expect(part1(puzzleInput)).toBe("7,1,3,4,1,2,6,7,1"); | ||
}); | ||
}); | ||
|
||
const exampleInputForPart2 = ` | ||
Register A: 2024 | ||
Register B: 0 | ||
Register C: 0 | ||
Program: 0,3,5,4,3,0 | ||
`.trim(); | ||
|
||
describe("part 2", () => { | ||
test("example", () => { | ||
expect(part2(exampleInputForPart2)).toBe("117440"); | ||
}); | ||
|
||
test("puzzle", () => { | ||
expect(part2(puzzleInput)).toBe("109019476330651"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
import { timePart1, timePart2 } from "../../utils/time-part"; | ||
|
||
const parseInput = (input: string) => { | ||
const [registers, program] = input.split("\n\n"); | ||
|
||
const [a, b, c] = registers | ||
.split("\n") | ||
.map((register) => BigInt(register.split(": ")[1])); | ||
|
||
return { | ||
registers: { a, b, c }, | ||
program: program.split(": ")[1].split(",").map(BigInt), | ||
}; | ||
}; | ||
|
||
const modulo = (a: bigint, b: bigint) => ((a % b) + b) % b; | ||
|
||
const getComboOperand = ( | ||
literalOperand: bigint, | ||
registers: ReturnType<typeof parseInput>["registers"] | ||
) => { | ||
if (literalOperand === 4n) return registers.a; | ||
if (literalOperand === 5n) return registers.b; | ||
if (literalOperand === 6n) return registers.c; | ||
|
||
return literalOperand; | ||
}; | ||
|
||
const runProgram = ({ program, registers }: ReturnType<typeof parseInput>) => { | ||
let output: bigint[] = []; | ||
|
||
let i = 0; | ||
while (true) { | ||
if (i >= program.length) { | ||
break; | ||
} | ||
|
||
const opcode = program[i]; | ||
const operandIndex = i + 1; | ||
|
||
if (operandIndex >= program.length) { | ||
break; | ||
} | ||
|
||
const literalOperand = program[operandIndex]; | ||
const comboOperand = getComboOperand(literalOperand, registers); | ||
|
||
// adv | ||
if (opcode === 0n) { | ||
registers.a = registers.a / 2n ** comboOperand; | ||
} | ||
|
||
// bxl | ||
if (opcode === 1n) { | ||
registers.b = registers.b ^ literalOperand; | ||
} | ||
|
||
// bst | ||
if (opcode === 2n) { | ||
registers.b = modulo(comboOperand, 8n); | ||
} | ||
|
||
// jnz | ||
if (opcode === 3n && registers.a !== 0n) { | ||
i = Number(literalOperand); | ||
continue; | ||
} | ||
|
||
// bxc | ||
if (opcode === 4n) { | ||
registers.b = registers.b ^ registers.c; | ||
} | ||
|
||
// out | ||
if (opcode === 5n) { | ||
output.push(modulo(comboOperand, 8n)); | ||
} | ||
|
||
// bdv | ||
if (opcode === 6n) { | ||
registers.b = registers.a / 2n ** comboOperand; | ||
} | ||
|
||
// cdv | ||
if (opcode === 7n) { | ||
registers.c = registers.a / 2n ** comboOperand; | ||
} | ||
|
||
// Skip operand | ||
i += 2; | ||
} | ||
|
||
return output.join(","); | ||
}; | ||
|
||
export const part1 = timePart1((input: string) => { | ||
const { program, registers } = parseInput(input); | ||
|
||
return runProgram({ program, registers }); | ||
}); | ||
|
||
export const part2 = timePart2((input: string) => { | ||
const { program, registers } = parseInput(input); | ||
|
||
const programStr = program.join(","); | ||
let a = 0n; | ||
|
||
while (true) { | ||
const output = runProgram({ program, registers: { ...registers, a } }); | ||
|
||
if (output === programStr) { | ||
break; | ||
} | ||
|
||
// Once we _start_ to find a match | ||
if (programStr.indexOf(output) + output.length === programStr.length) { | ||
// Just increment in instances of 8-times the value of the `A` register | ||
a = 8n * a; | ||
|
||
continue; | ||
} | ||
|
||
a++; | ||
} | ||
|
||
return a.toString(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters