Skip to content

Commit cc30e1f

Browse files
authored
Merge pull request #153 from romellem/2019/day-19
2019 - Day 19
2 parents d587a3c + aeb4fd2 commit cc30e1f

File tree

13 files changed

+1682
-5
lines changed

13 files changed

+1682
-5
lines changed

2019/11/intcode-computer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ class Computer {
224224
*
225225
* Now, if I were to re-map the pointer'd `@` symbols, I'd get
226226
*
227-
* @3 = @1 + 2
227+
* 7 = 9 + 2
228228
*
229229
* But _that isn't what we want!_ Namely, it doesn't make sense
230230
* to set the _literal_ number 7 equal to some addition operation.

2019/13/intcode-computer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ class Computer {
227227
*
228228
* Now, if I were to re-map the pointer'd `@` symbols, I'd get
229229
*
230-
* @3 = @1 + 2
230+
* 7 = 9 + 2
231231
*
232232
* But _that isn't what we want!_ Namely, it doesn't make sense
233233
* to set the _literal_ number 7 equal to some addition operation.

2019/19/README.md

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# Answers
2+
3+
| Part 1 | Part 2 |
4+
|--------|-----------|
5+
| `234` | `9290812` |
6+
7+
## --- Day 19: Tractor Beam ---
8+
9+
Unsure of the state of Santa's ship, you borrowed the tractor beam technology from Triton. Time to test it out.
10+
11+
When you're safely away from anything else, you activate the tractor beam, but nothing happens. It's hard to tell whether it's working if there's nothing to use it on. Fortunately, your ship's drone system can be configured to deploy a drone to specific coordinates and then check whether it's being pulled. There's even an [Intcode](https://adventofcode.com/2019/day/9) program (your puzzle input) that gives you access to the drone system.
12+
13+
The program uses two input instructions to request the _X and Y position_ to which the drone should be deployed. Negative numbers are invalid and will confuse the drone; all numbers should be _zero or positive_.
14+
15+
Then, the program will output whether the drone is _stationary_ (`0`) or _being pulled by something_ (`1`). For example, the coordinate X=`0`, Y=`0` is directly in front of the tractor beam emitter, so the drone control program will always report `1` at that location.
16+
17+
To better understand the tractor beam, it is important to _get a good picture_ of the beam itself. For example, suppose you scan the 10x10 grid of points closest to the emitter:
18+
19+
X
20+
0-> 9
21+
0#.........
22+
|.#........
23+
v..##......
24+
...###....
25+
....###...
26+
Y .....####.
27+
......####
28+
......####
29+
.......###
30+
9........##
31+
32+
33+
In this example, the _number of points affected by the tractor beam_ in the 10x10 area closest to the emitter is _`27`_.
34+
35+
However, you'll need to scan a larger area to _understand the shape_ of the beam. _How many points are affected by the tractor beam in the 50x50 area closest to the emitter?_ (For each of X and Y, this will be `0` through `49`.)
36+
37+
-----------------
38+
39+
## --- Part Two ---
40+
41+
You aren't sure how large Santa's ship is. You aren't even sure if you'll need to use this thing on Santa's ship, but it doesn't hurt to be prepared. You figure Santa's ship might fit in a _100x100_ square.
42+
43+
The beam gets wider as it travels away from the emitter; you'll need to be a minimum distance away to fit a square of that size into the beam fully. (Don't rotate the square; it should be aligned to the same axes as the drone grid.)
44+
45+
For example, suppose you have the following tractor beam readings:
46+
47+
<pre><code>#.......................................
48+
.#......................................
49+
..##....................................
50+
...###..................................
51+
....###.................................
52+
.....####...............................
53+
......#####.............................
54+
......######............................
55+
.......#######..........................
56+
........########........................
57+
.........#########......................
58+
..........#########.....................
59+
...........##########...................
60+
...........############.................
61+
............############................
62+
.............#############..............
63+
..............##############............
64+
...............###############..........
65+
................###############.........
66+
................#################.......
67+
.................########<b>O</b>OOOOOOOOO.....
68+
..................#######OOOOOOOOOO#....
69+
...................######OOOOOOOOOO###..
70+
....................#####OOOOOOOOOO#####
71+
.....................####OOOOOOOOOO#####
72+
.....................####OOOOOOOOOO#####
73+
......................###OOOOOOOOOO#####
74+
.......................##OOOOOOOOOO#####
75+
........................#OOOOOOOOOO#####
76+
.........................OOOOOOOOOO#####
77+
..........................##############
78+
..........................##############
79+
...........................#############
80+
............................############
81+
.............................###########
82+
</code></pre>
83+
84+
In this example, the _10x10_ square closest to the emitter that fits entirely within the tractor beam has been marked `O`. Within it, the point closest to the emitter (the only highlighted **`O`**) is at X=`25`, Y=`20`.
85+
86+
Find the _100x100_ square closest to the emitter that fits entirely within the tractor beam; within that square, find the point closest to the emitter. _What value do you get if you take that point's X coordinate, multiply it by `10000`, then add the point's Y coordinate?_ (In the example above, this would be `250020`.)

2019/19/infinite-grid.js

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
class InfiniteGrid {
2+
constructor({ defaultFactory = (x, y) => 0, string_map = {} } = {}) {
3+
this.defaultFactory = defaultFactory;
4+
this.string_map = string_map;
5+
this.grid = new Map();
6+
this.max_x = -Infinity;
7+
this.min_x = Infinity;
8+
this.max_y = -Infinity;
9+
this.min_y = Infinity;
10+
}
11+
12+
static toId(x, y) {
13+
return `${x},${y}`;
14+
}
15+
16+
set(x, y, value) {
17+
if (typeof x !== 'number' || typeof y !== 'number') {
18+
throw new Error(
19+
`x and y must be numbers, got (${typeof x})${x} and (${typeof y})${y}`
20+
);
21+
}
22+
if (x < this.min_x) this.min_x = x;
23+
if (x > this.max_x) this.max_x = x;
24+
if (y < this.min_y) this.min_y = y;
25+
if (y > this.max_y) this.max_y = y;
26+
const id = InfiniteGrid.toId(x, y);
27+
this.grid.set(id, value);
28+
}
29+
30+
get(x, y) {
31+
const id = InfiniteGrid.toId(x, y);
32+
if (!this.grid.has(id)) {
33+
this.set(x, y, this.defaultFactory(x, y));
34+
}
35+
return this.grid.get(id);
36+
}
37+
38+
toGrid() {
39+
let grid = [];
40+
for (let y = this.min_y; y <= this.max_y; y++) {
41+
let row = [];
42+
for (let x = this.min_x; x <= this.max_x; x++) {
43+
let cell = this.get(x, y);
44+
row.push(cell);
45+
}
46+
grid.push(row);
47+
}
48+
49+
return grid;
50+
}
51+
52+
sum() {
53+
let sum = 0;
54+
for (let value of this.grid.values()) {
55+
sum += value;
56+
}
57+
58+
return sum;
59+
}
60+
61+
toString() {
62+
let grid = this.toGrid();
63+
let rows = '';
64+
for (let y = 0; y < grid.length; y++) {
65+
let row = '';
66+
for (let x = 0; x < grid[y].length; x++) {
67+
let cell = grid[y][x];
68+
let cell_string =
69+
cell in this.string_map ? this.string_map[cell] : String(cell);
70+
row += cell_string;
71+
}
72+
rows += rows.length ? '\n' + row : row;
73+
}
74+
75+
return rows;
76+
}
77+
}
78+
79+
module.exports = {
80+
InfiniteGrid,
81+
};

2019/19/input.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const path = require('path');
2+
const fs = require('fs');
3+
4+
const input = fs
5+
.readFileSync(path.join(__dirname, 'input.txt'), 'utf8')
6+
.toString()
7+
.trim()
8+
.split(',')
9+
.map(v => parseInt(v, 10));
10+
11+
module.exports = {
12+
input,
13+
};

2019/19/input.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
109,424,203,1,21102,1,11,0,1106,0,282,21101,0,18,0,1105,1,259,2102,1,1,221,203,1,21102,31,1,0,1106,0,282,21101,38,0,0,1106,0,259,21002,23,1,2,21202,1,1,3,21102,1,1,1,21102,57,1,0,1106,0,303,2102,1,1,222,21001,221,0,3,21002,221,1,2,21101,0,259,1,21102,1,80,0,1106,0,225,21102,1,93,2,21102,1,91,0,1106,0,303,2101,0,1,223,21001,222,0,4,21102,1,259,3,21101,225,0,2,21101,225,0,1,21101,118,0,0,1106,0,225,20101,0,222,3,21102,1,120,2,21102,1,133,0,1106,0,303,21202,1,-1,1,22001,223,1,1,21101,0,148,0,1106,0,259,2102,1,1,223,21001,221,0,4,20102,1,222,3,21102,1,23,2,1001,132,-2,224,1002,224,2,224,1001,224,3,224,1002,132,-1,132,1,224,132,224,21001,224,1,1,21102,195,1,0,106,0,108,20207,1,223,2,20101,0,23,1,21101,-1,0,3,21102,1,214,0,1106,0,303,22101,1,1,1,204,1,99,0,0,0,0,109,5,2101,0,-4,249,21201,-3,0,1,21201,-2,0,2,21202,-1,1,3,21101,0,250,0,1105,1,225,21202,1,1,-4,109,-5,2106,0,0,109,3,22107,0,-2,-1,21202,-1,2,-1,21201,-1,-1,-1,22202,-1,-2,-2,109,-3,2106,0,0,109,3,21207,-2,0,-1,1206,-1,294,104,0,99,22102,1,-2,-2,109,-3,2106,0,0,109,5,22207,-3,-4,-1,1206,-1,346,22201,-4,-3,-4,21202,-3,-1,-1,22201,-4,-1,2,21202,2,-1,-1,22201,-4,-1,1,21201,-2,0,3,21102,343,1,0,1106,0,303,1106,0,415,22207,-2,-3,-1,1206,-1,387,22201,-3,-2,-3,21202,-2,-1,-1,22201,-3,-1,3,21202,3,-1,-1,22201,-3,-1,2,21201,-4,0,1,21101,0,384,0,1106,0,303,1105,1,415,21202,-4,-1,-4,22201,-4,-3,-4,22202,-3,-2,-2,22202,-2,-4,-4,22202,-3,-2,-3,21202,-4,-1,-2,22201,-3,-2,1,21202,1,1,-4,109,-5,2106,0,0

0 commit comments

Comments
 (0)