Skip to content

Commit

Permalink
Day23 (#19)
Browse files Browse the repository at this point in the history
* Define task.

* Add simpleInput for test

* Add tests for parsing a line.

* Implement parsing a line

* Add more tests for processInstruction

* Implement some instructions with tests.

* Implement all instructions with tests.

* Solve task for simple input.

* Fix processInstruction

* Fix parsing instructions. Solve puzzle1

* Solve puzzle2
  • Loading branch information
lukasbicus authored Oct 10, 2024
1 parent 6fa5e76 commit f2f60a1
Show file tree
Hide file tree
Showing 5 changed files with 497 additions and 0 deletions.
47 changes: 47 additions & 0 deletions scripts/aoc2015/day23/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
jio a, +18
inc a
tpl a
inc a
tpl a
tpl a
tpl a
inc a
tpl a
inc a
tpl a
inc a
inc a
tpl a
tpl a
tpl a
inc a
jmp +22
tpl a
inc a
tpl a
inc a
inc a
tpl a
inc a
tpl a
inc a
inc a
tpl a
tpl a
inc a
inc a
tpl a
inc a
inc a
tpl a
inc a
inc a
tpl a
jio a, +8
inc b
jie a, +4
tpl a
inc a
jmp +2
hlf a
jmp -7
107 changes: 107 additions & 0 deletions scripts/aoc2015/day23/puzzle1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
--- Day 23: Opening the Turing Lock ---
Little Jane Marie just got her very first computer for Christmas from some unknown benefactor. It comes with instructions and an example program, but the computer itself seems to be malfunctioning. She's curious what the program does, and would like you to help her run it.
The manual explains that the computer supports two registers and six instructions (truly, it goes on to remind the reader, a state-of-the-art technology).
The registers are named a and b, can hold any non-negative integer, and begin with a value of 0.
The instructions are as follows:
hlf r sets register r to half its current value, then continues with the next instruction.
tpl r sets register r to triple its current value, then continues with the next instruction.
inc r increments register r, adding 1 to it, then continues with the next instruction.
jmp offset is a jump; it continues with the instruction offset away relative to itself.
jie r, offset is like jmp, but only jumps if register r is even ("jump if even").
jio r, offset is like jmp, but only jumps if register r is 1 ("jump if one", not odd).
All three jump instructions work with an offset relative to that instruction. The offset is always written with a prefix + or - to indicate the direction of the jump (forward or backward, respectively). For example, jmp +1 would simply continue with the next instruction, while jmp +0 would continuously jump back to itself forever.
The program exits when it tries to run an instruction beyond the ones defined.
For example, this program sets a to 2, because the jio instruction causes it to skip the tpl instruction:
inc a
jio a, +2
tpl a
inc a
What is the value in register b when the program in your puzzle input is finished executing?
*/

// register a, non-negative int, starts with 0
// register b, non-negative int, starts with 0

// The instructions are as follows:
//
// `hlf` r sets register r to half its current value, then continues with the next instruction.
// `tpl` r sets register r to triple its current value, then continues with the next instruction.
// `inc` r increments register r, adding 1 to it, then continues with the next instruction.
// `jmp` offset is a jump; it continues with the instruction offset away relative to itself.
// `jie` r, offset is like jmp, but only jumps if register r is even ("jump if even").
// `jio` r, offset is like jmp, but only jumps if register r is 1 ("jump if one", not odd).

// the program exits, when it tries to read instruction, that doesn't exists

// ALGORITHM

// define registers a,b
// define instructions array
// define offset (instruction, we are processing)

// program ends, when offset is lower than 0 and greater than instructions length

// read instructions, save them into an array
// create a function, that parses a line with instruction
// fill array of instructions

// console log b

import {
ComputerState,
Instruction,
parseInstructionLine,
processInstruction,
} from "./utils.ts";

async function processFile(filename: string): Promise<void> {
const input = await Deno.readTextFile(filename);
const instructions: Instruction[] = [];
for (const line of input.split("\n")) {
const instruction = parseInstructionLine(line);
if (instruction) {
instructions.push(instruction);
}
}

let computerState: ComputerState = {
a: 0,
b: 0,
offset: 0,
};

// PUZZLE 2 START
computerState = {
a: 1,
b: 0,
offset: 0,
};
// PUZZLE 2 END

// do
// start to read instructions
// each instruction will update a register (a/b) or/and update offset
// -> create function for execution of each register
do {
computerState = processInstruction(
computerState,
instructions[computerState.offset],
);
// while offset is within (0..instructions.length)
} while (
(computerState.offset < instructions.length) && (computerState.offset >= 0)
);
console.log("computer state", computerState);
}

// processFile("simpleInput.txt");
processFile("input.txt");
4 changes: 4 additions & 0 deletions scripts/aoc2015/day23/simpleInput.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
inc a
jio a, +2
tpl a
inc a
191 changes: 191 additions & 0 deletions scripts/aoc2015/day23/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
import { assertEquals, assertThrows } from "@std/assert";
import { describe, it } from "@std/testing/bdd";
import {
ComputerState,
HlfInstruction,
IncInstruction,
InstructionShortcut,
JieInstruction,
JioInstruction,
JmpInstruction,
parseInstructionLine,
processInstruction,
TplInstruction,
} from "./utils.ts";

describe("parseInstructionLine", function () {
it("should return null for invalid line", function () {
assertEquals(parseInstructionLine("invalid instruction"), null);
});

it("should return half instruction", function () {
assertEquals(parseInstructionLine("hlf a"), {
register: "a",
shortcut: InstructionShortcut.Hlf,
offsetChange: 1,
});
});

it("should return triple instruction", function () {
assertEquals(parseInstructionLine("tpl a"), {
register: "a",
shortcut: InstructionShortcut.Tpl,
offsetChange: 1,
});
});

it("should return increment instruction", function () {
assertEquals(parseInstructionLine("inc a"), {
register: "a",
shortcut: InstructionShortcut.Inc,
offsetChange: 1,
});
});

it("should return jump instruction", function () {
assertEquals(parseInstructionLine("jmp +2"), {
shortcut: InstructionShortcut.Jmp,
offsetChange: 2,
});
assertEquals(parseInstructionLine("jmp +22"), {
shortcut: InstructionShortcut.Jmp,
offsetChange: 22,
});
assertEquals(parseInstructionLine("jmp -7"), {
shortcut: InstructionShortcut.Jmp,
offsetChange: -7,
});
});

it("should return jump if even instruction", function () {
assertEquals(parseInstructionLine("jie a, +8"), {
shortcut: InstructionShortcut.Jie,
register: "a",
offsetChange: 8,
});
assertEquals(parseInstructionLine("jie b, -2"), {
shortcut: InstructionShortcut.Jie,
register: "b",
offsetChange: -2,
});
});

it("should return jump if one instruction", function () {
assertEquals(parseInstructionLine("jio a, +8"), {
shortcut: InstructionShortcut.Jio,
register: "a",
offsetChange: 8,
});
assertEquals(parseInstructionLine("jio b, -2"), {
shortcut: InstructionShortcut.Jio,
register: "b",
offsetChange: -2,
});
});
});

describe("processInstruction", function () {
const initState: ComputerState = {
a: 10,
b: 1,
offset: 10,
};
it("should process half instruction", function () {
const instruction: HlfInstruction = {
offsetChange: 1,
register: "a",
shortcut: InstructionShortcut.Hlf,
};
assertEquals(
processInstruction(initState, instruction),
{
...initState,
a: Math.floor(initState.a / 2),
offset: initState.offset + instruction.offsetChange,
},
);
});
it("should process triple instruction", function () {
const instruction: TplInstruction = {
offsetChange: 1,
register: "a",
shortcut: InstructionShortcut.Tpl,
};
assertEquals(
processInstruction(initState, instruction),
{
...initState,
a: initState.a * 3,
offset: initState.offset + instruction.offsetChange,
},
);
});
it("should process increment instruction", function () {
const instruction: IncInstruction = {
offsetChange: 1,
register: "a",
shortcut: InstructionShortcut.Inc,
};
assertEquals(
processInstruction(initState, instruction),
{
...initState,
a: initState.a + 1,
offset: initState.offset + instruction.offsetChange,
},
);
});

it("should process jump instruction", function () {
const instruction: JmpInstruction = {
offsetChange: 8,
shortcut: InstructionShortcut.Jmp,
};
assertEquals(
processInstruction(initState, instruction),
{ ...initState, offset: initState.offset + instruction.offsetChange },
);
});

it("should process jump if even instruction", function () {
const instructionA: JieInstruction = {
offsetChange: 8,
register: "a",
shortcut: InstructionShortcut.Jie,
};
const instructionB: JieInstruction = {
offsetChange: 8,
register: "b",
shortcut: InstructionShortcut.Jie,
};
assertEquals(
processInstruction(initState, instructionA),
{ ...initState, offset: initState.offset + instructionA.offsetChange },
);
assertEquals(
processInstruction(initState, instructionB),
{ ...initState, offset: initState.offset + 1 },
);
});

it("should process jump if one instruction", function () {
const instructionA: JioInstruction = {
offsetChange: 8,
register: "a",
shortcut: InstructionShortcut.Jio,
};
const instructionB: JioInstruction = {
offsetChange: 8,
register: "b",
shortcut: InstructionShortcut.Jio,
};
assertEquals(
processInstruction(initState, instructionA),
{ ...initState, offset: initState.offset + 1 },
);
assertEquals(
processInstruction(initState, instructionB),
{ ...initState, offset: initState.offset + instructionA.offsetChange },
);
});
});
Loading

0 comments on commit f2f60a1

Please sign in to comment.