-
Notifications
You must be signed in to change notification settings - Fork 0
/
day3.js
91 lines (80 loc) · 1.77 KB
/
day3.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
const fs = require('fs');
const input = Number(fs.readFileSync('./day3_data.txt', { encoding: 'utf8' }).trim());
function part1() {
let i = 1;
while (input > i * i) {
i += 2;
}
let j = i - 1, num = i * i, k;
loop1:
while (j >= 0) {
k = i - 1;
loop2:
while (k >= 0) {
if (k == i - 1 || k == 0 || j == 0 || j == i - 1) {
if (num == input) {
break loop1;
}
num--;
}
k--;
}
j--;
}
const half = Math.ceil(i / 2) - 1;
return Math.abs(half - j) + Math.abs(half - k);
}
function part2() {
const grid = Array(1).fill(Array(1).fill(1));
let step = 1;
while (true) {
step += 2;
for (const row of grid) {
row.push(0);
row.unshift(0);
}
grid.push(Array(step).fill(0));
grid.unshift(Array(step).fill(0));
let x = step - 2;
let y = step - 1;
while (x >= 0) {
grid[x][y] = sumNeighbors(x, y);
if (grid[x][y] > input) return grid[x][y];
x--;
}
x = 0;
y--;
while (y >= 0) {
grid[x][y] = sumNeighbors(x, y);
if (grid[x][y] > input) return grid[x][y];
y--;
}
y = 0;
x = 1;
while (x < step) {
grid[x][y] = sumNeighbors(x, y);
if (grid[x][y] > input) return grid[x][y];
x++;
}
x = step - 1;
y = 1;
while (y < step) {
grid[x][y] = sumNeighbors(x, y);
if (grid[x][y] > input) return grid[x][y];
y++;
}
}
function sumNeighbors(m, n) {
let sum = 0;
for (let q = m - 1; q <= m + 1; q++) {
for (let w = n - 1; w <= n + 1; w++) {
if (grid[q] != undefined && grid[q][w] != undefined) {
sum += grid[q][w];
}
}
}
return sum;
}
}
console.log('Part 1: %d', part1());
console.log('Part 2: %d', part2());