-
Notifications
You must be signed in to change notification settings - Fork 0
/
day06.js
75 lines (61 loc) · 1.66 KB
/
day06.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
// input = `
// 1, 1
// 1, 6
// 8, 3
// 3, 4
// 5, 5
// 8, 9
// `
fs = require('fs')
input = fs.readFileSync('inputs/06.txt', 'utf-8')
lines = input.trim().split('\n')
coords = lines.map(l => l.match(/\d+/g).map(s => s | 0))
maxLeft = maxRight = coords[0][0]
maxTop = maxBottom = coords[0][1]
coords.forEach(point => {
const [x, y] = point
if (x < maxLeft) maxLeft = x
if (x > maxRight) maxRight = x
if (y < maxTop) maxTop = y
if (y > maxBottom) maxBottom = y
})
function distance(p1, p2) {
const [x1, y1] = p1
const [x2, y2] = p2
return Math.abs(x1 - x2) + Math.abs(y1 - y2)
}
function findClosest(p1) {
const distances = coords.map(p2 => distance(p1, p2))
const shortest = Math.min(...distances)
const count = distances.filter(d => d == shortest).length
if (count == 1) return distances.indexOf(shortest)
}
function onEdge(x, y) {
return x == maxLeft || x == maxRight || y == maxTop || y == maxBottom
}
function allDistances(p1) {
return coords.reduce((acc, p2) => acc + distance(p1, p2), 0)
}
counts = coords.map(_ => 0)
infinites = coords.map(_ => false)
for (let y = maxTop; y <= maxBottom; y++) {
for (let x = maxLeft; x <= maxRight; x++) {
const closest = findClosest([x,y])
if (closest != null) {
counts[closest]++
if (onEdge(x,y)) infinites[closest] = true
}
}
}
finiteCounts = counts.map((count, index) => infinites[index] ? 0 : count)
star1 = Math.max(...finiteCounts)
console.log(star1)
safeRegion = 0
maxDistance = 10000
for (let y = maxTop; y <= maxBottom; y++) {
for (let x = maxLeft; x <= maxRight; x++) {
if (allDistances([x, y]) < maxDistance) safeRegion++
}
}
star2 = safeRegion
console.log(star2)