Skip to content

Commit a1ff21a

Browse files
committed
Working 2nd part of algo
1 parent c88edfb commit a1ff21a

File tree

1 file changed

+53
-16
lines changed

1 file changed

+53
-16
lines changed

2019/19/tractor-beam.js

Lines changed: 53 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,26 +21,13 @@ class TractorBeam {
2121
return this.grid.sum();
2222
}
2323

24-
partTwo() {
24+
partTwo(square_size = 100) {
2525
/**
2626
* First, we need to find when the tractor beam begins after the origin.
2727
* We do this by diagonalizing from 1,0 (or 0,1) until we hit 1 (being pulled).
2828
* Rather than just following a simple `y = x` line from the origin, we diagonalize
2929
* because the "slope" may be thin enough to not add pulled markers near the origin
3030
* (as I see with my puzzle input).
31-
*
32-
* Once we have a start, we then mark two points, TOP and BOTTOM. Think of TOP
33-
* as the top-right edge and BOTTOM as the bottom-left edge. If we can determine
34-
* those values, we don't need to run the program for the points in between: we
35-
* know they'll be filled in.
36-
*
37-
* TOP and BOTTOM will both start out at the same point. BOTTOM will then go down
38-
* until we hit a `0` (more than likely will be after the first down move). When
39-
* that happens, then move inward towards the right until we hit a `1`. That is
40-
* the new BOTTOM edge.
41-
*
42-
* Similarly, for TOP, move down 1, then move right until we hit a `0`. The cell
43-
* immediately to the left of that is the right edge.
4431
*/
4532
this.grid.set(0, 0, 1);
4633
let start;
@@ -54,7 +41,7 @@ class TractorBeam {
5441

5542
if (output) {
5643
this.grid.set(x, y, 1);
57-
start = [x, y];
44+
start = {x, y};
5845
break;
5946
}
6047

@@ -66,7 +53,57 @@ class TractorBeam {
6653
y++;
6754
}
6855
}
69-
console.log(start);
56+
57+
/**
58+
* Once we have a start, we then mark two points, TOP and BOTTOM. Think of TOP
59+
* as the top-right edge and BOTTOM as the bottom-left edge. If we can determine
60+
* those values, we don't need to run the program for the points in between: we
61+
* know they'll be filled in.
62+
*
63+
* TOP and BOTTOM will both start out at the same point. BOTTOM will then go down
64+
* until we hit a `0` (more than likely will be after the first down move). When
65+
* that happens, then move inward towards the right until we hit a `1`. That is
66+
* the new BOTTOM edge.
67+
*
68+
* Similarly, for TOP, move down 1, then move right until we hit a `0`. The cell
69+
* immediately to the left of that is the right edge.
70+
*/
71+
let top_edge = {...start};
72+
let bottom_edge = {...start};
73+
while (top_edge.x - bottom_edge.x <= square_size) {
74+
// First, move down until we hit 0
75+
let output;
76+
do {
77+
bottom_edge.y++;
78+
let computer = new Computer({ memory: this.memory, inputs: [bottom_edge.x, bottom_edge.y] });
79+
[output] = computer.run();
80+
this.grid.set(bottom_edge.x, bottom_edge.y, output);
81+
} while (output === 1);
82+
83+
// Then, move inward until we hit a `1`
84+
do {
85+
bottom_edge.x++;
86+
let computer = new Computer({ memory: this.memory, inputs: [bottom_edge.x, bottom_edge.y] });
87+
[output] = computer.run();
88+
this.grid.set(bottom_edge.x, bottom_edge.y, output);
89+
} while (output === 0);
90+
91+
// Now do top edge
92+
do {
93+
// Double loops in case bottom calc mo
94+
top_edge.y++;
95+
do {
96+
// Move right until we hit a `0`
97+
top_edge.x++;
98+
let computer = new Computer({ memory: this.memory, inputs: [top_edge.x, top_edge.y] });
99+
[output] = computer.run();
100+
this.grid.set(top_edge.x, top_edge.y, output);
101+
} while (output === 1);
102+
} while (top_edge.y !== bottom_edge.y);
103+
104+
// The cell immediately to the left of that is the right edge.
105+
top_edge.x--;
106+
}
70107
}
71108
}
72109

0 commit comments

Comments
 (0)