-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday12.ts
60 lines (51 loc) · 1.41 KB
/
day12.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
import * as fs from "fs"
const input = fs.readFileSync("input-day12.txt", "utf8")
function parse(input: string): Vector[] {
const regEx = /\<x=(-?\d+), y=(-?\d+), z=(-?\d+)\>/g
let m: RegExpExecArray
const vs = []
while ((m = regEx.exec(input)) !== null) {
vs.push([m[1], m[2], m[3]].map(Number))
}
return vs
}
type Vector = [number, number, number]
function add(v1: Vector, v2: Vector): Vector {
return [v1[0] + v2[0], v1[1] + v2[1], v1[2] + v2[2]]
}
function sub(v1: Vector, v2: Vector): Vector {
return [v1[0] - v2[0], v1[1] - v2[1], v1[2] - v2[2]]
}
function sign(v1: Vector): Vector {
return [v1[0], v1[1], v1[2]].map(Math.sign) as Vector
}
function len(v1: Vector): number {
return v1.map(Math.abs).reduce((a, v) => a + v, 0)
}
let ps = parse(input)
let vs = ps.map(_ => <Vector>[0, 0, 0])
for (let i = 0; i < 1000; i++) {
vs = gravity()
ps = move()
console.log(`after step ${i + 1}`)
console.log(ps)
console.log(vs)
console.log()
}
const energy = ps.map((p, i) => len(p) * len(vs[i])).reduce((a, v) => a + v, 0)
console.log(energy)
function gravity(): Vector[] {
ps.forEach((p1, i) => {
ps.forEach((p2, j) => {
if (i == j) {
return
}
const v = vs[i]
vs[i] = add(v, sign(sub(p2, p1)))
})
})
return vs
}
function move() {
return ps.map((p, i) => add(p, vs[i]))
}