From 169d683cdb2b0dd57991e0761c8f2f27bfc76f84 Mon Sep 17 00:00:00 2001 From: TobenderZephyr Date: Wed, 18 Dec 2024 13:39:20 +0100 Subject: [PATCH] day 17 part 1 finished --- 2024/17/example.txt | 5 +++ 2024/17/index.js | 77 +++++++++++++++++++++++++++++++++++++++++++++ 2024/17/input.txt | 5 +++ 3 files changed, 87 insertions(+) create mode 100644 2024/17/example.txt create mode 100644 2024/17/index.js create mode 100644 2024/17/input.txt diff --git a/2024/17/example.txt b/2024/17/example.txt new file mode 100644 index 0000000..36fbf8d --- /dev/null +++ b/2024/17/example.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/2024/17/index.js b/2024/17/index.js new file mode 100644 index 0000000..6ff522c --- /dev/null +++ b/2024/17/index.js @@ -0,0 +1,77 @@ +import fs from 'node:fs' + + +function main(filename) { + const file = fs.readFileSync(filename, { encoding: "utf-8" }, data => data).split('\n') + const set = new Map(file.filter(line => line != '').map(str => str.split(':'))) + let computer = new Computer() + computer.program = set.get('Program').trim().split(',').map(Number) + computer.registers.A = parseInt(set.get('Register A').trim()) + computer.registers.B = parseInt(set.get('Register B').trim()) + computer.registers.C = parseInt(set.get('Register C').trim()) + computer.run(computer.program) +} +class Computer { + constructor() { + this.registers = { A: 0, B: 0, C: 0 } + this.output = [] + this.program = [] + this.ip = 0 + this.ops = 0 + } + + getCombo(operand) { + switch (operand) { + case 4: return this.registers.A; + case 5: return this.registers.B; + case 6: return this.registers.C; + case 7: return 0 + } + return operand + } + doOperations(opcode, operand) { + this.ops++ + switch (opcode) { + case 0: + this.registers.A = Math.floor(this.registers.A / Math.pow(2, this.getCombo(operand))) + break // division by operand^2 + case 1: + this.registers.B = (this.registers.B ^ operand); break // bitwise XOR literal operand + case 2: + this.registers.B = (this.getCombo(operand) % 8); break //mod8 combo operand + case 3: + if (this.registers.A !== 0) { + this.ip = operand + return true + } + break + case 4: + this.registers.B = (this.registers.B ^ this.registers.C); + break // bitwise XOR + case 5: + this.output.push((this.getCombo(operand) % 8)); + break // output combo + case 6: + this.registers.B = Math.floor(this.registers.A / Math.pow(2, this.getCombo(operand))); + break + case 7: + this.registers.C = Math.floor(this.registers.A / Math.pow(2, this.getCombo(operand))); + break + default: break + } + return false + } + run(program) { + + while (this.ip < program.length) { + let opcode = program[this.ip] + let operand = program[this.ip + 1] + const jumped = this.doOperations(opcode, operand) + if (!jumped) this.ip += 2 + } + console.log(this) + console.log(this.output.join(",")) + } +} + +main('input.txt') \ No newline at end of file diff --git a/2024/17/input.txt b/2024/17/input.txt new file mode 100644 index 0000000..9fc5f90 --- /dev/null +++ b/2024/17/input.txt @@ -0,0 +1,5 @@ +Register A: 64584136 +Register B: 0 +Register C: 0 + +Program: 2,4,1,2,7,5,1,3,4,3,5,5,0,3,3,0 \ No newline at end of file