-
Notifications
You must be signed in to change notification settings - Fork 0
/
15.js
82 lines (76 loc) · 2.04 KB
/
15.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
/**
* @param {string} d
*/
export const part1 = async d => {
const data = d.split('\n')
.map(e => [...(/^Sensor at x=(-?\d+), y=(-?\d+): closest beacon is at x=(-?\d+), y=(-?\d+)$/.exec(e))].slice(1))
.map(e => e.map(e => parseInt(e, 10)));
const targetY = 2000000;
const targetYPings = new Set();
// Set search area of Sensors
data.forEach(sensor => {
const dist = Math.abs(sensor[0] - sensor[2]) + Math.abs(sensor[1] - sensor[3]);
for (let x = sensor[0] - dist; x < sensor[0] + dist; x++) {
// Outside of the search range
if (Math.abs(sensor[0] - x) + Math.abs(sensor[1] - targetY) > dist) continue;
// There's a beacon here
if (x == sensor[2] && targetY == sensor[3]) continue;
targetYPings.add(x);
}
});
return targetYPings.size;
};
/**
* @param {string} d
*/
export const part2 = async d => {
const data = d.split('\n')
.map(e => [...(/^Sensor at x=(-?\d+), y=(-?\d+): closest beacon is at x=(-?\d+), y=(-?\d+)$/.exec(e))].slice(1))
.map(e => e.map(e => parseInt(e, 10)))
.map(e => [e[0], e[1], Math.abs(e[0] - e[2]) + Math.abs(e[1] - e[3]) + 1]);
//const range = 20;
const range = 4000000;
for (const sensor of data) {
// Search parameter
let x = sensor[0];
let y = sensor[1] - sensor[2];
let xDir = 1;
let yDir = 1;
let searching = true;
let turn = 0;
while (searching) {
// Check if we need to change the x direction
x += xDir;
y += yDir;
if (y == sensor[1]) {
xDir *= -1;
turn++;
}
if (x == sensor[0]) {
yDir *= -1;
turn++;
}
if (turn > 3) {
break;
}
if (x >= 0 && y >= 0 && x <= range && y <= range) {
// Check to see if this spot overlaps with another sensor
searching = false;
for (const checkSensor of data) {
if (checkSensor == sensor) {
continue;
}
if (Math.abs(x - checkSensor[0]) + Math.abs(y - checkSensor[1]) <= checkSensor[2] - 1) {
searching = true;
break;
}
}
if (!searching) {
return x * 4000000 + y;
}
searching = true;
}
}
//break; // Until I do the loops
}
};