Skip to content

Commit 3209bb4

Browse files
committed
Moves edge algo to method
1 parent a1ff21a commit 3209bb4

File tree

1 file changed

+36
-29
lines changed

1 file changed

+36
-29
lines changed

2019/19/tractor-beam.js

Lines changed: 36 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -71,39 +71,46 @@ class TractorBeam {
7171
let top_edge = {...start};
7272
let bottom_edge = {...start};
7373
while (top_edge.x - bottom_edge.x <= square_size) {
74-
// First, move down until we hit 0
75-
let output;
74+
this.calculateNewEdges(bottom_edge, top_edge);
75+
}
76+
}
77+
78+
/**
79+
* Updates edges in place.
80+
*/
81+
calculateNewEdges(bottom_edge, top_edge) {
82+
// First, move down until we hit 0
83+
let output;
84+
do {
85+
bottom_edge.y++;
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 === 1);
90+
91+
// Then, move inward until we hit a `1`
92+
do {
93+
bottom_edge.x++;
94+
let computer = new Computer({ memory: this.memory, inputs: [bottom_edge.x, bottom_edge.y] });
95+
[output] = computer.run();
96+
this.grid.set(bottom_edge.x, bottom_edge.y, output);
97+
} while (output === 0);
98+
99+
// Now do top edge
100+
do {
101+
// Double loops in case bottom calc mo
102+
top_edge.y++;
76103
do {
77-
bottom_edge.y++;
78-
let computer = new Computer({ memory: this.memory, inputs: [bottom_edge.x, bottom_edge.y] });
104+
// Move right until we hit a `0`
105+
top_edge.x++;
106+
let computer = new Computer({ memory: this.memory, inputs: [top_edge.x, top_edge.y] });
79107
[output] = computer.run();
80-
this.grid.set(bottom_edge.x, bottom_edge.y, output);
108+
this.grid.set(top_edge.x, top_edge.y, output);
81109
} 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);
110+
} while (top_edge.y !== bottom_edge.y);
103111

104-
// The cell immediately to the left of that is the right edge.
105-
top_edge.x--;
106-
}
112+
// The cell immediately to the left of that is the right edge.
113+
top_edge.x--;
107114
}
108115
}
109116

0 commit comments

Comments
 (0)