aoc 4+5
This commit is contained in:
parent
d3bfa9ff0c
commit
7998f74810
30
www/statics/aoc/2019/04_challenge.txt
Normal file
30
www/statics/aoc/2019/04_challenge.txt
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
--- Day 4: Secure Container ---
|
||||||
|
|
||||||
|
You arrive at the Venus fuel depot only to discover it's protected by a password. The Elves had written the password on a sticky note, but someone threw it out.
|
||||||
|
|
||||||
|
However, they do remember a few key facts about the password:
|
||||||
|
|
||||||
|
It is a six-digit number.
|
||||||
|
The value is within the range given in your puzzle input.
|
||||||
|
Two adjacent digits are the same (like 22 in 122345).
|
||||||
|
Going from left to right, the digits never decrease; they only ever increase or stay the same (like 111123 or 135679).
|
||||||
|
|
||||||
|
Other than the range rule, the following are true:
|
||||||
|
|
||||||
|
111111 meets these criteria (double 11, never decreases).
|
||||||
|
223450 does not meet these criteria (decreasing pair of digits 50).
|
||||||
|
123789 does not meet these criteria (no double).
|
||||||
|
|
||||||
|
How many different passwords within the range given in your puzzle input meet these criteria?
|
||||||
|
|
||||||
|
--- Part Two ---
|
||||||
|
|
||||||
|
An Elf just remembered one more important detail: the two adjacent matching digits are not part of a larger group of matching digits.
|
||||||
|
|
||||||
|
Given this additional criterion, but still ignoring the range rule, the following are now true:
|
||||||
|
|
||||||
|
112233 meets these criteria because the digits never decrease and all repeated digits are exactly two digits long.
|
||||||
|
123444 no longer meets the criteria (the repeated 44 is part of a larger group of 444).
|
||||||
|
111122 meets the criteria (even though 1 is repeated more than twice, it still contains a double 22).
|
||||||
|
|
||||||
|
How many different passwords within the range given in your puzzle input meet all of the criteria?
|
1
www/statics/aoc/2019/04_input.txt
Normal file
1
www/statics/aoc/2019/04_input.txt
Normal file
@ -0,0 +1 @@
|
|||||||
|
256310-732736
|
71
www/statics/aoc/2019/04_solution-1.ts
Normal file
71
www/statics/aoc/2019/04_solution-1.ts
Normal file
@ -0,0 +1,71 @@
|
|||||||
|
namespace AdventOfCode2019_04_1
|
||||||
|
{
|
||||||
|
const DAY = 4;
|
||||||
|
const PROBLEM = 1;
|
||||||
|
|
||||||
|
export async function run()
|
||||||
|
{
|
||||||
|
let input = await AdventOfCode.getInput(DAY);
|
||||||
|
if (input == null) return;
|
||||||
|
|
||||||
|
const min = parseInt(input.split('-')[0]);
|
||||||
|
const max = parseInt(input.split('-')[1]);
|
||||||
|
|
||||||
|
let rcount = 0;
|
||||||
|
|
||||||
|
for(let d0=0; d0<=9; d0++)
|
||||||
|
{
|
||||||
|
const v0 = d0 * 100000
|
||||||
|
|
||||||
|
if (v0+99999 <= min) continue;
|
||||||
|
if (v0 >= max) continue;
|
||||||
|
|
||||||
|
for(let d1=d0; d1<=9; d1++)
|
||||||
|
{
|
||||||
|
const v1 = v0 + d1 * 10000;
|
||||||
|
|
||||||
|
if (v1+9999 <= min) continue;
|
||||||
|
if (v1 >= max) continue;
|
||||||
|
|
||||||
|
for(let d2=d1; d2<=9; d2++)
|
||||||
|
{
|
||||||
|
const v2 = v1 + d2 * 1000;
|
||||||
|
|
||||||
|
if (v2+999 <= min) continue;
|
||||||
|
if (v2 >= max) continue;
|
||||||
|
|
||||||
|
for(let d3=d2; d3<=9; d3++)
|
||||||
|
{
|
||||||
|
const v3 = v2 + d3 * 100;
|
||||||
|
|
||||||
|
if (v3+99 <= min) continue;
|
||||||
|
if (v3 >= max) continue;
|
||||||
|
|
||||||
|
for(let d4=d3; d4<=9; d4++)
|
||||||
|
{
|
||||||
|
const v4 = v3 + d4 * 10;
|
||||||
|
|
||||||
|
if (v4+9 <= min) continue;
|
||||||
|
if (v4 >= max) continue;
|
||||||
|
|
||||||
|
for(let d5=d4; d5<=9; d5++)
|
||||||
|
{
|
||||||
|
const v5 = v4 + d5 * 1;
|
||||||
|
|
||||||
|
if (v5 <= min) continue;
|
||||||
|
if (v5 >= max) continue;
|
||||||
|
|
||||||
|
if (!(d0==d1 || d1==d2 || d2==d3 || d3==d4 || d4==d5)) continue;
|
||||||
|
|
||||||
|
rcount++;
|
||||||
|
console.log(v5);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
AdventOfCode.output(DAY, PROBLEM, rcount.toString());
|
||||||
|
}
|
||||||
|
}
|
82
www/statics/aoc/2019/04_solution-2.ts
Normal file
82
www/statics/aoc/2019/04_solution-2.ts
Normal file
@ -0,0 +1,82 @@
|
|||||||
|
namespace AdventOfCode2019_04_2
|
||||||
|
{
|
||||||
|
const DAY = 4;
|
||||||
|
const PROBLEM = 2;
|
||||||
|
|
||||||
|
export async function run()
|
||||||
|
{
|
||||||
|
let input = await AdventOfCode.getInput(DAY);
|
||||||
|
if (input == null) return;
|
||||||
|
|
||||||
|
const min = parseInt(input.split('-')[0]);
|
||||||
|
const max = parseInt(input.split('-')[1]);
|
||||||
|
|
||||||
|
let rcount = 0;
|
||||||
|
|
||||||
|
for(let d0=0; d0<=9; d0++)
|
||||||
|
{
|
||||||
|
const v0 = d0 * 100000
|
||||||
|
|
||||||
|
if (v0+99999 <= min) continue;
|
||||||
|
if (v0 >= max) continue;
|
||||||
|
|
||||||
|
for(let d1=d0; d1<=9; d1++)
|
||||||
|
{
|
||||||
|
const v1 = v0 + d1 * 10000;
|
||||||
|
|
||||||
|
if (v1+9999 <= min) continue;
|
||||||
|
if (v1 >= max) continue;
|
||||||
|
|
||||||
|
for(let d2=d1; d2<=9; d2++)
|
||||||
|
{
|
||||||
|
const v2 = v1 + d2 * 1000;
|
||||||
|
|
||||||
|
if (v2+999 <= min) continue;
|
||||||
|
if (v2 >= max) continue;
|
||||||
|
|
||||||
|
for(let d3=d2; d3<=9; d3++)
|
||||||
|
{
|
||||||
|
const v3 = v2 + d3 * 100;
|
||||||
|
|
||||||
|
if (v3+99 <= min) continue;
|
||||||
|
if (v3 >= max) continue;
|
||||||
|
|
||||||
|
for(let d4=d3; d4<=9; d4++)
|
||||||
|
{
|
||||||
|
const v4 = v3 + d4 * 10;
|
||||||
|
|
||||||
|
if (v4+9 <= min) continue;
|
||||||
|
if (v4 >= max) continue;
|
||||||
|
|
||||||
|
for(let d5=d4; d5<=9; d5++)
|
||||||
|
{
|
||||||
|
const v5 = v4 + d5 * 1;
|
||||||
|
|
||||||
|
if (v5 <= min) continue;
|
||||||
|
if (v5 >= max) continue;
|
||||||
|
|
||||||
|
if (!eq(d0, d1, d2, d3, d4, d5)) continue;
|
||||||
|
|
||||||
|
rcount++;
|
||||||
|
console.log(v5);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
AdventOfCode.output(DAY, PROBLEM, rcount.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
function eq(a:number, b: number, c: number, d: number, e: number, f: number): boolean
|
||||||
|
{
|
||||||
|
if ( a==b && b!=c) return true;
|
||||||
|
if (a!=b && b==c && c!=d) return true;
|
||||||
|
if (b!=c && c==d && d!=e) return true;
|
||||||
|
if (c!=d && d==e && e!=f) return true;
|
||||||
|
if (d!=e && e==f ) return true;
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
91
www/statics/aoc/2019/05_challenge.txt
Normal file
91
www/statics/aoc/2019/05_challenge.txt
Normal file
@ -0,0 +1,91 @@
|
|||||||
|
--- Day 5: Sunny with a Chance of Asteroids ---
|
||||||
|
|
||||||
|
You're starting to sweat as the ship makes its way toward Mercury. The Elves suggest that you get the air conditioner working by upgrading your ship computer to support the Thermal Environment Supervision Terminal.
|
||||||
|
|
||||||
|
The Thermal Environment Supervision Terminal (TEST) starts by running a diagnostic program (your puzzle input). The TEST diagnostic program will run on your existing Intcode computer after a few modifications:
|
||||||
|
|
||||||
|
First, you'll need to add two new instructions:
|
||||||
|
|
||||||
|
Opcode 3 takes a single integer as input and saves it to the position given by its only parameter. For example, the instruction 3,50 would take an input value and store it at address 50.
|
||||||
|
Opcode 4 outputs the value of its only parameter. For example, the instruction 4,50 would output the value at address 50.
|
||||||
|
|
||||||
|
Programs that use these instructions will come with documentation that explains what should be connected to the input and output. The program 3,0,4,0,99 outputs whatever it gets as input, then halts.
|
||||||
|
|
||||||
|
Second, you'll need to add support for parameter modes:
|
||||||
|
|
||||||
|
Each parameter of an instruction is handled based on its parameter mode. Right now, your ship computer already understands parameter mode 0, position mode, which causes the parameter to be interpreted as a position - if the parameter is 50, its value is the value stored at address 50 in memory. Until now, all parameters have been in position mode.
|
||||||
|
|
||||||
|
Now, your ship computer will also need to handle parameters in mode 1, immediate mode. In immediate mode, a parameter is interpreted as a value - if the parameter is 50, its value is simply 50.
|
||||||
|
|
||||||
|
Parameter modes are stored in the same value as the instruction's opcode. The opcode is a two-digit number based only on the ones and tens digit of the value, that is, the opcode is the rightmost two digits of the first value in an instruction. Parameter modes are single digits, one per parameter, read right-to-left from the opcode: the first parameter's mode is in the hundreds digit, the second parameter's mode is in the thousands digit, the third parameter's mode is in the ten-thousands digit, and so on. Any missing modes are 0.
|
||||||
|
|
||||||
|
For example, consider the program 1002,4,3,4,33.
|
||||||
|
|
||||||
|
The first instruction, 1002,4,3,4, is a multiply instruction - the rightmost two digits of the first value, 02, indicate opcode 2, multiplication. Then, going right to left, the parameter modes are 0 (hundreds digit), 1 (thousands digit), and 0 (ten-thousands digit, not present and therefore zero):
|
||||||
|
|
||||||
|
ABCDE
|
||||||
|
1002
|
||||||
|
|
||||||
|
DE - two-digit opcode, 02 == opcode 2
|
||||||
|
C - mode of 1st parameter, 0 == position mode
|
||||||
|
B - mode of 2nd parameter, 1 == immediate mode
|
||||||
|
A - mode of 3rd parameter, 0 == position mode,
|
||||||
|
omitted due to being a leading zero
|
||||||
|
|
||||||
|
This instruction multiplies its first two parameters. The first parameter, 4 in position mode, works like it did before - its value is the value stored at address 4 (33). The second parameter, 3 in immediate mode, simply has value 3. The result of this operation, 33 * 3 = 99, is written according to the third parameter, 4 in position mode, which also works like it did before - 99 is written to address 4.
|
||||||
|
|
||||||
|
Parameters that an instruction writes to will never be in immediate mode.
|
||||||
|
|
||||||
|
Finally, some notes:
|
||||||
|
|
||||||
|
It is important to remember that the instruction pointer should increase by the number of values in the instruction after the instruction finishes. Because of the new instructions, this amount is no longer always 4.
|
||||||
|
Integers can be negative: 1101,100,-1,4,0 is a valid program (find 100 + -1, store the result in position 4).
|
||||||
|
|
||||||
|
The TEST diagnostic program will start by requesting from the user the ID of the system to test by running an input instruction - provide it 1, the ID for the ship's air conditioner unit.
|
||||||
|
|
||||||
|
It will then perform a series of diagnostic tests confirming that various parts of the Intcode computer, like parameter modes, function correctly. For each test, it will run an output instruction indicating how far the result of the test was from the expected value, where 0 means the test was successful. Non-zero outputs mean that a function is not working correctly; check the instructions that were run before the output instruction to see which one failed.
|
||||||
|
|
||||||
|
Finally, the program will output a diagnostic code and immediately halt. This final output isn't an error; an output followed immediately by a halt means the program finished. If all outputs were zero except the diagnostic code, the diagnostic program ran successfully.
|
||||||
|
|
||||||
|
After providing 1 to the only input instruction and passing all the tests, what diagnostic code does the program produce?
|
||||||
|
|
||||||
|
--- Part Two ---
|
||||||
|
|
||||||
|
The air conditioner comes online! Its cold air feels good for a while, but then the TEST alarms start to go off. Since the air conditioner can't vent its heat anywhere but back into the spacecraft, it's actually making the air inside the ship warmer.
|
||||||
|
|
||||||
|
Instead, you'll need to use the TEST to extend the thermal radiators. Fortunately, the diagnostic program (your puzzle input) is already equipped for this. Unfortunately, your Intcode computer is not.
|
||||||
|
|
||||||
|
Your computer is only missing a few opcodes:
|
||||||
|
|
||||||
|
Opcode 5 is jump-if-true: if the first parameter is non-zero, it sets the instruction pointer to the value from the second parameter. Otherwise, it does nothing.
|
||||||
|
Opcode 6 is jump-if-false: if the first parameter is zero, it sets the instruction pointer to the value from the second parameter. Otherwise, it does nothing.
|
||||||
|
Opcode 7 is less than: if the first parameter is less than the second parameter, it stores 1 in the position given by the third parameter. Otherwise, it stores 0.
|
||||||
|
Opcode 8 is equals: if the first parameter is equal to the second parameter, it stores 1 in the position given by the third parameter. Otherwise, it stores 0.
|
||||||
|
|
||||||
|
Like all instructions, these instructions need to support parameter modes as described above.
|
||||||
|
|
||||||
|
Normally, after an instruction is finished, the instruction pointer increases by the number of values in that instruction. However, if the instruction modifies the instruction pointer, that value is used and the instruction pointer is not automatically increased.
|
||||||
|
|
||||||
|
For example, here are several programs that take one input, compare it to the value 8, and then produce one output:
|
||||||
|
|
||||||
|
3,9,8,9,10,9,4,9,99,-1,8 - Using position mode, consider whether the input is equal to 8; output 1 (if it is) or 0 (if it is not).
|
||||||
|
3,9,7,9,10,9,4,9,99,-1,8 - Using position mode, consider whether the input is less than 8; output 1 (if it is) or 0 (if it is not).
|
||||||
|
3,3,1108,-1,8,3,4,3,99 - Using immediate mode, consider whether the input is equal to 8; output 1 (if it is) or 0 (if it is not).
|
||||||
|
3,3,1107,-1,8,3,4,3,99 - Using immediate mode, consider whether the input is less than 8; output 1 (if it is) or 0 (if it is not).
|
||||||
|
|
||||||
|
Here are some jump tests that take an input, then output 0 if the input was zero or 1 if the input was non-zero:
|
||||||
|
|
||||||
|
3,12,6,12,15,1,13,14,13,4,13,99,-1,0,1,9 (using position mode)
|
||||||
|
3,3,1105,-1,9,1101,0,0,12,4,12,99,1 (using immediate mode)
|
||||||
|
|
||||||
|
Here's a larger example:
|
||||||
|
|
||||||
|
3,21,1008,21,8,20,1005,20,22,107,8,21,20,1006,20,31,
|
||||||
|
1106,0,36,98,0,0,1002,21,125,20,4,20,1105,1,46,104,
|
||||||
|
999,1105,1,46,1101,1000,1,20,4,20,1105,1,46,98,99
|
||||||
|
|
||||||
|
The above example program uses an input instruction to ask for a single number. The program will then output 999 if the input value is below 8, output 1000 if the input value is equal to 8, or output 1001 if the input value is greater than 8.
|
||||||
|
|
||||||
|
This time, when the TEST diagnostic program runs its input instruction to get the ID of the system to test, provide it 5, the ID for the ship's thermal radiator controller. This diagnostic test suite only outputs one number, the diagnostic code.
|
||||||
|
|
||||||
|
What is the diagnostic code for system ID 5?
|
1
www/statics/aoc/2019/05_input.txt
Normal file
1
www/statics/aoc/2019/05_input.txt
Normal file
@ -0,0 +1 @@
|
|||||||
|
3,225,1,225,6,6,1100,1,238,225,104,0,1101,82,10,225,101,94,44,224,101,-165,224,224,4,224,1002,223,8,223,101,3,224,224,1,224,223,223,1102,35,77,225,1102,28,71,225,1102,16,36,225,102,51,196,224,101,-3468,224,224,4,224,102,8,223,223,1001,224,7,224,1,223,224,223,1001,48,21,224,101,-57,224,224,4,224,1002,223,8,223,101,6,224,224,1,223,224,223,2,188,40,224,1001,224,-5390,224,4,224,1002,223,8,223,101,2,224,224,1,224,223,223,1101,9,32,224,101,-41,224,224,4,224,1002,223,8,223,1001,224,2,224,1,223,224,223,1102,66,70,225,1002,191,28,224,101,-868,224,224,4,224,102,8,223,223,101,5,224,224,1,224,223,223,1,14,140,224,101,-80,224,224,4,224,1002,223,8,223,101,2,224,224,1,224,223,223,1102,79,70,225,1101,31,65,225,1101,11,68,225,1102,20,32,224,101,-640,224,224,4,224,1002,223,8,223,1001,224,5,224,1,224,223,223,4,223,99,0,0,0,677,0,0,0,0,0,0,0,0,0,0,0,1105,0,99999,1105,227,247,1105,1,99999,1005,227,99999,1005,0,256,1105,1,99999,1106,227,99999,1106,0,265,1105,1,99999,1006,0,99999,1006,227,274,1105,1,99999,1105,1,280,1105,1,99999,1,225,225,225,1101,294,0,0,105,1,0,1105,1,99999,1106,0,300,1105,1,99999,1,225,225,225,1101,314,0,0,106,0,0,1105,1,99999,8,226,226,224,1002,223,2,223,1006,224,329,101,1,223,223,1008,677,677,224,102,2,223,223,1006,224,344,101,1,223,223,1107,226,677,224,102,2,223,223,1005,224,359,101,1,223,223,1008,226,226,224,1002,223,2,223,1006,224,374,1001,223,1,223,1108,677,226,224,1002,223,2,223,1006,224,389,1001,223,1,223,7,677,226,224,1002,223,2,223,1006,224,404,101,1,223,223,7,226,226,224,1002,223,2,223,1005,224,419,101,1,223,223,8,226,677,224,1002,223,2,223,1006,224,434,1001,223,1,223,7,226,677,224,1002,223,2,223,1006,224,449,1001,223,1,223,107,226,677,224,1002,223,2,223,1005,224,464,1001,223,1,223,1007,677,677,224,102,2,223,223,1005,224,479,101,1,223,223,1007,226,226,224,102,2,223,223,1005,224,494,1001,223,1,223,1108,226,677,224,102,2,223,223,1005,224,509,101,1,223,223,1008,677,226,224,102,2,223,223,1005,224,524,1001,223,1,223,1007,677,226,224,102,2,223,223,1005,224,539,101,1,223,223,1108,226,226,224,1002,223,2,223,1005,224,554,101,1,223,223,108,226,226,224,102,2,223,223,1005,224,569,101,1,223,223,108,677,677,224,102,2,223,223,1005,224,584,101,1,223,223,1107,226,226,224,1002,223,2,223,1006,224,599,101,1,223,223,8,677,226,224,1002,223,2,223,1006,224,614,1001,223,1,223,108,677,226,224,102,2,223,223,1006,224,629,1001,223,1,223,1107,677,226,224,1002,223,2,223,1006,224,644,1001,223,1,223,107,677,677,224,102,2,223,223,1005,224,659,101,1,223,223,107,226,226,224,102,2,223,223,1006,224,674,1001,223,1,223,4,223,99,226
|
145
www/statics/aoc/2019/05_solution-1.ts
Normal file
145
www/statics/aoc/2019/05_solution-1.ts
Normal file
@ -0,0 +1,145 @@
|
|||||||
|
namespace AdventOfCode2019_05_1
|
||||||
|
{
|
||||||
|
const DAY = 5;
|
||||||
|
const PROBLEM = 1;
|
||||||
|
|
||||||
|
export async function run()
|
||||||
|
{
|
||||||
|
let input = await AdventOfCode.getInput(DAY);
|
||||||
|
if (input == null) return;
|
||||||
|
|
||||||
|
let runner = new Interpreter(input.split(",").map(p => parseInt(p.trim())), [1]);
|
||||||
|
|
||||||
|
runner.run();
|
||||||
|
|
||||||
|
AdventOfCode.output(DAY, PROBLEM, runner.output.reverse()[0].toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
class Interpreter
|
||||||
|
{
|
||||||
|
program: number[];
|
||||||
|
input: number[];
|
||||||
|
instructionpointer: number;
|
||||||
|
inputpointer: number;
|
||||||
|
output: number[];
|
||||||
|
|
||||||
|
constructor(prog: number[], input: number[])
|
||||||
|
{
|
||||||
|
this.program = prog;
|
||||||
|
this.input = input;
|
||||||
|
this.instructionpointer = 0;
|
||||||
|
this.inputpointer = 0;
|
||||||
|
this.output = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
run()
|
||||||
|
{
|
||||||
|
while(true)
|
||||||
|
{
|
||||||
|
const cmd = new Op(this.program[this.instructionpointer]);
|
||||||
|
if (cmd.opcode == OpCode.ADD)
|
||||||
|
{
|
||||||
|
const p0 = cmd.getParameter(this, 0);
|
||||||
|
const p1 = cmd.getParameter(this, 1);
|
||||||
|
const pv = p0 + p1;
|
||||||
|
cmd.setParameter(this, 2, pv);
|
||||||
|
}
|
||||||
|
else if (cmd.opcode == OpCode.MUL)
|
||||||
|
{
|
||||||
|
const p0 = cmd.getParameter(this, 0);
|
||||||
|
const p1 = cmd.getParameter(this, 1);
|
||||||
|
const pv = p0 * p1;
|
||||||
|
cmd.setParameter(this, 2, pv);
|
||||||
|
}
|
||||||
|
else if (cmd.opcode == OpCode.HALT)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
else if (cmd.opcode == OpCode.IN)
|
||||||
|
{
|
||||||
|
const pv = this.input[this.inputpointer];
|
||||||
|
cmd.setParameter(this, 0, pv);
|
||||||
|
this.inputpointer++;
|
||||||
|
}
|
||||||
|
else if (cmd.opcode == OpCode.OUT)
|
||||||
|
{
|
||||||
|
const p0 = cmd.getParameter(this, 0);
|
||||||
|
this.output.push(p0);
|
||||||
|
console.log("# " + p0);
|
||||||
|
}
|
||||||
|
else throw "Unknown Op: " + cmd.opcode + " @ " + this.instructionpointer;
|
||||||
|
|
||||||
|
this.instructionpointer += 1 + cmd.parametercount;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
enum OpCode
|
||||||
|
{
|
||||||
|
ADD = 1,
|
||||||
|
MUL = 2,
|
||||||
|
IN = 3,
|
||||||
|
OUT = 4,
|
||||||
|
HALT = 99,
|
||||||
|
}
|
||||||
|
|
||||||
|
enum ParamMode
|
||||||
|
{
|
||||||
|
POSITION_MODE = 0,
|
||||||
|
IMMEDIATE_MODE = 1,
|
||||||
|
}
|
||||||
|
|
||||||
|
class Op
|
||||||
|
{
|
||||||
|
opcode: OpCode;
|
||||||
|
modes: ParamMode[];
|
||||||
|
|
||||||
|
name: string;
|
||||||
|
parametercount: number;
|
||||||
|
|
||||||
|
constructor(v: number)
|
||||||
|
{
|
||||||
|
this.opcode = v%100;
|
||||||
|
v = Math.floor(v/100);
|
||||||
|
this.modes = [];
|
||||||
|
for(let i=0; i<4; i++)
|
||||||
|
{
|
||||||
|
this.modes.push(v%10);
|
||||||
|
v = Math.floor(v/10);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.opcode == OpCode.ADD) { this.name="ADD"; this.parametercount=3; }
|
||||||
|
else if (this.opcode == OpCode.MUL) { this.name="MUL"; this.parametercount=3; }
|
||||||
|
else if (this.opcode == OpCode.HALT) { this.name="HALT"; this.parametercount=0; }
|
||||||
|
else if (this.opcode == OpCode.IN) { this.name="IN"; this.parametercount=1; }
|
||||||
|
else if (this.opcode == OpCode.OUT) { this.name="OUT"; this.parametercount=1; }
|
||||||
|
else throw "Unknown opcode: "+this.opcode;
|
||||||
|
}
|
||||||
|
|
||||||
|
getParameter(proc: Interpreter, index: number): number
|
||||||
|
{
|
||||||
|
const prog = proc.program;
|
||||||
|
const ip = proc.instructionpointer;
|
||||||
|
|
||||||
|
let p = prog[ip+1+index];
|
||||||
|
|
||||||
|
if (this.modes[index] == ParamMode.POSITION_MODE) p = prog[p];
|
||||||
|
else if (this.modes[index] == ParamMode.IMMEDIATE_MODE) p = p;
|
||||||
|
else throw "Unknown ParamMode: "+this.modes[index];
|
||||||
|
|
||||||
|
return p;
|
||||||
|
}
|
||||||
|
|
||||||
|
setParameter(proc: Interpreter, index: number, value: number): void
|
||||||
|
{
|
||||||
|
const prog = proc.program;
|
||||||
|
const ip = proc.instructionpointer;
|
||||||
|
|
||||||
|
let p = prog[ip+1+index];
|
||||||
|
|
||||||
|
if (this.modes[index] == ParamMode.POSITION_MODE) prog[p] = value;
|
||||||
|
else if (this.modes[index] == ParamMode.IMMEDIATE_MODE) throw "Immediate mode not allowed in write";
|
||||||
|
else throw "Unknown ParamMpde: "+this.modes[index];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
179
www/statics/aoc/2019/05_solution-2.ts
Normal file
179
www/statics/aoc/2019/05_solution-2.ts
Normal file
@ -0,0 +1,179 @@
|
|||||||
|
namespace AdventOfCode2019_05_2
|
||||||
|
{
|
||||||
|
const DAY = 5;
|
||||||
|
const PROBLEM = 2;
|
||||||
|
|
||||||
|
export async function run()
|
||||||
|
{
|
||||||
|
let input = await AdventOfCode.getInput(DAY);
|
||||||
|
if (input == null) return;
|
||||||
|
|
||||||
|
let runner = new Interpreter(input.split(",").map(p => parseInt(p.trim())), [5]);
|
||||||
|
|
||||||
|
runner.run();
|
||||||
|
|
||||||
|
AdventOfCode.output(DAY, PROBLEM, runner.output.reverse()[0].toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
class Interpreter
|
||||||
|
{
|
||||||
|
program: number[];
|
||||||
|
input: number[];
|
||||||
|
instructionpointer: number;
|
||||||
|
inputpointer: number;
|
||||||
|
output: number[];
|
||||||
|
|
||||||
|
constructor(prog: number[], input: number[])
|
||||||
|
{
|
||||||
|
this.program = prog;
|
||||||
|
this.input = input;
|
||||||
|
this.instructionpointer = 0;
|
||||||
|
this.inputpointer = 0;
|
||||||
|
this.output = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
run()
|
||||||
|
{
|
||||||
|
while(true)
|
||||||
|
{
|
||||||
|
const cmd = new Op(this.program[this.instructionpointer]);
|
||||||
|
let ipinc = true;
|
||||||
|
|
||||||
|
if (cmd.opcode == OpCode.ADD)
|
||||||
|
{
|
||||||
|
const p0 = cmd.getParameter(this, 0);
|
||||||
|
const p1 = cmd.getParameter(this, 1);
|
||||||
|
const pv = p0 + p1;
|
||||||
|
cmd.setParameter(this, 2, pv);
|
||||||
|
}
|
||||||
|
else if (cmd.opcode == OpCode.MUL)
|
||||||
|
{
|
||||||
|
const p0 = cmd.getParameter(this, 0);
|
||||||
|
const p1 = cmd.getParameter(this, 1);
|
||||||
|
const pv = p0 * p1;
|
||||||
|
cmd.setParameter(this, 2, pv);
|
||||||
|
}
|
||||||
|
else if (cmd.opcode == OpCode.HALT)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
else if (cmd.opcode == OpCode.IN)
|
||||||
|
{
|
||||||
|
const pv = this.input[this.inputpointer];
|
||||||
|
cmd.setParameter(this, 0, pv);
|
||||||
|
this.inputpointer++;
|
||||||
|
}
|
||||||
|
else if (cmd.opcode == OpCode.OUT)
|
||||||
|
{
|
||||||
|
const p0 = cmd.getParameter(this, 0);
|
||||||
|
this.output.push(p0);
|
||||||
|
console.log("# " + p0);
|
||||||
|
}
|
||||||
|
else if (cmd.opcode == OpCode.TJMP)
|
||||||
|
{
|
||||||
|
const p0 = cmd.getParameter(this, 0);
|
||||||
|
if (p0 != 0) { this.instructionpointer = cmd.getParameter(this, 1); ipinc=false; }
|
||||||
|
}
|
||||||
|
else if (cmd.opcode == OpCode.FJMP)
|
||||||
|
{
|
||||||
|
const p0 = cmd.getParameter(this, 0);
|
||||||
|
if (p0 == 0) { this.instructionpointer = cmd.getParameter(this, 1); ipinc=false; }
|
||||||
|
}
|
||||||
|
else if (cmd.opcode == OpCode.LT)
|
||||||
|
{
|
||||||
|
const p0 = cmd.getParameter(this, 0);
|
||||||
|
const p1 = cmd.getParameter(this, 1);
|
||||||
|
const pv = p0 < p1 ? 1 : 0;
|
||||||
|
cmd.setParameter(this, 2, pv);
|
||||||
|
}
|
||||||
|
else if (cmd.opcode == OpCode.EQ)
|
||||||
|
{
|
||||||
|
const p0 = cmd.getParameter(this, 0);
|
||||||
|
const p1 = cmd.getParameter(this, 1);
|
||||||
|
const pv = p0 == p1 ? 1 : 0;
|
||||||
|
cmd.setParameter(this, 2, pv);
|
||||||
|
}
|
||||||
|
else throw "Unknown Op: " + cmd.opcode + " @ " + this.instructionpointer;
|
||||||
|
|
||||||
|
if (ipinc) this.instructionpointer += 1 + cmd.parametercount;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
enum OpCode
|
||||||
|
{
|
||||||
|
ADD = 1,
|
||||||
|
MUL = 2,
|
||||||
|
IN = 3,
|
||||||
|
OUT = 4,
|
||||||
|
TJMP = 5,
|
||||||
|
FJMP = 6,
|
||||||
|
LT = 7,
|
||||||
|
EQ = 8,
|
||||||
|
HALT = 99,
|
||||||
|
}
|
||||||
|
|
||||||
|
enum ParamMode
|
||||||
|
{
|
||||||
|
POSITION_MODE = 0,
|
||||||
|
IMMEDIATE_MODE = 1,
|
||||||
|
}
|
||||||
|
|
||||||
|
class Op
|
||||||
|
{
|
||||||
|
opcode: OpCode;
|
||||||
|
modes: ParamMode[];
|
||||||
|
|
||||||
|
name: string;
|
||||||
|
parametercount: number;
|
||||||
|
|
||||||
|
constructor(v: number)
|
||||||
|
{
|
||||||
|
this.opcode = v%100;
|
||||||
|
v = Math.floor(v/100);
|
||||||
|
this.modes = [];
|
||||||
|
for(let i=0; i<4; i++)
|
||||||
|
{
|
||||||
|
this.modes.push(v%10);
|
||||||
|
v = Math.floor(v/10);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.opcode == OpCode.ADD) { this.name="ADD"; this.parametercount=3; }
|
||||||
|
else if (this.opcode == OpCode.MUL) { this.name="MUL"; this.parametercount=3; }
|
||||||
|
else if (this.opcode == OpCode.HALT) { this.name="HALT"; this.parametercount=0; }
|
||||||
|
else if (this.opcode == OpCode.IN) { this.name="IN"; this.parametercount=1; }
|
||||||
|
else if (this.opcode == OpCode.OUT) { this.name="OUT"; this.parametercount=1; }
|
||||||
|
else if (this.opcode == OpCode.TJMP) { this.name="TJMP"; this.parametercount=2; }
|
||||||
|
else if (this.opcode == OpCode.FJMP) { this.name="FJMP"; this.parametercount=2; }
|
||||||
|
else if (this.opcode == OpCode.LT) { this.name="LT"; this.parametercount=3; }
|
||||||
|
else if (this.opcode == OpCode.EQ) { this.name="EQ"; this.parametercount=3; }
|
||||||
|
else throw "Unknown opcode: "+this.opcode;
|
||||||
|
}
|
||||||
|
|
||||||
|
getParameter(proc: Interpreter, index: number): number
|
||||||
|
{
|
||||||
|
const prog = proc.program;
|
||||||
|
const ip = proc.instructionpointer;
|
||||||
|
|
||||||
|
let p = prog[ip+1+index];
|
||||||
|
|
||||||
|
if (this.modes[index] == ParamMode.POSITION_MODE) p = prog[p];
|
||||||
|
else if (this.modes[index] == ParamMode.IMMEDIATE_MODE) p = p;
|
||||||
|
else throw "Unknown ParamMode: "+this.modes[index];
|
||||||
|
|
||||||
|
return p;
|
||||||
|
}
|
||||||
|
|
||||||
|
setParameter(proc: Interpreter, index: number, value: number): void
|
||||||
|
{
|
||||||
|
const prog = proc.program;
|
||||||
|
const ip = proc.instructionpointer;
|
||||||
|
|
||||||
|
let p = prog[ip+1+index];
|
||||||
|
|
||||||
|
if (this.modes[index] == ParamMode.POSITION_MODE) prog[p] = value;
|
||||||
|
else if (this.modes[index] == ParamMode.IMMEDIATE_MODE) throw "Immediate mode not allowed in write";
|
||||||
|
else throw "Unknown ParamMpde: "+this.modes[index];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -35,6 +35,8 @@ return
|
|||||||
['day' => 1, 'parts' => 2, 'title' => 'The Tyranny of the Rocket Equation', 'language' => 'ts', 'solutions' => ['3464458', '5193796'] ],
|
['day' => 1, 'parts' => 2, 'title' => 'The Tyranny of the Rocket Equation', 'language' => 'ts', 'solutions' => ['3464458', '5193796'] ],
|
||||||
['day' => 2, 'parts' => 2, 'title' => '1202 Program Alarm', 'language' => 'ts', 'solutions' => ['3267740', '7870'] ],
|
['day' => 2, 'parts' => 2, 'title' => '1202 Program Alarm', 'language' => 'ts', 'solutions' => ['3267740', '7870'] ],
|
||||||
['day' => 3, 'parts' => 2, 'title' => 'Crossed Wires', 'language' => 'ts', 'solutions' => ['399', '15678'] ],
|
['day' => 3, 'parts' => 2, 'title' => 'Crossed Wires', 'language' => 'ts', 'solutions' => ['399', '15678'] ],
|
||||||
|
['day' => 4, 'parts' => 2, 'title' => 'Secure Container', 'language' => 'ts', 'solutions' => ['979', '635'] ],
|
||||||
|
['day' => 5, 'parts' => 2, 'title' => 'Sunny with a Chance of Asteroids', 'language' => 'ts', 'solutions' => ['8332629', '8805067'] ],
|
||||||
|
|
||||||
],
|
],
|
||||||
];
|
];
|
Loading…
Reference in New Issue
Block a user