-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathab.ts
76 lines (56 loc) · 1.82 KB
/
ab.ts
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
import { minBy } from 'lodash'
import { readInput } from '../helpers'
const calcManhattan = ([x, y]: number[], [xx, yy]: number[]): number => {
return Math.abs(x - xx) + Math.abs(y - yy)
}
const lines: string[] = readInput('06', 'input')
const xCollection = []
const yCollection = []
const areas: { [key: string]: number } = {}
const coords: number[][] = []
let safeCount = 0
// Determine bounds
for (const coord of lines) {
const [x, y]: string[] = coord.split(', ')
xCollection.push(+x)
yCollection.push(+y)
areas[`${x},${y}`] = 0
coords.push([+x, +y])
}
const xMin = Math.min(...xCollection)
const xMax = Math.max(...xCollection)
const yMin = Math.min(...yCollection)
const yMax = Math.max(...yCollection)
for (let x = xMin; x < xMax + 1; x++) {
for (let y = yMin; y < yMax + 1; y++) {
const distances: { [key: number]: string[] } = {}
for (const [xx, yy] of coords) {
const distance = calcManhattan([x, y], [xx, yy])
if (!distances.hasOwnProperty(distance)) {
distances[distance] = []
}
distances[distance].push(`${xx},${yy}`)
}
const sum = coords
.map(([xx, yy]: number[]) => calcManhattan([x, y], [xx, yy]))
.reduce((dist: number, total: number) => dist + total, 0)
if (sum < 10000) {
safeCount += 1
}
const [shortestDistance, closest]: [string, string[]] = minBy(
Object.entries(distances), ([dist]: [string, string[]]) => +dist
)!
if (closest.length > 1) {
continue
}
// Remove from possible areas if one of the coords is on bounds
if (x === xMin || x === xMax || y === yMin || y === yMax) {
delete areas[closest[0]]
}
if (areas.hasOwnProperty(closest[0])) {
areas[closest[0]] += 1
}
}
}
console.log('Part 1:', Math.max(...Object.values(areas)))
console.log('Part 2:', safeCount)