Skip to content

Commit c351ff0

Browse files
committed
Tries for solution, too high
1 parent 3209bb4 commit c351ff0

File tree

2 files changed

+77
-17
lines changed

2 files changed

+77
-17
lines changed

2019/19/part-two.js

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

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

2019/19/tractor-beam.js

Lines changed: 76 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,9 @@ class TractorBeam {
1010
partOne() {
1111
for (let x = 0; x < 50; x++) {
1212
for (let y = 0; y < 50; y++) {
13-
// The computer halts after every output, so we create a new one each time
14-
let computer = new Computer({ memory: this.memory, inputs: [x, y] });
15-
let output = computer.run();
13+
let output = this.computeAt(x, y);
1614

17-
this.grid.set(x, y, output.shift());
15+
this.grid.set(x, y, output);
1816
}
1917
}
2018

@@ -35,13 +33,11 @@ class TractorBeam {
3533
let x = 1;
3634
let y = 0;
3735
while (!start) {
38-
// The computer halts after every output, so we create a new one each time
39-
let computer = new Computer({ memory: this.memory, inputs: [x, y] });
40-
let [output] = computer.run();
36+
let output = this.computeAt(x, y);
4137

42-
if (output) {
38+
if (output === 1) {
4339
this.grid.set(x, y, 1);
44-
start = {x, y};
40+
start = { x, y };
4541
break;
4642
}
4743

@@ -68,11 +64,55 @@ class TractorBeam {
6864
* Similarly, for TOP, move down 1, then move right until we hit a `0`. The cell
6965
* immediately to the left of that is the right edge.
7066
*/
71-
let top_edge = {...start};
72-
let bottom_edge = {...start};
73-
while (top_edge.x - bottom_edge.x <= square_size) {
67+
let top_edge = { ...start };
68+
let bottom_edge = { ...start };
69+
while (this.getWidth(bottom_edge, top_edge) < square_size) {
7470
this.calculateNewEdges(bottom_edge, top_edge);
7571
}
72+
73+
/**
74+
* Once we have a row that is wide enough, we can move onto the next phase.
75+
* 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.
77+
*
78+
* If we hit a `1`, then move right `square_size` amount from that point.
79+
* If _that_ is a `1`, we found our top-left point. Exit.
80+
*
81+
* 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
83+
* the available squares in the row, then the top-left point doesn't exist
84+
* in that row. Calculate the next set of edges and start again.
85+
*/
86+
let found_square;
87+
while (!found_square) {
88+
const row_width = this.getWidth(bottom_edge, top_edge);
89+
for (
90+
let x = bottom_edge.x;
91+
x < bottom_edge.x + row_width - square_size;
92+
x++
93+
) {
94+
let y = bottom_edge.y + square_size;
95+
let output = this.computeAt(x, y);
96+
this.grid.set(x, y, output);
97+
if (output === 1) {
98+
let bottom_right = this.computeAt(x + square_size, y);
99+
if (bottom_right === 1) {
100+
found_square = { x, y: bottom_edge.y };
101+
break;
102+
}
103+
}
104+
}
105+
106+
if (!found_square) {
107+
// Try the next row
108+
this.calculateNewEdges(bottom_edge, top_edge);
109+
}
110+
}
111+
112+
// What value do you get if you take that
113+
// point's X coordinate, multiply it by 10000, then add the point's Y coordinate?
114+
console.log(found_square);
115+
return found_square.x * 10000 + found_square.y;
76116
}
77117

78118
/**
@@ -83,15 +123,21 @@ class TractorBeam {
83123
let output;
84124
do {
85125
bottom_edge.y++;
86-
let computer = new Computer({ memory: this.memory, inputs: [bottom_edge.x, bottom_edge.y] });
126+
let computer = new Computer({
127+
memory: this.memory,
128+
inputs: [bottom_edge.x, bottom_edge.y],
129+
});
87130
[output] = computer.run();
88131
this.grid.set(bottom_edge.x, bottom_edge.y, output);
89132
} while (output === 1);
90-
133+
91134
// Then, move inward until we hit a `1`
92135
do {
93136
bottom_edge.x++;
94-
let computer = new Computer({ memory: this.memory, inputs: [bottom_edge.x, bottom_edge.y] });
137+
let computer = new Computer({
138+
memory: this.memory,
139+
inputs: [bottom_edge.x, bottom_edge.y],
140+
});
95141
[output] = computer.run();
96142
this.grid.set(bottom_edge.x, bottom_edge.y, output);
97143
} while (output === 0);
@@ -103,7 +149,10 @@ class TractorBeam {
103149
do {
104150
// Move right until we hit a `0`
105151
top_edge.x++;
106-
let computer = new Computer({ memory: this.memory, inputs: [top_edge.x, top_edge.y] });
152+
let computer = new Computer({
153+
memory: this.memory,
154+
inputs: [top_edge.x, top_edge.y],
155+
});
107156
[output] = computer.run();
108157
this.grid.set(top_edge.x, top_edge.y, output);
109158
} while (output === 1);
@@ -112,6 +161,17 @@ class TractorBeam {
112161
// The cell immediately to the left of that is the right edge.
113162
top_edge.x--;
114163
}
164+
165+
getWidth(bottom_edge, top_edge) {
166+
return top_edge.x - bottom_edge.x + 1;
167+
}
168+
169+
computeAt(x, y) {
170+
// The computer halts after every output, so we create a new one each time
171+
let computer = new Computer({ memory: this.memory, inputs: [x, y] });
172+
let [output] = computer.run();
173+
return output;
174+
}
115175
}
116176

117177
module.exports = {

0 commit comments

Comments
 (0)