Skip to content

Commit 161e5b8

Browse files
author
Alberto Bastos
committed
day 11 part 1 rewrote using array instead of object
1 parent e8e871a commit 161e5b8

File tree

1 file changed

+49
-70
lines changed

1 file changed

+49
-70
lines changed

src/d11.js

Lines changed: 49 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,101 +1,80 @@
11
console.time("d11");
22
const rl = require("./utils").getInputRL("d11");
33

4+
const GRID_SIZE = 300;
5+
const PART1_SQUARE_SIZE = 3;
6+
47
function programReadLine(rl) {
58
let sn = null;
69
rl.on("line", line => {
710
sn = Number(line);
811
});
912

1013
rl.on("close", () => {
11-
let grid = createGrid(sn, 300);
12-
let { x, y } = bestSquareWithSize(grid, 300, 3);
14+
//sn = 18; // example
15+
16+
let grid = createGrid(sn);
17+
console.log("Answer (part I):", processPart1(grid));
1318

14-
console.log("Answer (part I):", `${x},${y}`);
15-
console.log("Answer (part II):");
19+
//let best = bestSquare(grid, 300);
20+
//console.log("Answer (part II):", best);
1621

1722
console.timeEnd("d11");
1823
});
1924
}
2025

21-
function createGrid(sn, size) {
22-
let grid = {}; // 'x.y': power level
23-
let rackID, powerLevel, tmp;
24-
for (let x = 1; x <= size; x++) {
25-
for (let y = 1; y <= size; y++) {
26-
rackID = x + 10;
27-
tmp = (rackID * y + sn) * rackID;
28-
powerLevel = ((tmp % 1000) - (tmp % 100)) / 100 - 5;
29-
grid[`${x},${y}`] = powerLevel;
30-
}
31-
}
32-
return grid;
33-
}
34-
35-
function bestSquareWithSize(grid, gridSize, targetSize) {
36-
let maxPower = null;
37-
let maxX = null;
38-
let maxY = null;
39-
for (let x = 1; x <= gridSize - targetSize; x++) {
40-
for (let y = 1; y <= gridSize - targetSize; y++) {
41-
let value = squareValue(grid, x, y, targetSize);
42-
if (maxPower === null || value > maxPower) {
43-
maxPower = value;
44-
maxX = x;
45-
maxY = y;
26+
function processPart1(grid) {
27+
let bestPower = null;
28+
let bestCoord = null;
29+
for (let x = 1; x < GRID_SIZE - PART1_SQUARE_SIZE; x++) {
30+
for (let y = 1; y < GRID_SIZE - PART1_SQUARE_SIZE; y++) {
31+
let power = calculateSquare(grid, x, y, PART1_SQUARE_SIZE);
32+
if (bestPower == null || power > bestPower) {
33+
bestPower = power;
34+
bestCoord = x + "," + y;
4635
}
4736
}
4837
}
49-
return { x: maxX, y: maxY, power: maxPower };
38+
return bestCoord;
5039
}
5140

52-
function squareValue(grid, x, y, size) {
41+
function calculateSquare(grid, x, y, squareWidth) {
42+
//console.log("square", x, y, squareWidth);
5343
let sum = 0;
54-
for (let i = x; i < x + size; i++) {
55-
for (let j = y; j < y + size; j++) {
56-
sum += grid[`${i},${j}`];
44+
for (let dx = 0; dx < squareWidth; dx++) {
45+
for (let dy = 0; dy < squareWidth; dy++) {
46+
let cell = getCellLevel(grid, x + dx, y + dy);
47+
//console.log("add", x + dx, y + dy, cell);
48+
sum += cell;
5749
}
5850
}
5951
return sum;
6052
}
6153

62-
programReadLine(rl);
63-
64-
/**
65-
const getPower = (serial, x, y) => {
66-
const rackID = x + 10;
67-
const power = (rackID * y + serial) * rackID;
68-
const hundreds = Math.floor((power % 1000) / 100);
69-
return hundreds - 5;
70-
};
54+
function createGrid(sn) {
55+
let grid = new Array(GRID_SIZE * GRID_SIZE);
56+
for (let x = 0; x < GRID_SIZE; x++) {
57+
for (let y = 0; y < GRID_SIZE; y++) {
58+
grid[_2dto1d(GRID_SIZE, x, y)] = getPowerLevel(sn, x + 1, y + 1);
59+
}
60+
}
61+
return grid;
62+
}
7163

72-
module.exports = (input) => {
73-
const serial = Number(input);
64+
function getPowerLevel(sn, x, y) {
65+
let rackID = x + 10;
66+
let tmp = (rackID * y + sn) * rackID;
67+
return ((tmp % 1000) - (tmp % 100)) / 100 - 5;
68+
}
7469

75-
let bestCoord = '';
76-
let bestSum = 0;
70+
function getCellLevel(grid, x, y) {
71+
let level = grid[_2dto1d(GRID_SIZE, x - 1, y - 1)];
72+
return level;
73+
}
7774

78-
for (let y = 1; y <= 300; y += 1) {
79-
for (let x = 1; x <= 300; x += 1) {
80-
const maxSize = Math.min(301 - x, 301 - y);
81-
let powerSum = 0;
82-
for (let s = 0; s < maxSize; s += 1) {
83-
for (let dx = 0; dx < s; dx += 1) {
84-
powerSum += getPower(serial, x + dx, y + s);
85-
}
86-
for (let dy = 0; dy < s; dy += 1) {
87-
powerSum += getPower(serial, x + s, y + dy);
88-
}
89-
powerSum += getPower(serial, x + s, y + s);
90-
if (powerSum > bestSum) {
91-
bestSum = powerSum;
92-
bestCoord = x + ',' + y + ',' + (s + 1);
93-
}
94-
}
95-
}
96-
console.log(y);
97-
}
75+
function _2dto1d(width, x, y) {
76+
// converts matrix coordinates (2d) into array coordinates (1d)
77+
return width * y + x;
78+
}
9879

99-
return bestCoord;
100-
};
101-
*/
80+
programReadLine(rl);

0 commit comments

Comments
 (0)