|
1 | 1 | console.time("d11"); |
2 | 2 | const rl = require("./utils").getInputRL("d11"); |
3 | 3 |
|
| 4 | +const GRID_SIZE = 300; |
| 5 | +const PART1_SQUARE_SIZE = 3; |
| 6 | + |
4 | 7 | function programReadLine(rl) { |
5 | 8 | let sn = null; |
6 | 9 | rl.on("line", line => { |
7 | 10 | sn = Number(line); |
8 | 11 | }); |
9 | 12 |
|
10 | 13 | 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)); |
13 | 18 |
|
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); |
16 | 21 |
|
17 | 22 | console.timeEnd("d11"); |
18 | 23 | }); |
19 | 24 | } |
20 | 25 |
|
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; |
46 | 35 | } |
47 | 36 | } |
48 | 37 | } |
49 | | - return { x: maxX, y: maxY, power: maxPower }; |
| 38 | + return bestCoord; |
50 | 39 | } |
51 | 40 |
|
52 | | -function squareValue(grid, x, y, size) { |
| 41 | +function calculateSquare(grid, x, y, squareWidth) { |
| 42 | + //console.log("square", x, y, squareWidth); |
53 | 43 | 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; |
57 | 49 | } |
58 | 50 | } |
59 | 51 | return sum; |
60 | 52 | } |
61 | 53 |
|
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 | +} |
71 | 63 |
|
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 | +} |
74 | 69 |
|
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 | +} |
77 | 74 |
|
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 | +} |
98 | 79 |
|
99 | | - return bestCoord; |
100 | | -}; |
101 | | - */ |
| 80 | +programReadLine(rl); |
0 commit comments