TypeScript Day 11
This commit is contained in:
parent
c33fa5b120
commit
9f577ac2f8
9 changed files with 156415 additions and 0 deletions
147392
2019/JavaScript-TypeScript/day11/.yarn/releases/yarn-1.22.10.cjs
vendored
Normal file
147392
2019/JavaScript-TypeScript/day11/.yarn/releases/yarn-1.22.10.cjs
vendored
Normal file
File diff suppressed because one or more lines are too long
5
2019/JavaScript-TypeScript/day11/.yarnrc
Normal file
5
2019/JavaScript-TypeScript/day11/.yarnrc
Normal file
|
@ -0,0 +1,5 @@
|
|||
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
|
||||
# yarn lockfile v1
|
||||
|
||||
|
||||
yarn-path ".yarn/releases/yarn-1.22.10.cjs"
|
1
2019/JavaScript-TypeScript/day11/input
Normal file
1
2019/JavaScript-TypeScript/day11/input
Normal file
|
@ -0,0 +1 @@
|
|||
3,8,1005,8,305,1106,0,11,0,0,0,104,1,104,0,3,8,1002,8,-1,10,101,1,10,10,4,10,1008,8,0,10,4,10,1002,8,1,29,3,8,102,-1,8,10,1001,10,1,10,4,10,108,1,8,10,4,10,1002,8,1,50,1,104,20,10,1,1102,6,10,1006,0,13,3,8,102,-1,8,10,101,1,10,10,4,10,108,1,8,10,4,10,102,1,8,83,1,1102,0,10,1006,0,96,2,1004,19,10,3,8,1002,8,-1,10,101,1,10,10,4,10,108,0,8,10,4,10,101,0,8,116,3,8,1002,8,-1,10,1001,10,1,10,4,10,108,1,8,10,4,10,102,1,8,138,1006,0,60,1,1008,12,10,3,8,102,-1,8,10,101,1,10,10,4,10,1008,8,0,10,4,10,102,1,8,168,1006,0,14,1006,0,28,3,8,1002,8,-1,10,1001,10,1,10,4,10,108,0,8,10,4,10,101,0,8,195,2,1005,9,10,1006,0,29,3,8,1002,8,-1,10,101,1,10,10,4,10,108,1,8,10,4,10,1002,8,1,224,2,1009,8,10,1,3,5,10,3,8,1002,8,-1,10,101,1,10,10,4,10,108,1,8,10,4,10,102,1,8,254,3,8,102,-1,8,10,1001,10,1,10,4,10,1008,8,0,10,4,10,1002,8,1,277,1,1003,18,10,1,1104,1,10,101,1,9,9,1007,9,957,10,1005,10,15,99,109,627,104,0,104,1,21101,0,666681062292,1,21102,322,1,0,1105,1,426,21101,847073883028,0,1,21102,333,1,0,1105,1,426,3,10,104,0,104,1,3,10,104,0,104,0,3,10,104,0,104,1,3,10,104,0,104,1,3,10,104,0,104,0,3,10,104,0,104,1,21101,0,179356855319,1,21102,1,380,0,1105,1,426,21102,1,179356998696,1,21102,1,391,0,1105,1,426,3,10,104,0,104,0,3,10,104,0,104,0,21101,0,988669698816,1,21101,0,414,0,1106,0,426,21102,1,868494500628,1,21102,425,1,0,1106,0,426,99,109,2,21202,-1,1,1,21102,1,40,2,21102,457,1,3,21102,1,447,0,1105,1,490,109,-2,2105,1,0,0,1,0,0,1,109,2,3,10,204,-1,1001,452,453,468,4,0,1001,452,1,452,108,4,452,10,1006,10,484,1102,0,1,452,109,-2,2105,1,0,0,109,4,1201,-1,0,489,1207,-3,0,10,1006,10,507,21102,0,1,-3,22101,0,-3,1,21202,-2,1,2,21101,1,0,3,21102,1,526,0,1106,0,531,109,-4,2105,1,0,109,5,1207,-3,1,10,1006,10,554,2207,-4,-2,10,1006,10,554,22101,0,-4,-4,1106,0,622,21201,-4,0,1,21201,-3,-1,2,21202,-2,2,3,21102,573,1,0,1106,0,531,21202,1,1,-4,21101,1,0,-1,2207,-4,-2,10,1006,10,592,21102,1,0,-1,22202,-2,-1,-2,2107,0,-3,10,1006,10,614,22101,0,-1,1,21102,614,1,0,105,1,489,21202,-2,-1,-2,22201,-4,-2,-4,109,-5,2105,1,0
|
8094
2019/JavaScript-TypeScript/day11/out.log
Normal file
8094
2019/JavaScript-TypeScript/day11/out.log
Normal file
File diff suppressed because it is too large
Load diff
16
2019/JavaScript-TypeScript/day11/package.json
Normal file
16
2019/JavaScript-TypeScript/day11/package.json
Normal file
|
@ -0,0 +1,16 @@
|
|||
{
|
||||
"scripts": {
|
||||
"lint": "prettier solution_a.ts solution_b.ts --write --print-width=120",
|
||||
"a": "ts-node solution_a.ts",
|
||||
"b": "ts-node solution_b.ts"
|
||||
},
|
||||
"devDependencies": {
|
||||
"prettier": "^2.1.2"
|
||||
},
|
||||
"dependencies": {
|
||||
"@types/node": "^14.14.8",
|
||||
"ts-node": "^9.0.0",
|
||||
"typescript": "^4.0.5"
|
||||
},
|
||||
"license": "WTFPL"
|
||||
}
|
385
2019/JavaScript-TypeScript/day11/solution_a.ts
Normal file
385
2019/JavaScript-TypeScript/day11/solution_a.ts
Normal file
|
@ -0,0 +1,385 @@
|
|||
import * as fs from "fs";
|
||||
import { relative } from "path";
|
||||
const fsPromises = fs.promises;
|
||||
import * as readline from "readline";
|
||||
import { factory } from "typescript";
|
||||
|
||||
const rl = readline.createInterface(process.stdin, process.stdout);
|
||||
|
||||
enum ParameterMode {
|
||||
position = 0,
|
||||
immediate = 1,
|
||||
relative = 2,
|
||||
}
|
||||
|
||||
enum Instruction {
|
||||
add = 1,
|
||||
mul = 2,
|
||||
inp = 3,
|
||||
out = 4,
|
||||
jnz = 5,
|
||||
jz = 6,
|
||||
lt = 7,
|
||||
eq = 8,
|
||||
rel = 9,
|
||||
end = 99,
|
||||
}
|
||||
|
||||
type Memory = Array<OpCode>;
|
||||
|
||||
class OpCode {
|
||||
private _executable: boolean = false;
|
||||
private _literalValue: number = 0;
|
||||
private _instruction?: Instruction;
|
||||
private _parameterModes?: [a: ParameterMode, b: ParameterMode, c: ParameterMode];
|
||||
|
||||
constructor(value: number);
|
||||
constructor(value: string);
|
||||
constructor(value: string | number) {
|
||||
let valueNumber: number;
|
||||
let valueString: string;
|
||||
if (typeof value === "number") {
|
||||
valueNumber = value;
|
||||
valueString = value.toString();
|
||||
} else {
|
||||
valueString = value;
|
||||
valueNumber = parseInt(value);
|
||||
}
|
||||
if (isNaN(valueNumber)) throw new Error(`Invalid value '${value}' for OpCode.`);
|
||||
valueString = valueString.padStart(5, "0");
|
||||
|
||||
this._literalValue = valueNumber;
|
||||
|
||||
if (valueNumber >= 0 && valueNumber <= 99999 && valueNumber % 100 in Instruction) {
|
||||
const parameterModes: [ParameterMode, ParameterMode, ParameterMode] = [
|
||||
Math.floor(valueNumber / 10000) % 10,
|
||||
Math.floor(valueNumber / 1000) % 10,
|
||||
Math.floor(valueNumber / 100) % 10,
|
||||
];
|
||||
if (parameterModes.filter((val) => val in ParameterMode).length === 3) {
|
||||
this._parameterModes = parameterModes;
|
||||
this._instruction = valueNumber % 100;
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
|
||||
this._executable = true;
|
||||
}
|
||||
}
|
||||
|
||||
get executable() {
|
||||
if (this._instruction === undefined || this._parameterModes === undefined) {
|
||||
this._executable = false;
|
||||
}
|
||||
return this._executable;
|
||||
}
|
||||
|
||||
get literalValue() {
|
||||
return this._literalValue;
|
||||
}
|
||||
get instruction() {
|
||||
return this._instruction;
|
||||
}
|
||||
get parameterModes() {
|
||||
return this._parameterModes;
|
||||
}
|
||||
|
||||
toString() {
|
||||
return this.literalValue.toString();
|
||||
}
|
||||
}
|
||||
|
||||
class Machine {
|
||||
private readonly initialMemory: Memory;
|
||||
private memory: Memory;
|
||||
constructor(program: Memory) {
|
||||
this.initialMemory = program;
|
||||
this.memory = program;
|
||||
}
|
||||
|
||||
parseParam(value: number, mode: ParameterMode, relativeOffset: number) {
|
||||
switch (mode) {
|
||||
case ParameterMode.immediate:
|
||||
return value;
|
||||
case ParameterMode.position:
|
||||
return this.memory[value]?.literalValue ?? 0;
|
||||
case ParameterMode.relative:
|
||||
return this.memory[value + relativeOffset]?.literalValue ?? 0;
|
||||
}
|
||||
}
|
||||
|
||||
async run(
|
||||
inputHandler: (context: Context) => Promise<OpCode>,
|
||||
outputHandler: (context: Context) => Promise<unknown>,
|
||||
debug: boolean = false
|
||||
) {
|
||||
let index = 0;
|
||||
let relative = 0;
|
||||
let debugIteration = 0;
|
||||
const debugLog = debug ? await fsPromises.open("out.log", "w") : undefined;
|
||||
let output: number[] = [];
|
||||
try {
|
||||
while (index < this.memory.length) {
|
||||
/*for (let i = 0; i < this.memory.length; i++) {
|
||||
this.memory[i] = this.memory[i] ?? new OpCode(0);
|
||||
}*/
|
||||
if (debug) {
|
||||
debugLog!.write(
|
||||
`${(++debugIteration).toString().padStart(5, "0")};${relative
|
||||
.toString()
|
||||
.padStart(8, "0")};${index.toString().padStart(5, "0")};[${this.memory
|
||||
.map((op) => op.literalValue)
|
||||
.join(",")}]\n`
|
||||
);
|
||||
}
|
||||
const opCode = this.memory[index];
|
||||
if (!opCode.executable) {
|
||||
throw new Error(`Tried executing non-executable value ${opCode.literalValue} at index ${index}`);
|
||||
}
|
||||
|
||||
const parameterModes = opCode.parameterModes!;
|
||||
const valueA = this.memory[index + 1]?.literalValue ?? 0;
|
||||
const valueB = this.memory[index + 2]?.literalValue ?? 0;
|
||||
const valueC = this.memory[index + 3]?.literalValue ?? 0;
|
||||
|
||||
const paramA = this.parseParam(valueA, parameterModes[2 - 0], relative);
|
||||
const paramB = this.parseParam(valueB, parameterModes[2 - 1], relative);
|
||||
const paramC = this.parseParam(valueC, parameterModes[2 - 2], relative);
|
||||
|
||||
if (debug) {
|
||||
console.debug(`${opCode.literalValue}(${valueA}, ${valueB}, ${valueC});${this.memory[1028]?.literalValue}`);
|
||||
}
|
||||
|
||||
const context: Context = {
|
||||
parameterModes,
|
||||
valueA,
|
||||
valueB,
|
||||
valueC,
|
||||
paramA,
|
||||
paramB,
|
||||
paramC,
|
||||
debug,
|
||||
index,
|
||||
relative,
|
||||
opCode,
|
||||
output,
|
||||
};
|
||||
|
||||
switch (opCode.instruction) {
|
||||
case Instruction.add:
|
||||
if (debug) {
|
||||
console.debug(
|
||||
`Adding ${paramA}+${paramB} and writing ${paramA + paramB} to ${valueC} - OpCode ${opCode.literalValue}`
|
||||
);
|
||||
}
|
||||
if (parameterModes[2 - 2] === ParameterMode.relative) {
|
||||
this.memory[valueC + relative] = new OpCode(paramA + paramB);
|
||||
} else {
|
||||
this.memory[valueC] = new OpCode(paramA + paramB);
|
||||
}
|
||||
index += 4;
|
||||
break;
|
||||
case Instruction.mul:
|
||||
if (debug) {
|
||||
console.debug(
|
||||
`Multiplying ${paramA}*${paramB} and writing ${paramA * paramB} to ${valueC} - OpCode ${
|
||||
opCode.literalValue
|
||||
}`
|
||||
);
|
||||
}
|
||||
if (parameterModes[2 - 2] === ParameterMode.relative) {
|
||||
this.memory[valueC + relative] = new OpCode(paramA * paramB);
|
||||
} else {
|
||||
this.memory[valueC] = new OpCode(paramA * paramB);
|
||||
}
|
||||
index += 4;
|
||||
break;
|
||||
case Instruction.inp:
|
||||
let input = await inputHandler(context);
|
||||
|
||||
if (parameterModes[2 - 0] === ParameterMode.relative) {
|
||||
this.memory[valueA + relative] = input;
|
||||
} else {
|
||||
this.memory[valueA] = input;
|
||||
}
|
||||
|
||||
index += 2;
|
||||
break;
|
||||
case Instruction.out:
|
||||
outputHandler(context);
|
||||
index += 2;
|
||||
break;
|
||||
case Instruction.jnz:
|
||||
if (debug) {
|
||||
console.debug(`Testing if ${paramA}!=0, if so, jumping to ${paramB} - OpCode ${opCode.literalValue}`);
|
||||
}
|
||||
if (paramA !== 0) {
|
||||
index = paramB;
|
||||
} else {
|
||||
index += 3;
|
||||
}
|
||||
break;
|
||||
case Instruction.jz:
|
||||
if (debug) {
|
||||
console.debug(`Testing if ${paramA}==0, if so, jumping to ${paramB} - OpCode ${opCode.literalValue}`);
|
||||
}
|
||||
if (paramA === 0) {
|
||||
index = paramB;
|
||||
} else {
|
||||
index += 3;
|
||||
}
|
||||
break;
|
||||
case Instruction.lt:
|
||||
if (debug) {
|
||||
console.debug(
|
||||
`Checking if ${paramA}<${paramB} and writing result to ${
|
||||
parameterModes[2 - 2] === ParameterMode.relative ? valueC + relative : valueC
|
||||
} - OpCode ${opCode.literalValue}`
|
||||
);
|
||||
}
|
||||
if (parameterModes[2 - 2] === ParameterMode.relative) {
|
||||
this.memory[valueC + relative] = new OpCode(paramA < paramB ? 1 : 0);
|
||||
} else {
|
||||
this.memory[valueC] = new OpCode(paramA < paramB ? 1 : 0);
|
||||
}
|
||||
index += 4;
|
||||
break;
|
||||
case Instruction.eq:
|
||||
if (debug) {
|
||||
console.debug(
|
||||
`Checking if ${paramA}==${paramB} and writing result to ${
|
||||
parameterModes[2 - 2] === ParameterMode.relative ? valueC + relative : valueC
|
||||
} - OpCode ${opCode.literalValue}`
|
||||
);
|
||||
}
|
||||
if (parameterModes[2 - 2] === ParameterMode.relative) {
|
||||
this.memory[valueC + relative] = new OpCode(paramA === paramB ? 1 : 0);
|
||||
} else {
|
||||
this.memory[valueC] = new OpCode(paramA === paramB ? 1 : 0);
|
||||
}
|
||||
index += 4;
|
||||
break;
|
||||
case Instruction.rel:
|
||||
if (debug) {
|
||||
console.debug(
|
||||
`Adjusting relative offset from ${relative} +${paramA} to ${relative + paramA} - OpCode ${
|
||||
opCode.literalValue
|
||||
}`
|
||||
);
|
||||
}
|
||||
relative += paramA;
|
||||
index += 2;
|
||||
break;
|
||||
case Instruction.end:
|
||||
// console.log("Final output:", output);
|
||||
return this.memory;
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
console.debug(`Execution failed at index ${index}. Dumping memory.`);
|
||||
if (debug) {
|
||||
debugLog!.write(
|
||||
`${debugIteration.toString().padStart(5, "0")};${index.toString().padStart(5, "0")};[${this.memory
|
||||
.map((op) => op.literalValue)
|
||||
.join(",")}]\n`
|
||||
);
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
// console.log("Final output:", output);
|
||||
return this.memory;
|
||||
}
|
||||
|
||||
reset() {
|
||||
this.memory = this.initialMemory;
|
||||
}
|
||||
|
||||
static memdump(memory: Memory) {
|
||||
return memory.map((op) => op.toString()).join(",");
|
||||
}
|
||||
}
|
||||
|
||||
interface Context {
|
||||
parameterModes: [ParameterMode, ParameterMode, ParameterMode];
|
||||
valueA: number;
|
||||
valueB: number;
|
||||
valueC: number;
|
||||
paramA: number;
|
||||
paramB: number;
|
||||
paramC: number;
|
||||
debug: boolean;
|
||||
index: number;
|
||||
relative: number;
|
||||
opCode: OpCode;
|
||||
output: number[];
|
||||
}
|
||||
|
||||
async function main(code: string): Promise<[number, string]> {
|
||||
let painted = new Set<string>();
|
||||
const memdump: Memory = code.split(",").map((val) => new OpCode(val));
|
||||
|
||||
const machine = new Machine(memdump);
|
||||
|
||||
async function input(context: Context) {
|
||||
if (context.debug) {
|
||||
console.debug("inputting", field[coords[1]][coords[0]] === "⬜" ? 1 : 0);
|
||||
}
|
||||
return new OpCode(field[coords[1]][coords[0]] === "⬜" ? 1 : 0);
|
||||
}
|
||||
|
||||
async function output(context: Context) {
|
||||
if (context.debug) {
|
||||
console.debug(`Outputting ${context.paramA} - OpCode ${context.opCode.literalValue}`);
|
||||
}
|
||||
context.output.push(context.paramA);
|
||||
if (awaiting === "color") {
|
||||
awaiting = "turning";
|
||||
const newColor = context.paramA === 1 ? "⬜" : "⬛";
|
||||
field[coords[1]][coords[0]] = newColor;
|
||||
} else {
|
||||
awaiting = "color";
|
||||
let turning = context.paramA === 0 ? -1 : context.paramA;
|
||||
|
||||
if (!painted.has(`${coords[0]};${coords[1]}`)) {
|
||||
painted.add(`${coords[0]};${coords[1]}`);
|
||||
}
|
||||
|
||||
facing = ((facing + turning + 4) % 4) as 0 | 1 | 2 | 3;
|
||||
switch (facing) {
|
||||
case 0:
|
||||
coords[1] = coords[1] - 1;
|
||||
break;
|
||||
case 1:
|
||||
coords[0] = coords[0] + 1;
|
||||
break;
|
||||
case 2:
|
||||
coords[1] = coords[1] + 1;
|
||||
break;
|
||||
case 3:
|
||||
coords[0] = coords[0] - 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let field: ("⬜" | "⬛")[][] = Array<("⬜" | "⬛")[]>(86)
|
||||
.fill([])
|
||||
.map((_) => []);
|
||||
let coords: [number, number] = [0, 72];
|
||||
let facing: 0 | 1 | 2 | 3 = 0;
|
||||
let awaiting: "turning" | "color" = "color";
|
||||
|
||||
await machine.run(input, output);
|
||||
rl.close();
|
||||
return [painted.size, field.map((line) => line.join("")).join("\n")];
|
||||
}
|
||||
const input = fs.readFileSync("input").toString();
|
||||
main(input)
|
||||
.then(([count, field]) => {
|
||||
console.log(count);
|
||||
// console.log(field);
|
||||
})
|
||||
.catch((...args) => {
|
||||
rl.close();
|
||||
console.error(args);
|
||||
});
|
386
2019/JavaScript-TypeScript/day11/solution_b.ts
Normal file
386
2019/JavaScript-TypeScript/day11/solution_b.ts
Normal file
|
@ -0,0 +1,386 @@
|
|||
import * as fs from "fs";
|
||||
import { relative } from "path";
|
||||
const fsPromises = fs.promises;
|
||||
import * as readline from "readline";
|
||||
import { factory } from "typescript";
|
||||
|
||||
const rl = readline.createInterface(process.stdin, process.stdout);
|
||||
|
||||
enum ParameterMode {
|
||||
position = 0,
|
||||
immediate = 1,
|
||||
relative = 2,
|
||||
}
|
||||
|
||||
enum Instruction {
|
||||
add = 1,
|
||||
mul = 2,
|
||||
inp = 3,
|
||||
out = 4,
|
||||
jnz = 5,
|
||||
jz = 6,
|
||||
lt = 7,
|
||||
eq = 8,
|
||||
rel = 9,
|
||||
end = 99,
|
||||
}
|
||||
|
||||
type Memory = Array<OpCode>;
|
||||
|
||||
class OpCode {
|
||||
private _executable: boolean = false;
|
||||
private _literalValue: number = 0;
|
||||
private _instruction?: Instruction;
|
||||
private _parameterModes?: [a: ParameterMode, b: ParameterMode, c: ParameterMode];
|
||||
|
||||
constructor(value: number);
|
||||
constructor(value: string);
|
||||
constructor(value: string | number) {
|
||||
let valueNumber: number;
|
||||
let valueString: string;
|
||||
if (typeof value === "number") {
|
||||
valueNumber = value;
|
||||
valueString = value.toString();
|
||||
} else {
|
||||
valueString = value;
|
||||
valueNumber = parseInt(value);
|
||||
}
|
||||
if (isNaN(valueNumber)) throw new Error(`Invalid value '${value}' for OpCode.`);
|
||||
valueString = valueString.padStart(5, "0");
|
||||
|
||||
this._literalValue = valueNumber;
|
||||
|
||||
if (valueNumber >= 0 && valueNumber <= 99999 && valueNumber % 100 in Instruction) {
|
||||
const parameterModes: [ParameterMode, ParameterMode, ParameterMode] = [
|
||||
Math.floor(valueNumber / 10000) % 10,
|
||||
Math.floor(valueNumber / 1000) % 10,
|
||||
Math.floor(valueNumber / 100) % 10,
|
||||
];
|
||||
if (parameterModes.filter((val) => val in ParameterMode).length === 3) {
|
||||
this._parameterModes = parameterModes;
|
||||
this._instruction = valueNumber % 100;
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
|
||||
this._executable = true;
|
||||
}
|
||||
}
|
||||
|
||||
get executable() {
|
||||
if (this._instruction === undefined || this._parameterModes === undefined) {
|
||||
this._executable = false;
|
||||
}
|
||||
return this._executable;
|
||||
}
|
||||
|
||||
get literalValue() {
|
||||
return this._literalValue;
|
||||
}
|
||||
get instruction() {
|
||||
return this._instruction;
|
||||
}
|
||||
get parameterModes() {
|
||||
return this._parameterModes;
|
||||
}
|
||||
|
||||
toString() {
|
||||
return this.literalValue.toString();
|
||||
}
|
||||
}
|
||||
|
||||
class Machine {
|
||||
private readonly initialMemory: Memory;
|
||||
private memory: Memory;
|
||||
constructor(program: Memory) {
|
||||
this.initialMemory = program;
|
||||
this.memory = program;
|
||||
}
|
||||
|
||||
parseParam(value: number, mode: ParameterMode, relativeOffset: number) {
|
||||
switch (mode) {
|
||||
case ParameterMode.immediate:
|
||||
return value;
|
||||
case ParameterMode.position:
|
||||
return this.memory[value]?.literalValue ?? 0;
|
||||
case ParameterMode.relative:
|
||||
return this.memory[value + relativeOffset]?.literalValue ?? 0;
|
||||
}
|
||||
}
|
||||
|
||||
async run(
|
||||
inputHandler: (context: Context) => Promise<OpCode>,
|
||||
outputHandler: (context: Context) => Promise<unknown>,
|
||||
debug: boolean = false
|
||||
) {
|
||||
let index = 0;
|
||||
let relative = 0;
|
||||
let debugIteration = 0;
|
||||
const debugLog = debug ? await fsPromises.open("out.log", "w") : undefined;
|
||||
let output: number[] = [];
|
||||
try {
|
||||
while (index < this.memory.length) {
|
||||
/*for (let i = 0; i < this.memory.length; i++) {
|
||||
this.memory[i] = this.memory[i] ?? new OpCode(0);
|
||||
}*/
|
||||
if (debug) {
|
||||
debugLog!.write(
|
||||
`${(++debugIteration).toString().padStart(5, "0")};${relative
|
||||
.toString()
|
||||
.padStart(8, "0")};${index.toString().padStart(5, "0")};[${this.memory
|
||||
.map((op) => op.literalValue)
|
||||
.join(",")}]\n`
|
||||
);
|
||||
}
|
||||
const opCode = this.memory[index];
|
||||
if (!opCode.executable) {
|
||||
throw new Error(`Tried executing non-executable value ${opCode.literalValue} at index ${index}`);
|
||||
}
|
||||
|
||||
const parameterModes = opCode.parameterModes!;
|
||||
const valueA = this.memory[index + 1]?.literalValue ?? 0;
|
||||
const valueB = this.memory[index + 2]?.literalValue ?? 0;
|
||||
const valueC = this.memory[index + 3]?.literalValue ?? 0;
|
||||
|
||||
const paramA = this.parseParam(valueA, parameterModes[2 - 0], relative);
|
||||
const paramB = this.parseParam(valueB, parameterModes[2 - 1], relative);
|
||||
const paramC = this.parseParam(valueC, parameterModes[2 - 2], relative);
|
||||
|
||||
if (debug) {
|
||||
console.debug(`${opCode.literalValue}(${valueA}, ${valueB}, ${valueC});${this.memory[1028]?.literalValue}`);
|
||||
}
|
||||
|
||||
const context: Context = {
|
||||
parameterModes,
|
||||
valueA,
|
||||
valueB,
|
||||
valueC,
|
||||
paramA,
|
||||
paramB,
|
||||
paramC,
|
||||
debug,
|
||||
index,
|
||||
relative,
|
||||
opCode,
|
||||
output,
|
||||
};
|
||||
|
||||
switch (opCode.instruction) {
|
||||
case Instruction.add:
|
||||
if (debug) {
|
||||
console.debug(
|
||||
`Adding ${paramA}+${paramB} and writing ${paramA + paramB} to ${valueC} - OpCode ${opCode.literalValue}`
|
||||
);
|
||||
}
|
||||
if (parameterModes[2 - 2] === ParameterMode.relative) {
|
||||
this.memory[valueC + relative] = new OpCode(paramA + paramB);
|
||||
} else {
|
||||
this.memory[valueC] = new OpCode(paramA + paramB);
|
||||
}
|
||||
index += 4;
|
||||
break;
|
||||
case Instruction.mul:
|
||||
if (debug) {
|
||||
console.debug(
|
||||
`Multiplying ${paramA}*${paramB} and writing ${paramA * paramB} to ${valueC} - OpCode ${
|
||||
opCode.literalValue
|
||||
}`
|
||||
);
|
||||
}
|
||||
if (parameterModes[2 - 2] === ParameterMode.relative) {
|
||||
this.memory[valueC + relative] = new OpCode(paramA * paramB);
|
||||
} else {
|
||||
this.memory[valueC] = new OpCode(paramA * paramB);
|
||||
}
|
||||
index += 4;
|
||||
break;
|
||||
case Instruction.inp:
|
||||
let input = await inputHandler(context);
|
||||
|
||||
if (parameterModes[2 - 0] === ParameterMode.relative) {
|
||||
this.memory[valueA + relative] = input;
|
||||
} else {
|
||||
this.memory[valueA] = input;
|
||||
}
|
||||
|
||||
index += 2;
|
||||
break;
|
||||
case Instruction.out:
|
||||
outputHandler(context);
|
||||
index += 2;
|
||||
break;
|
||||
case Instruction.jnz:
|
||||
if (debug) {
|
||||
console.debug(`Testing if ${paramA}!=0, if so, jumping to ${paramB} - OpCode ${opCode.literalValue}`);
|
||||
}
|
||||
if (paramA !== 0) {
|
||||
index = paramB;
|
||||
} else {
|
||||
index += 3;
|
||||
}
|
||||
break;
|
||||
case Instruction.jz:
|
||||
if (debug) {
|
||||
console.debug(`Testing if ${paramA}==0, if so, jumping to ${paramB} - OpCode ${opCode.literalValue}`);
|
||||
}
|
||||
if (paramA === 0) {
|
||||
index = paramB;
|
||||
} else {
|
||||
index += 3;
|
||||
}
|
||||
break;
|
||||
case Instruction.lt:
|
||||
if (debug) {
|
||||
console.debug(
|
||||
`Checking if ${paramA}<${paramB} and writing result to ${
|
||||
parameterModes[2 - 2] === ParameterMode.relative ? valueC + relative : valueC
|
||||
} - OpCode ${opCode.literalValue}`
|
||||
);
|
||||
}
|
||||
if (parameterModes[2 - 2] === ParameterMode.relative) {
|
||||
this.memory[valueC + relative] = new OpCode(paramA < paramB ? 1 : 0);
|
||||
} else {
|
||||
this.memory[valueC] = new OpCode(paramA < paramB ? 1 : 0);
|
||||
}
|
||||
index += 4;
|
||||
break;
|
||||
case Instruction.eq:
|
||||
if (debug) {
|
||||
console.debug(
|
||||
`Checking if ${paramA}==${paramB} and writing result to ${
|
||||
parameterModes[2 - 2] === ParameterMode.relative ? valueC + relative : valueC
|
||||
} - OpCode ${opCode.literalValue}`
|
||||
);
|
||||
}
|
||||
if (parameterModes[2 - 2] === ParameterMode.relative) {
|
||||
this.memory[valueC + relative] = new OpCode(paramA === paramB ? 1 : 0);
|
||||
} else {
|
||||
this.memory[valueC] = new OpCode(paramA === paramB ? 1 : 0);
|
||||
}
|
||||
index += 4;
|
||||
break;
|
||||
case Instruction.rel:
|
||||
if (debug) {
|
||||
console.debug(
|
||||
`Adjusting relative offset from ${relative} +${paramA} to ${relative + paramA} - OpCode ${
|
||||
opCode.literalValue
|
||||
}`
|
||||
);
|
||||
}
|
||||
relative += paramA;
|
||||
index += 2;
|
||||
break;
|
||||
case Instruction.end:
|
||||
// console.log("Final output:", output);
|
||||
return this.memory;
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
console.debug(`Execution failed at index ${index}. Dumping memory.`);
|
||||
if (debug) {
|
||||
debugLog!.write(
|
||||
`${debugIteration.toString().padStart(5, "0")};${index.toString().padStart(5, "0")};[${this.memory
|
||||
.map((op) => op.literalValue)
|
||||
.join(",")}]\n`
|
||||
);
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
// console.log("Final output:", output);
|
||||
return this.memory;
|
||||
}
|
||||
|
||||
reset() {
|
||||
this.memory = this.initialMemory;
|
||||
}
|
||||
|
||||
static memdump(memory: Memory) {
|
||||
return memory.map((op) => op.toString()).join(",");
|
||||
}
|
||||
}
|
||||
|
||||
interface Context {
|
||||
parameterModes: [ParameterMode, ParameterMode, ParameterMode];
|
||||
valueA: number;
|
||||
valueB: number;
|
||||
valueC: number;
|
||||
paramA: number;
|
||||
paramB: number;
|
||||
paramC: number;
|
||||
debug: boolean;
|
||||
index: number;
|
||||
relative: number;
|
||||
opCode: OpCode;
|
||||
output: number[];
|
||||
}
|
||||
|
||||
async function main(code: string): Promise<[number, string]> {
|
||||
let painted = new Set<string>();
|
||||
const memdump: Memory = code.split(",").map((val) => new OpCode(val));
|
||||
|
||||
const machine = new Machine(memdump);
|
||||
|
||||
async function input(context: Context) {
|
||||
if (context.debug) {
|
||||
console.debug("inputting", field[coords[1]][coords[0]] === "⬜" ? 1 : 0);
|
||||
}
|
||||
return new OpCode(field[coords[1]][coords[0]] === "⬜" ? 1 : 0);
|
||||
}
|
||||
|
||||
async function output(context: Context) {
|
||||
if (context.debug) {
|
||||
console.debug(`Outputting ${context.paramA} - OpCode ${context.opCode.literalValue}`);
|
||||
}
|
||||
context.output.push(context.paramA);
|
||||
if (awaiting === "color") {
|
||||
awaiting = "turning";
|
||||
const newColor = context.paramA === 1 ? "⬜" : "⬛";
|
||||
field[coords[1]][coords[0]] = newColor;
|
||||
} else {
|
||||
awaiting = "color";
|
||||
let turning = context.paramA === 0 ? -1 : context.paramA;
|
||||
|
||||
if (!painted.has(`${coords[0]};${coords[1]}`)) {
|
||||
painted.add(`${coords[0]};${coords[1]}`);
|
||||
}
|
||||
|
||||
facing = ((facing + turning + 4) % 4) as 0 | 1 | 2 | 3;
|
||||
switch (facing) {
|
||||
case 0:
|
||||
coords[1] = coords[1] - 1;
|
||||
break;
|
||||
case 1:
|
||||
coords[0] = coords[0] + 1;
|
||||
break;
|
||||
case 2:
|
||||
coords[1] = coords[1] + 1;
|
||||
break;
|
||||
case 3:
|
||||
coords[0] = coords[0] - 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let field: ("⬜" | "⬛")[][] = Array<("⬜" | "⬛")[]>(7)
|
||||
.fill([])
|
||||
.map((_) => Array(43).fill("⬛"));
|
||||
let coords: [number, number] = [0, 1];
|
||||
let facing: 0 | 1 | 2 | 3 = 0;
|
||||
let awaiting: "turning" | "color" = "color";
|
||||
field[coords[1]][coords[0]] = "⬜"
|
||||
|
||||
await machine.run(input, output);
|
||||
rl.close();
|
||||
return [painted.size, field.map((line) => line.join("")).join("\n")];
|
||||
}
|
||||
const input = fs.readFileSync("input").toString();
|
||||
main(input)
|
||||
.then(([count, field]) => {
|
||||
console.log(count);
|
||||
console.log(field);
|
||||
})
|
||||
.catch((...args) => {
|
||||
rl.close();
|
||||
console.error(args);
|
||||
});
|
69
2019/JavaScript-TypeScript/day11/tsconfig.json
Normal file
69
2019/JavaScript-TypeScript/day11/tsconfig.json
Normal file
|
@ -0,0 +1,69 @@
|
|||
{
|
||||
"compilerOptions": {
|
||||
/* Visit https://aka.ms/tsconfig.json to read more about this file */
|
||||
|
||||
/* Basic Options */
|
||||
// "incremental": true, /* Enable incremental compilation */
|
||||
"target": "ES2019", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
|
||||
"module": "CommonJS", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
|
||||
// "lib": [], /* Specify library files to be included in the compilation. */
|
||||
// "allowJs": true, /* Allow javascript files to be compiled. */
|
||||
// "checkJs": true, /* Report errors in .js files. */
|
||||
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
|
||||
// "declaration": true, /* Generates corresponding '.d.ts' file. */
|
||||
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
|
||||
"sourceMap": false, /* Generates corresponding '.map' file. */
|
||||
// "outFile": "./", /* Concatenate and emit output to single file. */
|
||||
// "outDir": "./", /* Redirect output structure to the directory. */
|
||||
// "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
|
||||
// "composite": true, /* Enable project compilation */
|
||||
// "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */
|
||||
// "removeComments": true, /* Do not emit comments to output. */
|
||||
"noEmit": true, /* Do not emit outputs. */
|
||||
// "importHelpers": true, /* Import emit helpers from 'tslib'. */
|
||||
// "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
|
||||
// "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */
|
||||
|
||||
/* Strict Type-Checking Options */
|
||||
"strict": true, /* Enable all strict type-checking options. */
|
||||
// "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
|
||||
// "strictNullChecks": true, /* Enable strict null checks. */
|
||||
// "strictFunctionTypes": true, /* Enable strict checking of function types. */
|
||||
// "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */
|
||||
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
|
||||
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
|
||||
"alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
|
||||
|
||||
/* Additional Checks */
|
||||
// "noUnusedLocals": true, /* Report errors on unused locals. */
|
||||
// "noUnusedParameters": true, /* Report errors on unused parameters. */
|
||||
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
|
||||
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
|
||||
|
||||
/* Module Resolution Options */
|
||||
"moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
|
||||
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
|
||||
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
|
||||
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
|
||||
// "typeRoots": [], /* List of folders to include type definitions from. */
|
||||
// "types": [], /* Type declaration files to be included in compilation. */
|
||||
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
|
||||
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
|
||||
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
|
||||
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
|
||||
|
||||
/* Source Map Options */
|
||||
// "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
|
||||
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
|
||||
// "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
|
||||
// "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */
|
||||
|
||||
/* Experimental Options */
|
||||
// "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
|
||||
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
|
||||
|
||||
/* Advanced Options */
|
||||
"skipLibCheck": true, /* Skip type checking of declaration files. */
|
||||
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
|
||||
}
|
||||
}
|
67
2019/JavaScript-TypeScript/day11/yarn.lock
Normal file
67
2019/JavaScript-TypeScript/day11/yarn.lock
Normal file
|
@ -0,0 +1,67 @@
|
|||
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
|
||||
# yarn lockfile v1
|
||||
|
||||
|
||||
"@types/node@^14.14.8":
|
||||
version "14.14.8"
|
||||
resolved "https://registry.yarnpkg.com/@types/node/-/node-14.14.8.tgz#2127bd81949a95c8b7d3240f3254352d72563aec"
|
||||
integrity sha512-z/5Yd59dCKI5kbxauAJgw6dLPzW+TNOItNE00PkpzNwUIEwdj/Lsqwq94H5DdYBX7C13aRA0CY32BK76+neEUA==
|
||||
|
||||
arg@^4.1.0:
|
||||
version "4.1.3"
|
||||
resolved "https://registry.yarnpkg.com/arg/-/arg-4.1.3.tgz#269fc7ad5b8e42cb63c896d5666017261c144089"
|
||||
integrity sha512-58S9QDqG0Xx27YwPSt9fJxivjYl432YCwfDMfZ+71RAqUrZef7LrKQZ3LHLOwCS4FLNBplP533Zx895SeOCHvA==
|
||||
|
||||
buffer-from@^1.0.0:
|
||||
version "1.1.1"
|
||||
resolved "https://registry.yarnpkg.com/buffer-from/-/buffer-from-1.1.1.tgz#32713bc028f75c02fdb710d7c7bcec1f2c6070ef"
|
||||
integrity sha512-MQcXEUbCKtEo7bhqEs6560Hyd4XaovZlO/k9V3hjVUF/zwW7KBVdSK4gIt/bzwS9MbR5qob+F5jusZsb0YQK2A==
|
||||
|
||||
diff@^4.0.1:
|
||||
version "4.0.2"
|
||||
resolved "https://registry.yarnpkg.com/diff/-/diff-4.0.2.tgz#60f3aecb89d5fae520c11aa19efc2bb982aade7d"
|
||||
integrity sha512-58lmxKSA4BNyLz+HHMUzlOEpg09FV+ev6ZMe3vJihgdxzgcwZ8VoEEPmALCZG9LmqfVoNMMKpttIYTVG6uDY7A==
|
||||
|
||||
make-error@^1.1.1:
|
||||
version "1.3.6"
|
||||
resolved "https://registry.yarnpkg.com/make-error/-/make-error-1.3.6.tgz#2eb2e37ea9b67c4891f684a1394799af484cf7a2"
|
||||
integrity sha512-s8UhlNe7vPKomQhC1qFelMokr/Sc3AgNbso3n74mVPA5LTZwkB9NlXf4XPamLxJE8h0gh73rM94xvwRT2CVInw==
|
||||
|
||||
prettier@^2.1.2:
|
||||
version "2.1.2"
|
||||
resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.1.2.tgz#3050700dae2e4c8b67c4c3f666cdb8af405e1ce5"
|
||||
integrity sha512-16c7K+x4qVlJg9rEbXl7HEGmQyZlG4R9AgP+oHKRMsMsuk8s+ATStlf1NpDqyBI1HpVyfjLOeMhH2LvuNvV5Vg==
|
||||
|
||||
source-map-support@^0.5.17:
|
||||
version "0.5.19"
|
||||
resolved "https://registry.yarnpkg.com/source-map-support/-/source-map-support-0.5.19.tgz#a98b62f86dcaf4f67399648c085291ab9e8fed61"
|
||||
integrity sha512-Wonm7zOCIJzBGQdB+thsPar0kYuCIzYvxZwlBa87yi/Mdjv7Tip2cyVbLj5o0cFPN4EVkuTwb3GDDyUx2DGnGw==
|
||||
dependencies:
|
||||
buffer-from "^1.0.0"
|
||||
source-map "^0.6.0"
|
||||
|
||||
source-map@^0.6.0:
|
||||
version "0.6.1"
|
||||
resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.6.1.tgz#74722af32e9614e9c287a8d0bbde48b5e2f1a263"
|
||||
integrity sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==
|
||||
|
||||
ts-node@^9.0.0:
|
||||
version "9.0.0"
|
||||
resolved "https://registry.yarnpkg.com/ts-node/-/ts-node-9.0.0.tgz#e7699d2a110cc8c0d3b831715e417688683460b3"
|
||||
integrity sha512-/TqB4SnererCDR/vb4S/QvSZvzQMJN8daAslg7MeaiHvD8rDZsSfXmNeNumyZZzMned72Xoq/isQljYSt8Ynfg==
|
||||
dependencies:
|
||||
arg "^4.1.0"
|
||||
diff "^4.0.1"
|
||||
make-error "^1.1.1"
|
||||
source-map-support "^0.5.17"
|
||||
yn "3.1.1"
|
||||
|
||||
typescript@^4.0.5:
|
||||
version "4.0.5"
|
||||
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.0.5.tgz#ae9dddfd1069f1cb5beb3ef3b2170dd7c1332389"
|
||||
integrity sha512-ywmr/VrTVCmNTJ6iV2LwIrfG1P+lv6luD8sUJs+2eI9NLGigaN+nUQc13iHqisq7bra9lnmUSYqbJvegraBOPQ==
|
||||
|
||||
yn@3.1.1:
|
||||
version "3.1.1"
|
||||
resolved "https://registry.yarnpkg.com/yn/-/yn-3.1.1.tgz#1e87401a09d767c1d5eab26a6e4c185182d2eb50"
|
||||
integrity sha512-Ux4ygGWsu2c7isFWe8Yu1YluJmqVhxqK2cLXNQA5AcC3QfbGNpM7fu0Y8b/z16pXLnFxZYvWhd3fhBY9DLmC6Q==
|
Loading…
Reference in a new issue