-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathday-12-2.go
167 lines (151 loc) · 3.14 KB
/
day-12-2.go
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package main
import (
"fmt"
"io/ioutil"
"regexp"
"strconv"
"strings"
)
type Point struct {
x int
y int
z int
}
type Moon struct {
id int
pos Point
vel Point
}
func Abs(n int) int {
if n < 0 {
return -n
}
return n
}
func (m *Moon) updateVel(moons []Moon) {
for _, other := range moons {
if m.id == other.id {
continue
}
dx := other.pos.x - m.pos.x
dy := other.pos.y - m.pos.y
dz := other.pos.z - m.pos.z
if dx != 0 {
dx = dx / Abs(dx)
}
if dy != 0 {
dy = dy / Abs(dy)
}
if dz != 0 {
dz = dz / Abs(dz)
}
m.vel.x += dx
m.vel.y += dy
m.vel.z += dz
}
}
func (m *Moon) updatePos() {
m.pos.x += m.vel.x
m.pos.y += m.vel.y
m.pos.z += m.vel.z
}
func (m Moon) energy() int {
potential := Abs(m.pos.x) + Abs(m.pos.y) + Abs(m.pos.z)
kinetic := Abs(m.vel.x) + Abs(m.vel.y) + Abs(m.vel.z)
return potential * kinetic
}
func Gcd(x, y int) int {
if y == 0 {
return x
}
return Gcd(y, x%y)
}
func Lcm3(x, y, z int) int {
return Lcm(x, Lcm(y, z))
}
func Lcm(a, b int) int {
return a * b / Gcd(a, b)
}
type PosVelKey struct {
pos [4]int
vel [4]int
}
func main() {
input, err := ioutil.ReadFile("input-12")
if err != nil {
fmt.Println(err)
return
}
input_as_str := string(input)
lines := strings.Split(input_as_str, "\n")
moons := make([]Moon, 4)
input_cleanup := regexp.MustCompile("[^0-9,-]")
for i, line := range lines {
if i > 3 {
continue
}
line = input_cleanup.ReplaceAllString(line, "")
points_as_str := strings.Split(line, ",")
moons[i].pos.x, _ = strconv.Atoi(points_as_str[0])
moons[i].pos.y, _ = strconv.Atoi(points_as_str[1])
moons[i].pos.z, _ = strconv.Atoi(points_as_str[2])
moons[i].id = i
}
hasLoopX := false
hasLoopY := false
hasLoopZ := false
seenX := make(map[PosVelKey]int)
seenY := make(map[PosVelKey]int)
seenZ := make(map[PosVelKey]int)
loopX := 0
loopY := 0
loopZ := 0
startLoopX := 0
startLoopY := 0
startLoopZ := 0
for i := 0; i < 10000000; i++ {
if hasLoopX && hasLoopY && hasLoopZ {
break
}
for k, _ := range moons {
moons[k].updateVel(moons)
}
for k, _ := range moons {
moons[k].updatePos()
}
if !hasLoopX {
k := PosVelKey{[4]int{moons[0].pos.x, moons[1].pos.x, moons[2].pos.x, moons[3].pos.x}, [4]int{moons[0].vel.x, moons[1].vel.x, moons[2].vel.x, moons[3].vel.x}}
if xs, ok := seenX[k]; ok {
hasLoopX = true
startLoopX = xs
loopX = i - xs
} else {
seenX[k] = i
}
}
if !hasLoopY {
k := PosVelKey{[4]int{moons[0].pos.y, moons[1].pos.y, moons[2].pos.y, moons[3].pos.y}, [4]int{moons[0].vel.y, moons[1].vel.y, moons[2].vel.y, moons[3].vel.y}}
if xs, ok := seenY[k]; ok {
hasLoopY = true
startLoopY = xs
loopY = i - xs
} else {
seenY[k] = i
}
}
if !hasLoopZ {
k := PosVelKey{[4]int{moons[0].pos.z, moons[1].pos.z, moons[2].pos.z, moons[3].pos.z}, [4]int{moons[0].vel.z, moons[1].vel.z, moons[2].vel.z, moons[3].vel.z}}
if xs, ok := seenZ[k]; ok {
hasLoopZ = true
startLoopZ = xs
loopZ = i - xs
} else {
seenZ[k] = i
}
}
}
fmt.Println(startLoopX, loopX)
fmt.Println(startLoopY, loopY)
fmt.Println(startLoopZ, loopZ)
fmt.Println(Lcm3(loopX, loopY, loopZ))
}