Skip to content

Commit bae60c7

Browse files
committed
Working part two!
Classic off by one error
1 parent c351ff0 commit bae60c7

File tree

5 files changed

+72
-54
lines changed

5 files changed

+72
-54
lines changed

2019/19/README.md

Lines changed: 43 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
# Answers
22

3-
| Part 1 | Part 2 |
4-
|--------|---------|
5-
| `234` | ` ` |
3+
| Part 1 | Part 2 |
4+
|--------|-----------|
5+
| `234` | `9290812` |
66

77
## --- Day 19: Tractor Beam ---
88

99
Unsure of the state of Santa's ship, you borrowed the tractor beam technology from Triton. Time to test it out.
1010

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](9) program (your puzzle input) that gives you access to the drone system.
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.
1212

1313
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_.
1414

@@ -30,7 +30,7 @@ To better understand the tractor beam, it is important to _get a good picture_ o
3030
9........##
3131

3232

33-
In this example, the _number of points affected by the tractor beam_ in the 10x10 area closest to the emitter is `_27_`.
33+
In this example, the _number of points affected by the tractor beam_ in the 10x10 area closest to the emitter is _`27`_.
3434

3535
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`.)
3636

@@ -44,43 +44,43 @@ The beam gets wider as it travels away from the emitter; you'll need to be a min
4444

4545
For example, suppose you have the following tractor beam readings:
4646

47-
#.......................................
48-
.#......................................
49-
..##....................................
50-
...###..................................
51-
....###.................................
52-
.....####...............................
53-
......#####.............................
54-
......######............................
55-
.......#######..........................
56-
........########........................
57-
.........#########......................
58-
..........#########.....................
59-
...........##########...................
60-
...........############.................
61-
............############................
62-
.............#############..............
63-
..............##############............
64-
...............###############..........
65-
................###############.........
66-
................#################.......
67-
.................########OOOOOOOOOO.....
68-
..................#######OOOOOOOOOO#....
69-
...................######OOOOOOOOOO###..
70-
....................#####OOOOOOOOOO#####
71-
.....................####OOOOOOOOOO#####
72-
.....................####OOOOOOOOOO#####
73-
......................###OOOOOOOOOO#####
74-
.......................##OOOOOOOOOO#####
75-
........................#OOOOOOOOOO#####
76-
.........................OOOOOOOOOO#####
77-
..........................##############
78-
..........................##############
79-
...........................#############
80-
............................############
81-
.............................###########
82-
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`.
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`.
8585

8686
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/input.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1-
const input = [
2-
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
3-
]
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));
410

511
module.exports = {
612
input,

2019/19/part-one.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ const { input } = require('./input');
22
const { TractorBeam } = require('./tractor-beam');
33

44
let tractor_beam = new TractorBeam(input);
5-
console.log(tractor_beam.partOne());
5+
let answer = tractor_beam.partOne();
66
console.log(tractor_beam.grid.toString());
7+
console.log(answer);

2019/19/part-two.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,3 @@ const { TractorBeam } = require('./tractor-beam');
33

44
let tractor_beam = new TractorBeam(input);
55
console.log(tractor_beam.partTwo());
6-
// 9370819 - too high

2019/19/tractor-beam.js

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,13 @@ class TractorBeam {
7373
/**
7474
* Once we have a row that is wide enough, we can move onto the next phase.
7575
* Take the left most point in the row (bottom_edge.x). Calculate the value
76-
* at the point + `square_size` down from there. If we hit a `0`, go to B.
76+
* at the `point + square_size - 1` down from there. If we hit a `0`, go to B.
7777
*
78-
* If we hit a `1`, then move right `square_size` amount from that point.
78+
* If we hit a `1`, then move right `square_size - 1` amount from that point.
7979
* If _that_ is a `1`, we found our top-left point. Exit.
8080
*
8181
* B. If it's a `0`, then move inward `square_size - row_width` number of times,
82-
* continually checking the point + `square_size` down. If we iterate through
82+
* continually checking the `point + square_size - 1` down. If we iterate through
8383
* the available squares in the row, then the top-left point doesn't exist
8484
* in that row. Calculate the next set of edges and start again.
8585
*/
@@ -88,14 +88,26 @@ class TractorBeam {
8888
const row_width = this.getWidth(bottom_edge, top_edge);
8989
for (
9090
let x = bottom_edge.x;
91-
x < bottom_edge.x + row_width - square_size;
91+
x <= bottom_edge.x + row_width - square_size;
9292
x++
9393
) {
94-
let y = bottom_edge.y + square_size;
94+
/**
95+
* If I want a 2x2 square, and I'm at 0,0, I don't jump to 0,2 / 2,2 / 0,2
96+
*
97+
* 01234
98+
* 0 x.x--
99+
* 1 ...
100+
* 2 x.x
101+
* 3 |
102+
* 4 |
103+
*
104+
* That'd make a 3x3 square! So I jump forward the square size minus 1.
105+
*/
106+
let y = bottom_edge.y + square_size - 1;
95107
let output = this.computeAt(x, y);
96108
this.grid.set(x, y, output);
97109
if (output === 1) {
98-
let bottom_right = this.computeAt(x + square_size, y);
110+
let bottom_right = this.computeAt(x + square_size - 1, y);
99111
if (bottom_right === 1) {
100112
found_square = { x, y: bottom_edge.y };
101113
break;

0 commit comments

Comments
 (0)