Oxygen System


Fork me on GitHub
2019-12-15

Day 15: Oxygen System

Description:
--- Day 15: Oxygen System ---

Out here in deep space, many things can go wrong. Fortunately, many of those things have indicator lights. Unfortunately, one of those lights is lit: the oxygen system for part of the ship has failed!

According to the readouts, the oxygen system must have failed days ago after a rupture in oxygen tank two; that section of the ship was automatically sealed once oxygen levels went dangerously low. A single remotely-operated repair droid is your only option for fixing the oxygen system.

The Elves' care package included an Intcode program (your puzzle input) that you can use to remotely control the repair droid. By running that program, you can direct the repair droid to the oxygen system and fix the problem.

The remote control program executes the following steps in a loop forever:

Accept a movement command via an input instruction.
Send the movement command to the repair droid.
Wait for the repair droid to finish the movement operation.
Report on the status of the repair droid via an output instruction.

Only four movement commands are understood: north (1), south (2), west (3), and east (4). Any other command is invalid. The movements differ in direction, but not in distance: in a long enough east-west hallway, a series of commands like 4,4,4,4,3,3,3,3 would leave the repair droid back where it started.

The repair droid can reply with any of the following status codes:

0: The repair droid hit a wall. Its position has not changed.
1: The repair droid has moved one step in the requested direction.
2: The repair droid has moved one step in the requested direction; its new position is the location of the oxygen system.

You don't know anything about the area around the repair droid, but you can figure it out by watching the status codes.

For example, we can draw the area using D for the droid, # for walls, . for locations the droid can traverse, and empty space for unexplored locations. Then, the initial state looks like this:



D



To make the droid go north, send it 1. If it replies with 0, you know that location is a wall and that the droid didn't move:


#
D



To move east, send 4; a reply of 1 means the movement was successful:


#
.D



Then, perhaps attempts to move north (1), south (2), and east (4) are all met with replies of 0:


##
.D#
#


Now, you know the repair droid is in a dead end. Backtrack with 3 (which you already know will get a reply of 1 because you already know that location is open):


##
D.#
#


Then, perhaps west (3) gets a reply of 0, south (2) gets a reply of 1, south again (2) gets a reply of 0, and then west (3) gets a reply of 2:


##
#..#
D.#
#

Now, because of the reply of 2, you know you've found the oxygen system! In this example, it was only 2 moves away from the repair droid's starting position.

What is the fewest number of movement commands required to move the repair droid from its starting position to the location of the oxygen system?

--- Part Two ---

You quickly repair the oxygen system; oxygen gradually fills the area.

Oxygen starts in the location containing the repaired oxygen system. It takes one minute for oxygen to spread to all open locations that are adjacent to a location that already contains oxygen. Diagonal locations are not adjacent.

In the example above, suppose you've used the droid to explore the area fully and have the following map (where locations that currently contain oxygen are marked O):

##
#..##
#.#..#
#.O.#
###

Initially, the only location which contains oxygen is the location of the repaired oxygen system. However, after one minute, the oxygen spreads to all open (.) locations that are adjacent to a location containing oxygen:

##
#..##
#.#..#
#OOO#
###

After a total of two minutes, the map looks like this:

##
#..##
#O#O.#
#OOO#
###

After a total of three minutes:

##
#O.##
#O#OO#
#OOO#
###

And finally, the whole region is full of oxygen after a total of four minutes:

##
#OO##
#O#OO#
#OOO#
###

So, in this example, all locations contain oxygen after 4 minutes.

Use the repair droid to get a complete map of the area. How many minutes will it take to fill with oxygen?

Input:
3,1033,1008,1033,1,1032,1005,1032,31,1008,1033,2,1032,1005,1032,58,1008,1033,3,1032,1005,1032,81,1008,1033,4,1032,1005,1032,104,99,101,0,1034,1039,102,1,1036,1041,1001,1035,-1,1040,1008,1038,0,1043,102,-1,1043,1032,1,1037,1032,1042,1106,0,124,101,0,1034,1039,101,0,1036,1041,1001,1035,1,1040,1008,1038,0,1043,1,1037,1038,1042,1105,1,124,1001,1034,-1,1039,1008,1036,0,1041,1002,1035,1,1040,1001,1038,0,1043,1001,1037,0,1042,1106,0,124,1001,1034,1,1039,1008,1036,0,1041,102,1,1035,1040,1002,1038,1,1043,102,1,1037,1042,1006,1039,217,1006,1040,217,1008,1039,40,1032,1005,1032,217,1008,1040,40,1032,1005,1032,217,1008,1039,9,1032,1006,1032,165,1008,1040,33,1032,1006,1032,165,1101,2,0,1044,1106,0,224,2,1041,1043,1032,1006,1032,179,1102,1,1,1044,1106,0,224,1,1041,1043,1032,1006,1032,217,1,1042,1043,1032,1001,1032,-1,1032,1002,1032,39,1032,1,1032,1039,1032,101,-1,1032,1032,101,252,1032,211,1007,0,53,1044,1106,0,224,1101,0,0,1044,1106,0,224,1006,1044,247,1001,1039,0,1034,101,0,1040,1035,102,1,1041,1036,101,0,1043,1038,101,0,1042,1037,4,1044,1105,1,0,64,32,22,30,60,6,82,30,34,77,7,15,17,32,43,96,51,43,27,74,39,14,10,70,32,20,59,35,98,3,81,47,40,23,65,16,1,82,35,35,44,76,93,55,6,40,65,15,62,62,67,7,72,21,92,85,54,71,42,84,80,30,64,88,50,90,16,34,63,20,88,24,64,86,11,64,20,44,23,63,11,26,10,84,75,13,93,39,16,67,2,91,97,22,86,40,69,11,40,58,93,22,82,30,24,40,58,26,75,70,52,20,27,95,57,23,69,9,30,82,87,70,42,32,90,67,36,92,41,97,72,24,3,36,60,96,5,62,13,74,27,21,60,58,90,17,49,27,70,29,59,48,72,30,35,11,21,60,99,35,37,71,9,84,3,22,74,20,48,70,19,58,65,22,14,72,15,7,31,77,61,5,31,60,24,80,33,58,49,78,80,37,79,66,37,83,4,21,50,65,96,23,67,89,44,17,58,60,41,96,96,39,27,62,84,18,74,38,56,9,72,70,32,62,95,6,87,51,96,36,4,3,79,21,21,31,66,93,13,10,77,43,52,68,66,47,42,55,57,23,60,45,63,3,86,96,29,70,81,31,3,48,38,91,34,69,85,18,95,93,96,85,15,38,80,35,17,98,92,14,57,60,25,46,63,60,16,58,25,48,73,59,40,6,72,46,91,39,22,63,79,58,67,84,33,52,78,52,26,21,61,49,78,77,5,95,75,20,56,30,43,67,75,33,84,10,14,60,21,98,14,31,81,97,49,64,19,69,44,3,68,2,66,20,69,48,81,96,22,56,22,25,27,60,59,36,10,45,81,39,46,97,54,49,42,78,89,26,93,55,14,96,48,48,96,57,51,82,94,23,46,64,20,10,56,19,63,41,77,17,26,68,47,37,97,84,6,93,26,99,1,11,84,12,79,74,34,85,25,48,92,69,68,44,59,35,99,33,88,75,29,12,87,79,37,74,24,98,4,68,1,85,43,31,60,2,82,16,51,65,97,4,82,42,52,82,56,58,24,33,60,22,65,29,43,75,10,72,34,97,70,11,36,89,26,69,84,26,50,17,42,83,44,63,1,84,77,22,89,46,72,79,93,22,94,34,79,48,68,55,3,73,91,30,79,37,76,19,24,61,41,98,32,12,6,57,16,44,55,43,63,55,98,11,68,17,50,67,26,86,19,60,14,56,30,59,11,9,41,26,59,39,56,49,48,82,3,83,64,69,48,65,89,42,78,33,25,91,92,50,91,8,64,73,92,16,96,28,40,27,67,22,69,95,7,12,70,56,49,81,22,68,67,40,48,92,43,14,86,60,49,39,74,58,42,43,54,37,2,84,25,41,22,22,65,16,6,67,62,22,25,88,52,76,88,40,20,75,84,24,4,39,99,51,72,73,14,15,81,39,70,15,26,15,32,34,83,33,73,95,14,55,91,65,81,44,3,89,49,80,5,38,56,42,76,68,14,4,76,71,98,65,62,31,6,96,23,77,82,4,59,91,29,14,91,42,80,35,2,58,99,27,40,64,43,86,74,17,58,68,24,71,51,73,35,74,63,50,17,24,5,83,71,12,62,33,19,31,84,41,25,71,75,41,43,55,85,22,11,88,71,49,33,55,50,63,52,64,23,25,42,85,47,49,30,65,42,95,61,15,86,7,61,62,32,79,62,82,13,84,30,69,21,70,22,99,95,71,5,71,11,69,39,85,79,89,41,94,82,86,46,83,96,80,48,74,63,56,8,58,38,66,12,61,33,88,30,92,27,57,0,0,21,21,1,10,1,0,0,0,0,0,0

Part 1:
namespace AdventOfCode2019_15_1
{
	const DAY     = 15;
	const PROBLEM = 1;

	export async function run()
	{
		let input = await AdventOfCode.getInput(DAY);
		if (input == null) return;

		const code = input.trim().split(",").map(p => parseInt(p.trim()));

		let wm = new WorldMap();

		let rnr = new Interpreter(code, []);

		let d = Direction.North;

		await AdventOfCode.outputIntermed(wm.dump());

		for(;;)
		{
			if (wm.isfree(move_pos(wm.position, turn_right(d))))
			{
				d = turn_right(d);
			}

			rnr.inputqueue.push(d);
			rnr.autoRun();

			if (rnr.is_halted) throw "halted";
			if (rnr.output.length !== 1) throw "out";
		
			let nx = wm.position[0];
			let ny = wm.position[1];

			if (d === Direction.North) ny--;
			if (d === Direction.East)  nx++;
			if (d === Direction.South) ny++;
			if (d === Direction.West)  nx--;

			if (rnr.output[0] === 0)
			{
				wm.set_wall(nx, ny);

				d = turn_left(d);
			}
			else if (rnr.output[0] === 1)
			{
				wm.set_empty(nx, ny);
				wm.position = [nx, ny];
			}
			else if (rnr.output[0] === 2) 
			{
				wm.set_target(nx, ny);
				wm.position = [nx, ny];
			}

			rnr.output = [];

			await AdventOfCode.outputIntermed(wm.dump());

			if (wm.unknowns.length === 0) break;
		}

		await AdventOfCode.outputIntermed(wm.dump());

		const dist = await wm.calcDistance();

		await AdventOfCode.outputIntermed(wm.dump3());

		AdventOfCode.output(DAY, PROBLEM, dist.toString());
	}

	function turn_right(d: Direction): Direction
	{
		if (d === Direction.North) return Direction.East;
		if (d === Direction.East)  return Direction.South;
		if (d === Direction.South) return Direction.West;
		if (d === Direction.West)  return Direction.North;

		throw "d";
	}

	function turn_left(d: Direction): Direction
	{
		if (d === Direction.North) return Direction.West;
		if (d === Direction.East)  return Direction.North;
		if (d === Direction.South) return Direction.East;
		if (d === Direction.West)  return Direction.South;

		throw "d";
	}

	function move_pos(p: [number, number], d: Direction): [number, number]
	{
		let nx = p[0];
		let ny = p[1];

		if (d === Direction.North) ny--;
		if (d === Direction.East)  nx++;
		if (d === Direction.South) ny++;476
		if (d === Direction.West)  nx--;

		return [nx, ny];
	}

	enum Direction
	{
		North = 1,
		South = 2,
		West  = 3,
		East  = 4,
	}
	
	enum Field
	{
		Unknown = 0,
		Wall    = 1,
		Empty   = 2,
		POI     = 3, // possible new way to go, but unknown
		Target  = 4,
		Path    = 5,
	}

	class WorldMap
	{
		world: { [_:number]: Field } = {};
		unknowns: [number, number][] = [];

		position: [number, number] = [0, 0];

		minx: number = 0;
		miny: number = 0;
		maxx: number = 0;
		maxy: number = 0;

		target_pos: [number,number] = [NaN, NaN];

		distance_map: { [_:number]: number } = {};

		constructor()
		{
			this.set_empty(0, 0);
		}

		async calcDistance(): Promise<number>
		{
			this.position = [0,0];

			let distance_map: { [_:number]: number } = {};
			distance_map[0] = 0;

			let updates: [number, number][] = [];
			updates.unshift([-1,0]);
			updates.unshift([+1,0]);
			updates.unshift([0,+1]);
			updates.unshift([0,-1]);

			while(updates.length > 0)
			{
				const pos = updates.pop();
				if (pos === undefined) throw "undef";
				if (!this.isfree(pos)) continue;

				const x = pos[0];
				const y = pos[1];

				const i = (y*10000000 + x);

				const i_n = ((y-1)*10000000 + (x));
				const i_e = ((y)*10000000 + (x+1));
				const i_s = ((y+1)*10000000 + (x));
				const i_w = ((y)*10000000 + (x-1));

				const d_curr = (i in distance_map) ? distance_map[i] : Number.MAX_SAFE_INTEGER;

				const d_n = (i_n in distance_map) ? distance_map[i_n] : Number.MAX_SAFE_INTEGER;
				const d_e = (i_e in distance_map) ? distance_map[i_e] : Number.MAX_SAFE_INTEGER;
				const d_s = (i_s in distance_map) ? distance_map[i_s] : Number.MAX_SAFE_INTEGER;
				const d_w = (i_w in distance_map) ? distance_map[i_w] : Number.MAX_SAFE_INTEGER;

				let d_new = Math.min(d_n, d_e, d_s, d_w);
				if (d_new !== Number.MAX_SAFE_INTEGER) d_new++;

				await AdventOfCode.outputIntermed(this.dump2(distance_map, updates));

				if (d_curr <= d_new) continue;

				distance_map[i] = d_new;

				updates.unshift([x+1, y]);
				updates.unshift([x-1, y]);
				updates.unshift([x, y+1]);
				updates.unshift([x, y-1]);
			}

			await AdventOfCode.outputIntermed(this.dump2(distance_map, updates));


			let tpos = this.target_pos;
			for(;;)
			{
				const x = tpos[0];
				const y = tpos[1];

				const i = (y*10000000 + x);
				const d = distance_map[i];

				const i_n = ((y-1)*10000000 + (x));
				const i_e = ((y)*10000000 + (x+1));
				const i_s = ((y+1)*10000000 + (x));
				const i_w = ((y)*10000000 + (x-1));

				const d_n = (i_n in distance_map) ? distance_map[i_n] : Number.MAX_SAFE_INTEGER;
				const d_e = (i_e in distance_map) ? distance_map[i_e] : Number.MAX_SAFE_INTEGER;
				const d_s = (i_s in distance_map) ? distance_map[i_s] : Number.MAX_SAFE_INTEGER;
				const d_w = (i_w in distance_map) ? distance_map[i_w] : Number.MAX_SAFE_INTEGER;

				if (d === 1) break;

				if (d_n+1 === d) 
				{
					tpos = [tpos[0], tpos[1]-1];
					this.set(tpos[0], tpos[1], Field.Path);
				}
				else if (d_e+1 === d) 
				{
					tpos = [tpos[0]+1, tpos[1]];
					this.set(tpos[0], tpos[1], Field.Path);
					
				}
				else if (d_s+1 === d) 
				{
					tpos = [tpos[0], tpos[1]+1];
					this.set(tpos[0], tpos[1], Field.Path);
					
				}
				else if (d_w+1 === d) 
				{
					tpos = [tpos[0]-1, tpos[1]];
					this.set(tpos[0], tpos[1], Field.Path);
					
				}
				else throw "";
			}


			return distance_map[(this.target_pos[1]*10000000 + this.target_pos[0])];
		}

		dump2(distance_map: { [_:number]: number }, updates: [number, number][]): string 
		{
			let str = "";
			for(let yy=this.miny;yy<=this.maxy;yy++)
			{
				for(let xx=this.minx;xx<=this.maxx;xx++)
				{
					const i = (yy*10000000 + xx);

					if (this.get(xx, yy) === Field.Wall)
					{
						str += "#";
					}
					else if (updates.filter(u => u[0] === xx && u[1] === yy).length > 0)
					{
						str += "@";
					}
					else if (i in distance_map)
					{
						str += "+";
					}
					else
					{
						str += ".";
					}
				}
				str += "\n";
			}
			return str;
		}

		isfree(p: [number, number]): boolean
		{
			const f = this.get(p[0], p[1]);

			return (f !== Field.Wall) && (f !== Field.Unknown);
		}

		get(x: number, y: number): Field 
		{
			const i = (y*10000000 + x);
			if (!(i in this.world)) return Field.Unknown;
			return this.world[i];
		}

		set(x: number, y: number, f: Field) 
		{
			const i = (y*10000000 + x);
			
			if (this.world[i] === Field.POI) this.unknowns = this.unknowns.filter(p => p[0] !== x || p[1] !== y);
			
			this.world[i] = f;
			
			if (f === Field.POI) this.unknowns.push([x, y]);

			if (f === Field.Target) this.target_pos = [x, y];

			this.minx = Math.min(this.minx, x);
			this.maxx = Math.max(this.maxx, x);
			this.miny = Math.min(this.miny, y);
			this.maxy = Math.max(this.maxy, y);
		}

		set_empty(x: number, y: number)
		{
			this.set(x, y, Field.Empty);

			if (this.get(x-1, y) === Field.Unknown) this.set(x-1, y, Field.POI);
			if (this.get(x+1, y) === Field.Unknown) this.set(x+1, y, Field.POI);
			if (this.get(x, y-1) === Field.Unknown) this.set(x, y-1, Field.POI);
			if (this.get(x, y+1) === Field.Unknown) this.set(x, y+1, Field.POI);
		}

		set_target(x: number, y: number)
		{
			this.set(x, y, Field.Target);

			if (this.get(x-1, y) === Field.Unknown) this.set(x-1, y, Field.POI);
			if (this.get(x+1, y) === Field.Unknown) this.set(x+1, y, Field.POI);
			if (this.get(x, y-1) === Field.Unknown) this.set(x, y-1, Field.POI);
			if (this.get(x, y+1) === Field.Unknown) this.set(x, y+1, Field.POI);
		}

		set_wall(x: number, y: number)
		{
			this.set(x, y, Field.Wall);
		}

		dump(): string 
		{
			let str = "";
			for(let yy=this.miny;yy<=this.maxy;yy++)
			{
				for(let xx=this.minx;xx<=this.maxx;xx++)
				{
					if (xx === this.position[0] && yy === this.position[1])
					{
						str += "X";
					}
					else if (xx === 0 && yy === 0)
					{
						str += "0";
					}
					else
					{
						switch(this.get(xx, yy))
						{
							case Field.Empty:   str += "."; break;
							case Field.POI:     str += "?"; break;
							case Field.Target:  str += "@"; break;
							case Field.Unknown: str += " "; break;
							case Field.Wall:    str += "#"; break;
							case Field.Path:    str += "="; break;
						}
					}
				}
				str += "\n";
			}
			return str;
		}

		dump3(): string 
		{
			let str = "";
			for(let yy=this.miny;yy<=this.maxy;yy++)
			{
				for(let xx=this.minx;xx<=this.maxx;xx++)
				{
					if (xx === 0 && yy === 0)
					{
						str += "X";
					}
					else
					{
						switch(this.get(xx, yy))
						{
							case Field.Empty:   str += " "; break;
							case Field.POI:     str += " "; break;
							case Field.Target:  str += "@"; break;
							case Field.Unknown: str += " "; break;
							case Field.Wall:    str += "#"; break;
							case Field.Path:    str += "."; break;
						}
					}
				}
				str += "\n";
			}
			return str;
		}
	}

	class Interpreter
	{
		program: InfMem;
		inputqueue: number[];
		instructionpointer: number;
		output: number[];
		relative_base: number;

		is_halted: boolean = false;

		constructor(prog: number[], input: number[])
		{
			this.program = new InfMem(prog);
			this.inputqueue = input;
			this.instructionpointer = 0;
			this.output = [];
			this.relative_base = 0;
		}

		fullRun() : number[]
		{
			while(!this.is_halted)
			{
				const r = this.singleStep();

				if (r === StepResult.EXECUTED) continue;
				if (r === StepResult.HALTED) return this.output;
				if (r === StepResult.WAITING_FOR_IN) throw "not enough input";

				throw "unknown output of singleStep";
			}

			return this.output;
		}

		autoRun() : StepResult
		{
			while(!this.is_halted)
			{
				const r = this.singleStep();

				if (r === StepResult.EXECUTED) continue;
				if (r === StepResult.HALTED) return StepResult.HALTED;
				if (r === StepResult.WAITING_FOR_IN) return StepResult.WAITING_FOR_IN;

				throw "unknown output of singleStep";
			}

			return StepResult.HALTED;
		}

		singleStep() : StepResult
		{
			const cmd = new Op(this.program.r(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);

				this.incInstrPtr(cmd);
				
				return StepResult.EXECUTED;
			}
			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);

				this.incInstrPtr(cmd);
				
				return StepResult.EXECUTED;
			}
			else if (cmd.opcode == OpCode.HALT)
			{
				this.is_halted = true;
				return StepResult.HALTED;
			}
			else if (cmd.opcode == OpCode.IN)
			{
				if (this.inputqueue.length == 0) return StepResult.WAITING_FOR_IN;

				const pv = this.inputqueue[0];
				cmd.setParameter(this, 0, pv);
				this.inputqueue = this.inputqueue.slice(1);

				this.incInstrPtr(cmd);
				return StepResult.EXECUTED;
			}
			else if (cmd.opcode == OpCode.OUT)
			{
				const p0 = cmd.getParameter(this, 0);
				this.output.push(p0);
				//AdventOfCode.outputConsole("# " + p0);

				this.incInstrPtr(cmd);

				return StepResult.EXECUTED;
			}
			else if (cmd.opcode == OpCode.TJMP)
			{
				const p0 = cmd.getParameter(this, 0);
				if (p0 != 0) this.instructionpointer = cmd.getParameter(this, 1);
				else this.incInstrPtr(cmd);
				
				return StepResult.EXECUTED;
			}
			else if (cmd.opcode == OpCode.FJMP)
			{
				const p0 = cmd.getParameter(this, 0);
				if (p0 == 0) this.instructionpointer = cmd.getParameter(this, 1);
				else this.incInstrPtr(cmd);
				
				return StepResult.EXECUTED;
			}
			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);

				this.incInstrPtr(cmd);
				return StepResult.EXECUTED;
			}
			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);

				this.incInstrPtr(cmd);
				return StepResult.EXECUTED;
			}
			else if (cmd.opcode == OpCode.ARB)
			{
				const p0 = cmd.getParameter(this, 0);
				this.relative_base = this.relative_base+p0;

				this.incInstrPtr(cmd);
				return StepResult.EXECUTED;
			}
			else throw "Unknown Op: " + cmd.opcode + " @ " + this.instructionpointer;
		}

		private incInstrPtr(cmd: Op)
		{
			this.instructionpointer += 1 + cmd.parametercount;
		}
	}

	enum StepResult { EXECUTED, HALTED, WAITING_FOR_IN }

	enum OpCode 
	{
		ADD  = 1,
		MUL  = 2,
		IN   = 3,
		OUT  = 4,
		TJMP = 5,
		FJMP = 6,
		LT   = 7,
		EQ   = 8,
		ARB  = 9,
		HALT = 99,
	}

	enum ParamMode
	{
		POSITION_MODE  = 0,
		IMMEDIATE_MODE = 1,
		RELATIVE_MODE  = 2,
	}

	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 if (this.opcode == OpCode.ARB)  { this.name="ARB";   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.r(ip+1+index);

				 if (this.modes[index] == ParamMode.POSITION_MODE)  p = prog.r(p);
			else if (this.modes[index] == ParamMode.IMMEDIATE_MODE) p = p;
			else if (this.modes[index] == ParamMode.RELATIVE_MODE)  p = prog.r(proc.relative_base+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.r(ip+1+index);

				 if (this.modes[index] == ParamMode.POSITION_MODE)  prog.w(p, value);
			else if (this.modes[index] == ParamMode.IMMEDIATE_MODE) throw "Immediate mode not allowed in write";
			else if (this.modes[index] == ParamMode.RELATIVE_MODE)  prog.w(proc.relative_base+p, value);
			else throw "Unknown ParamMode: "+this.modes[index];
		}
	}

	class InfMem
	{
		private data: { [_:number]:number } = {};   

		constructor(v: number[])
		{
			for(let i=0; i<v.length;i++) this.data[i]=v[i];
		}

		r(pos: number): number
		{
			if (!(pos in this.data)) this.data[pos] = 0;
			return this.data[pos];
		}

		w(pos: number, val: number): number
		{
			return this.data[pos] = val;
		}
	}
}

Result: 244

Part 2:
namespace AdventOfCode2019_15_2
{
	const DAY     = 15;
	const PROBLEM = 2;

	export async function run()
	{
		let input = await AdventOfCode.getInput(DAY);
		if (input == null) return;

		const code = input.trim().split(",").map(p => parseInt(p.trim()));

		let wm = new WorldMap();

		let rnr = new Interpreter(code, []);

		let d = Direction.North;

		await AdventOfCode.outputIntermed(wm.dump());

		for(;;)
		{
			if (wm.isfree(move_pos(wm.position, turn_right(d))))
			{
				d = turn_right(d);
			}

			rnr.inputqueue.push(d);
			rnr.autoRun();

			if (rnr.is_halted) throw "halted";
			if (rnr.output.length !== 1) throw "out";
		
			let nx = wm.position[0];
			let ny = wm.position[1];

			if (d === Direction.North) ny--;
			if (d === Direction.East)  nx++;
			if (d === Direction.South) ny++;
			if (d === Direction.West)  nx--;

			if (rnr.output[0] === 0)
			{
				wm.set_wall(nx, ny);

				d = turn_left(d);
			}
			else if (rnr.output[0] === 1)
			{
				wm.set_empty(nx, ny);
				wm.position = [nx, ny];
			}
			else if (rnr.output[0] === 2) 
			{
				wm.set_target(nx, ny);
				wm.position = [nx, ny];
			}

			rnr.output = [];

			await AdventOfCode.outputIntermed(wm.dump());

			if (wm.unknowns.length === 0) break;
		}

		await AdventOfCode.outputIntermed(wm.dump());

		const time = await wm.calcFillTime();

		AdventOfCode.output(DAY, PROBLEM, time.toString());
	}

	function turn_right(d: Direction): Direction
	{
		if (d === Direction.North) return Direction.East;
		if (d === Direction.East)  return Direction.South;
		if (d === Direction.South) return Direction.West;
		if (d === Direction.West)  return Direction.North;

		throw "d";
	}

	function turn_left(d: Direction): Direction
	{
		if (d === Direction.North) return Direction.West;
		if (d === Direction.East)  return Direction.North;
		if (d === Direction.South) return Direction.East;
		if (d === Direction.West)  return Direction.South;

		throw "d";
	}

	function move_pos(p: [number, number], d: Direction): [number, number]
	{
		let nx = p[0];
		let ny = p[1];

		if (d === Direction.North) ny--;
		if (d === Direction.East)  nx++;
		if (d === Direction.South) ny++;476
		if (d === Direction.West)  nx--;

		return [nx, ny];
	}

	enum Direction
	{
		North = 1,
		South = 2,
		West  = 3,
		East  = 4,
	}
	
	enum Field
	{
		Unknown = 0,
		Wall    = 1,
		Empty   = 2,
		POI     = 3, // possible new way to go, but unknown
		Target  = 4,
		Path    = 5,
	}

	class WorldMap
	{
		world: { [_:number]: Field } = {};
		unknowns: [number, number][] = [];

		position: [number, number] = [0, 0];

		minx: number = 0;
		miny: number = 0;
		maxx: number = 0;
		maxy: number = 0;

		target_pos: [number,number] = [NaN, NaN];

		distance_map: { [_:number]: number } = {};

		constructor()
		{
			this.set_empty(0, 0);
		}

		async calcFillTime(): Promise<number>
		{
			let oxy_map: { [_:number]: boolean } = {};

			let next: [number, number][] = [];
			next.push([this.target_pos[0], this.target_pos[1]]);

			let counter = -1;

			for(;;)
			{

				let ls = Object.assign([], next);
				next = [];

				let updates = 0;
				for(let pos of ls)
				{
					const x = pos[0];
					const y = pos[1];

					const i = (y*10000000 + x);
					if (i in oxy_map) continue;

					oxy_map[i] = true;
					updates++;

					if (this.isfree([x-1, y])) next.push([x-1, y]);
					if (this.isfree([x+1, y])) next.push([x+1, y]);
					if (this.isfree([x, y-1])) next.push([x, y-1]);
					if (this.isfree([x, y+1])) next.push([x, y+1]);
				}

				await AdventOfCode.outputIntermed(this.dump4(oxy_map));

				if (updates === 0) return counter;

				counter++;
			}
		}

		async calcDistance(): Promise<number>
		{
			this.position = [0,0];

			let distance_map: { [_:number]: number } = {};
			distance_map[0] = 0;

			let updates: [number, number][] = [];
			updates.unshift([-1,0]);
			updates.unshift([+1,0]);
			updates.unshift([0,+1]);
			updates.unshift([0,-1]);

			while(updates.length > 0)
			{
				const pos = updates.pop();
				if (pos === undefined) throw "undef";
				if (!this.isfree(pos)) continue;

				const x = pos[0];
				const y = pos[1];

				const i = (y*10000000 + x);

				const i_n = ((y-1)*10000000 + (x));
				const i_e = ((y)*10000000 + (x+1));
				const i_s = ((y+1)*10000000 + (x));
				const i_w = ((y)*10000000 + (x-1));

				const d_curr = (i in distance_map) ? distance_map[i] : Number.MAX_SAFE_INTEGER;

				const d_n = (i_n in distance_map) ? distance_map[i_n] : Number.MAX_SAFE_INTEGER;
				const d_e = (i_e in distance_map) ? distance_map[i_e] : Number.MAX_SAFE_INTEGER;
				const d_s = (i_s in distance_map) ? distance_map[i_s] : Number.MAX_SAFE_INTEGER;
				const d_w = (i_w in distance_map) ? distance_map[i_w] : Number.MAX_SAFE_INTEGER;

				let d_new = Math.min(d_n, d_e, d_s, d_w);
				if (d_new !== Number.MAX_SAFE_INTEGER) d_new++;

				await AdventOfCode.outputIntermed(this.dump2(distance_map, updates));

				if (d_curr <= d_new) continue;

				distance_map[i] = d_new;

				updates.unshift([x+1, y]);
				updates.unshift([x-1, y]);
				updates.unshift([x, y+1]);
				updates.unshift([x, y-1]);
			}

			await AdventOfCode.outputIntermed(this.dump2(distance_map, updates));

			let tpos = this.target_pos;
			for(;;)
			{
				const x = tpos[0];
				const y = tpos[1];

				const i = (y*10000000 + x);
				const d = distance_map[i];

				const i_n = ((y-1)*10000000 + (x));
				const i_e = ((y)*10000000 + (x+1));
				const i_s = ((y+1)*10000000 + (x));
				const i_w = ((y)*10000000 + (x-1));

				const d_n = (i_n in distance_map) ? distance_map[i_n] : Number.MAX_SAFE_INTEGER;
				const d_e = (i_e in distance_map) ? distance_map[i_e] : Number.MAX_SAFE_INTEGER;
				const d_s = (i_s in distance_map) ? distance_map[i_s] : Number.MAX_SAFE_INTEGER;
				const d_w = (i_w in distance_map) ? distance_map[i_w] : Number.MAX_SAFE_INTEGER;

				if (d === 1) break;

				if (d_n+1 === d) 
				{
					tpos = [tpos[0], tpos[1]-1];
					this.set(tpos[0], tpos[1], Field.Path);
				}
				else if (d_e+1 === d) 
				{
					tpos = [tpos[0]+1, tpos[1]];
					this.set(tpos[0], tpos[1], Field.Path);
					
				}
				else if (d_s+1 === d) 
				{
					tpos = [tpos[0], tpos[1]+1];
					this.set(tpos[0], tpos[1], Field.Path);
					
				}
				else if (d_w+1 === d) 
				{
					tpos = [tpos[0]-1, tpos[1]];
					this.set(tpos[0], tpos[1], Field.Path);
					
				}
				else throw "";
			}


			return distance_map[(this.target_pos[1]*10000000 + this.target_pos[0])];
		}
		isfree(p: [number, number]): boolean
		{
			const f = this.get(p[0], p[1]);

			return (f !== Field.Wall) && (f !== Field.Unknown);
		}

		get(x: number, y: number): Field 
		{
			const i = (y*10000000 + x);
			if (!(i in this.world)) return Field.Unknown;
			return this.world[i];
		}

		set(x: number, y: number, f: Field) 
		{
			const i = (y*10000000 + x);
			
			if (this.world[i] === Field.POI) this.unknowns = this.unknowns.filter(p => p[0] !== x || p[1] !== y);
			
			this.world[i] = f;
			
			if (f === Field.POI) this.unknowns.push([x, y]);

			if (f === Field.Target) this.target_pos = [x, y];

			this.minx = Math.min(this.minx, x);
			this.maxx = Math.max(this.maxx, x);
			this.miny = Math.min(this.miny, y);
			this.maxy = Math.max(this.maxy, y);
		}

		set_empty(x: number, y: number)
		{
			this.set(x, y, Field.Empty);

			if (this.get(x-1, y) === Field.Unknown) this.set(x-1, y, Field.POI);
			if (this.get(x+1, y) === Field.Unknown) this.set(x+1, y, Field.POI);
			if (this.get(x, y-1) === Field.Unknown) this.set(x, y-1, Field.POI);
			if (this.get(x, y+1) === Field.Unknown) this.set(x, y+1, Field.POI);
		}

		set_target(x: number, y: number)
		{
			this.set(x, y, Field.Target);

			if (this.get(x-1, y) === Field.Unknown) this.set(x-1, y, Field.POI);
			if (this.get(x+1, y) === Field.Unknown) this.set(x+1, y, Field.POI);
			if (this.get(x, y-1) === Field.Unknown) this.set(x, y-1, Field.POI);
			if (this.get(x, y+1) === Field.Unknown) this.set(x, y+1, Field.POI);
		}

		set_wall(x: number, y: number)
		{
			this.set(x, y, Field.Wall);
		}

		dump(): string 
		{
			let str = "";
			for(let yy=this.miny;yy<=this.maxy;yy++)
			{
				for(let xx=this.minx;xx<=this.maxx;xx++)
				{
					if (xx === this.position[0] && yy === this.position[1])
					{
						str += "X";
					}
					else if (xx === 0 && yy === 0)
					{
						str += "0";
					}
					else
					{
						switch(this.get(xx, yy))
						{
							case Field.Empty:   str += "."; break;
							case Field.POI:     str += "?"; break;
							case Field.Target:  str += "@"; break;
							case Field.Unknown: str += " "; break;
							case Field.Wall:    str += "#"; break;
							case Field.Path:    str += "="; break;
						}
					}
				}
				str += "\n";
			}
			return str;
		}

		dump2(distance_map: { [_:number]: number }, updates: [number, number][]): string 
		{
			let str = "";
			for(let yy=this.miny;yy<=this.maxy;yy++)
			{
				for(let xx=this.minx;xx<=this.maxx;xx++)
				{
					const i = (yy*10000000 + xx);

					if (this.get(xx, yy) === Field.Wall)
					{
						str += "#";
					}
					else if (updates.filter(u => u[0] === xx && u[1] === yy).length > 0)
					{
						str += "@";
					}
					else if (i in distance_map)
					{
						str += "+";
					}
					else
					{
						str += ".";
					}
				}
				str += "\n";
			}
			return str;
		}


		dump3(): string 
		{
			let str = "";
			for(let yy=this.miny;yy<=this.maxy;yy++)
			{
				for(let xx=this.minx;xx<=this.maxx;xx++)
				{
					if (xx === 0 && yy === 0)
					{
						str += "X";
					}
					else
					{
						switch(this.get(xx, yy))
						{
							case Field.Empty:   str += " "; break;
							case Field.POI:     str += " "; break;
							case Field.Target:  str += "@"; break;
							case Field.Unknown: str += " "; break;
							case Field.Wall:    str += "#"; break;
							case Field.Path:    str += "."; break;
						}
					}
				}
				str += "\n";
			}
			return str;
		}

		dump4(oxy_map: { [_:number]: boolean }): string 
		{
			let str = "";
			for(let yy=this.miny;yy<=this.maxy;yy++)
			{
				for(let xx=this.minx;xx<=this.maxx;xx++)
				{
					const i = (yy*10000000 + xx);

					if (this.get(xx, yy) === Field.Wall)
					{
						str += "#";
					}
					else if (xx === this.target_pos[0] && yy === this.target_pos[1])
					{
						str += "X";
					}
					else if (i in oxy_map)
					{
						str += "@";
					}
					else
					{
						str += " ";
					}
				}
				str += "\n";
			}
			return str;
		}

	}

	class Interpreter
	{
		program: InfMem;
		inputqueue: number[];
		instructionpointer: number;
		output: number[];
		relative_base: number;

		is_halted: boolean = false;

		constructor(prog: number[], input: number[])
		{
			this.program = new InfMem(prog);
			this.inputqueue = input;
			this.instructionpointer = 0;
			this.output = [];
			this.relative_base = 0;
		}

		fullRun() : number[]
		{
			while(!this.is_halted)
			{
				const r = this.singleStep();

				if (r === StepResult.EXECUTED) continue;
				if (r === StepResult.HALTED) return this.output;
				if (r === StepResult.WAITING_FOR_IN) throw "not enough input";

				throw "unknown output of singleStep";
			}

			return this.output;
		}

		autoRun() : StepResult
		{
			while(!this.is_halted)
			{
				const r = this.singleStep();

				if (r === StepResult.EXECUTED) continue;
				if (r === StepResult.HALTED) return StepResult.HALTED;
				if (r === StepResult.WAITING_FOR_IN) return StepResult.WAITING_FOR_IN;

				throw "unknown output of singleStep";
			}

			return StepResult.HALTED;
		}

		singleStep() : StepResult
		{
			const cmd = new Op(this.program.r(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);

				this.incInstrPtr(cmd);
				
				return StepResult.EXECUTED;
			}
			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);

				this.incInstrPtr(cmd);
				
				return StepResult.EXECUTED;
			}
			else if (cmd.opcode == OpCode.HALT)
			{
				this.is_halted = true;
				return StepResult.HALTED;
			}
			else if (cmd.opcode == OpCode.IN)
			{
				if (this.inputqueue.length == 0) return StepResult.WAITING_FOR_IN;

				const pv = this.inputqueue[0];
				cmd.setParameter(this, 0, pv);
				this.inputqueue = this.inputqueue.slice(1);

				this.incInstrPtr(cmd);
				return StepResult.EXECUTED;
			}
			else if (cmd.opcode == OpCode.OUT)
			{
				const p0 = cmd.getParameter(this, 0);
				this.output.push(p0);
				//AdventOfCode.outputConsole("# " + p0);

				this.incInstrPtr(cmd);

				return StepResult.EXECUTED;
			}
			else if (cmd.opcode == OpCode.TJMP)
			{
				const p0 = cmd.getParameter(this, 0);
				if (p0 != 0) this.instructionpointer = cmd.getParameter(this, 1);
				else this.incInstrPtr(cmd);
				
				return StepResult.EXECUTED;
			}
			else if (cmd.opcode == OpCode.FJMP)
			{
				const p0 = cmd.getParameter(this, 0);
				if (p0 == 0) this.instructionpointer = cmd.getParameter(this, 1);
				else this.incInstrPtr(cmd);
				
				return StepResult.EXECUTED;
			}
			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);

				this.incInstrPtr(cmd);
				return StepResult.EXECUTED;
			}
			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);

				this.incInstrPtr(cmd);
				return StepResult.EXECUTED;
			}
			else if (cmd.opcode == OpCode.ARB)
			{
				const p0 = cmd.getParameter(this, 0);
				this.relative_base = this.relative_base+p0;

				this.incInstrPtr(cmd);
				return StepResult.EXECUTED;
			}
			else throw "Unknown Op: " + cmd.opcode + " @ " + this.instructionpointer;
		}

		private incInstrPtr(cmd: Op)
		{
			this.instructionpointer += 1 + cmd.parametercount;
		}
	}

	enum StepResult { EXECUTED, HALTED, WAITING_FOR_IN }

	enum OpCode 
	{
		ADD  = 1,
		MUL  = 2,
		IN   = 3,
		OUT  = 4,
		TJMP = 5,
		FJMP = 6,
		LT   = 7,
		EQ   = 8,
		ARB  = 9,
		HALT = 99,
	}

	enum ParamMode
	{
		POSITION_MODE  = 0,
		IMMEDIATE_MODE = 1,
		RELATIVE_MODE  = 2,
	}

	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 if (this.opcode == OpCode.ARB)  { this.name="ARB";   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.r(ip+1+index);

				 if (this.modes[index] == ParamMode.POSITION_MODE)  p = prog.r(p);
			else if (this.modes[index] == ParamMode.IMMEDIATE_MODE) p = p;
			else if (this.modes[index] == ParamMode.RELATIVE_MODE)  p = prog.r(proc.relative_base+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.r(ip+1+index);

				 if (this.modes[index] == ParamMode.POSITION_MODE)  prog.w(p, value);
			else if (this.modes[index] == ParamMode.IMMEDIATE_MODE) throw "Immediate mode not allowed in write";
			else if (this.modes[index] == ParamMode.RELATIVE_MODE)  prog.w(proc.relative_base+p, value);
			else throw "Unknown ParamMode: "+this.modes[index];
		}
	}

	class InfMem
	{
		private data: { [_:number]:number } = {};   

		constructor(v: number[])
		{
			for(let i=0; i<v.length;i++) this.data[i]=v[i];
		}

		r(pos: number): number
		{
			if (!(pos in this.data)) this.data[pos] = 0;
			return this.data[pos];
		}

		w(pos: number, val: number): number
		{
			return this.data[pos] = val;
		}
	}
}

Result: 278


made with vanilla PHP and MySQL, no frameworks, no bootstrap, no unnecessary* javascript