-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
176 lines (151 loc) · 3.14 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
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
168
169
170
171
172
173
174
175
176
package main
import (
"bufio"
"fmt"
"io"
"math"
"strings"
lib "github.com/teivah/advent-of-code"
)
func fs1(input io.Reader) int {
scanner := bufio.NewScanner(input)
var bots []Bot
maxRange := 0
var maxBot Bot
for scanner.Scan() {
line := scanner.Text()
bot := toBot(line)
bots = append(bots, bot)
if bot.r > maxRange {
maxRange = bot.r
maxBot = bot
}
}
sum := 0
for _, bot := range bots {
if maxBot.hasInRange(bot.pos) {
sum++
}
}
return sum
}
type Bot struct {
pos Position
r int
}
type Position struct {
x int
y int
z int
}
func (p Position) Delta(x, y, z int) Position {
p.x += x
p.y += y
p.z += z
return p
}
func distance(a, b Position) int {
return lib.Abs(a.x-b.x) + lib.Abs(a.y-b.y) + lib.Abs(a.z-b.z)
}
func distanceDiv(a, b Position, div int) int {
return lib.Abs(a.x/div-b.x) + lib.Abs(a.y/div-b.y) + lib.Abs(a.z/div-b.z)
}
func (b Bot) hasInRange(pos Position) bool {
return distance(b.pos, pos) <= b.r
}
func (b Bot) hasInRangeDiv(pos Position, div int) bool {
d := distanceDiv(b.pos, pos, div)
return d <= b.r/div
}
func (b Bot) isInRange(b2 Bot) bool {
return distance(b.pos, b2.pos) <= b2.r
}
func (b Bot) isInRangeDiv(b2 Bot, div int) bool {
return distanceDiv(b.pos, b2.pos, div) <= b2.r/div
}
func toBot(s string) Bot {
coords := s[5:strings.Index(s, ">")]
cdel := lib.NewDelimiter(coords, ",")
space := strings.Index(s, " ")
r := s[space+3:]
return Bot{
pos: Position{
x: cdel.GetInt(0),
y: cdel.GetInt(1),
z: cdel.GetInt(2),
},
r: lib.StringToInt(r),
}
}
func fs2(input io.Reader) int {
scanner := bufio.NewScanner(input)
var bots []Bot
minX := math.MaxInt
minY := math.MaxInt
minZ := math.MaxInt
maxX := math.MinInt
maxY := math.MinInt
maxZ := math.MinInt
for scanner.Scan() {
line := scanner.Text()
bot := toBot(line)
bots = append(bots, bot)
minX = lib.Min(minX, bot.pos.x-bot.r)
minY = lib.Min(minY, bot.pos.y-bot.r)
minZ = lib.Min(minZ, bot.pos.z-bot.r)
maxX = lib.Max(maxX, bot.pos.x+bot.r)
maxY = lib.Max(maxY, bot.pos.y+bot.r)
maxZ = lib.Max(maxZ, bot.pos.z+bot.r)
}
var best Position
max := 0
div := 1_000_000
for x := minX / div; x < maxX/div; x++ {
fmt.Println(x)
for y := minY / div; y < maxY/div; y++ {
for z := minZ / div; z < maxZ/div; z++ {
pos := Position{x, y, z}
inRange := sumInRangeDiv(pos, bots, div)
if inRange > max {
fmt.Println(inRange)
max = inRange
best = pos
}
}
}
}
div /= 10
best = Position{12, 30, 42}
for ; div > 0; div /= 10 {
max = 0
minX = (best.x - 1) * 10
minY = (best.y - 1) * 10
minZ = (best.z - 1) * 10
maxX = (best.x + 1) * 10
maxY = (best.y + 1) * 10
maxZ = (best.z + 1) * 10
for x := minX; x < maxX; x++ {
fmt.Println(x)
for y := minY; y < maxY; y++ {
for z := minZ; z < maxZ; z++ {
pos := Position{x, y, z}
inRange := sumInRangeDiv(pos, bots, div)
if inRange > max {
max = inRange
best = pos
}
}
}
}
}
return distance(Position{}, best)
}
func sumInRangeDiv(pos Position, bots []Bot, div int) int {
sum := 0
for _, b := range bots {
if b.hasInRangeDiv(pos, div) {
sum++
}
}
return sum
}