-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
118 lines (102 loc) · 2.33 KB
/
main.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
package main
import (
"bufio"
"io"
"math"
lib "github.com/teivah/advent-of-code"
)
func fs1(moves int, input io.Reader) int {
scanner := bufio.NewScanner(input)
var particles []*Particle
for scanner.Scan() {
line := scanner.Text()
spaceDel := lib.NewDelimiter(line, " ")
particles = append(particles, &Particle{
position: toCoord(line[3 : spaceDel.Ind[0]-2]),
velocity: toCoord(line[spaceDel.Ind[0]+4 : spaceDel.Ind[1]-2]),
acceleration: toCoord(line[spaceDel.Ind[1]+4 : len(line)-1]),
})
}
closest := make(map[int]int)
for move := 0; move < moves; move++ {
minDistance := math.MaxInt
res := -1
for i, particle := range particles {
particle.move()
distance := particle.manhattan()
if distance < minDistance {
minDistance = distance
res = i
}
}
closest[res]++
}
best := -1
max := -1
for id, v := range closest {
if v > max {
max = v
best = id
}
}
return best
}
type Particle struct {
position Coord
velocity Coord
acceleration Coord
}
func (p *Particle) move() {
p.velocity.x += p.acceleration.x
p.velocity.y += p.acceleration.y
p.velocity.z += p.acceleration.z
p.position.x += p.velocity.x
p.position.y += p.velocity.y
p.position.z += p.velocity.z
}
func (p *Particle) manhattan() int {
return lib.Abs(p.position.x) + lib.Abs(p.position.y) + lib.Abs(p.position.z)
}
type Coord struct {
x int
y int
z int
}
func toCoord(s string) Coord {
del := lib.NewDelimiter(s, ",")
return Coord{
x: del.GetInt(0),
y: del.GetInt(1),
z: del.GetInt(2),
}
}
func fs2(moves int, input io.Reader) int {
scanner := bufio.NewScanner(input)
particles := make(map[int]*Particle)
i := -1
for scanner.Scan() {
i++
line := scanner.Text()
spaceDel := lib.NewDelimiter(line, " ")
particles[i] = &Particle{
position: toCoord(line[3 : spaceDel.Ind[0]-2]),
velocity: toCoord(line[spaceDel.Ind[0]+4 : spaceDel.Ind[1]-2]),
acceleration: toCoord(line[spaceDel.Ind[1]+4 : len(line)-1]),
}
}
for move := 0; move < moves; move++ {
positions := make(map[Coord][]int)
for id, particle := range particles {
particle.move()
positions[particle.position] = append(positions[particle.position], id)
}
for _, ids := range positions {
if len(ids) > 1 {
for _, id := range ids {
delete(particles, id)
}
}
}
}
return len(particles)
}