-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathgrid.js
121 lines (103 loc) · 3.25 KB
/
grid.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
const SERIAL = require('./input');
class FuelCell {
constructor([x, y], serial = SERIAL) {
this.x = x;
this.y = y;
this.serial = serial;
this.computePowerLevel();
}
computePowerLevel() {
let rack_id = this.x + 10;
let power_level = rack_id * this.y;
power_level += this.serial;
power_level *= rack_id;
power_level = +String(power_level).substr(-3, 1);
power_level -= 5;
return (this.power_level = power_level);
}
get coord() {
return `${this.x},${this.y}`;
}
}
class Grid {
constructor([size_x, size_y], serial = SERIAL) {
this.grid = Array(size_y)
.fill()
.map((r, y) =>
Array(size_x)
.fill()
.map((c, x) => new FuelCell([x + 1, y + 1], serial))
);
}
getMostPowerfulSquare(size = 3) {
let square_at_coords_power = {};
for (let y = 0; y < this.grid.length - size + 1; y++) {
for (let x = 0; x < this.grid.length - size + 1; x++) {
let square_sum = this.getSquareSum(x, y, size);
square_at_coords_power[x + 1 + ',' + (y + 1)] = square_sum;
}
}
let squares_sorted = Object.entries(square_at_coords_power);
squares_sorted.sort((a, b) => {
if (a[1] < b[1]) return -1;
else if (a[1] > b[1]) return 1;
else return 0;
});
return {
coords: squares_sorted[squares_sorted.length - 1][0],
power: squares_sorted[squares_sorted.length - 1][1],
};
}
getMostPowerfulSquareOfAnySize(range_min = 1, range_max = this.grid.length) {
let coords = '';
let power = -Infinity;
let size = 0;
for (let i = range_min; i <= range_max; i++) {
process.stdout.write(i + ' ');
let max_square = this.getMostPowerfulSquare(i);
if (max_square.power > power) {
coords = max_square.coords;
power = max_square.power;
size = i;
console.log('');
console.log('New Largest!');
console.log({
coords,
size,
power,
});
console.log('');
}
}
console.log('\n==== DONE ====\n');
return {
coords,
size,
power,
};
}
getFlatSquare(x_coord, y_coord, size = 3) {
let flat_square = [];
for (let y = y_coord; y < y_coord + size; y++) {
for (let x = x_coord; x < x_coord + size; x++) {
let cell = this.grid[y][x];
flat_square.push(cell);
}
}
return flat_square;
}
getSquareSum(x_coord, y_coord, size = 3) {
let accumulator = 0;
for (let y = y_coord; y < y_coord + size; y++) {
for (let x = x_coord; x < x_coord + size; x++) {
let cell = this.grid[y][x];
accumulator += cell.power_level;
}
}
return accumulator;
}
getFuelCellAt(x, y) {
return this.grid[y - 1][x - 1];
}
}
module.exports = Grid;