104 lines
1.7 KiB
Go
104 lines
1.7 KiB
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"bufio"
|
||
|
"fmt"
|
||
|
"log"
|
||
|
"math"
|
||
|
"os"
|
||
|
"strconv"
|
||
|
"strings"
|
||
|
)
|
||
|
|
||
|
var collective *Collective
|
||
|
|
||
|
func main() {
|
||
|
input := stdinToStringSlice()
|
||
|
|
||
|
collective = &Collective{}
|
||
|
for _, v := range input {
|
||
|
collective.bots = append(collective.bots, NewBot(v))
|
||
|
}
|
||
|
part1()
|
||
|
}
|
||
|
|
||
|
func part1() {
|
||
|
s := collective.strongest()
|
||
|
fmt.Println("= Part 1 =")
|
||
|
fmt.Println(len(collective.inRange(s)))
|
||
|
}
|
||
|
|
||
|
func part2() {
|
||
|
finder := &Bot{}
|
||
|
}
|
||
|
|
||
|
type Collective struct {
|
||
|
bots []*Bot
|
||
|
}
|
||
|
|
||
|
func (c *Collective) inRange(b *Bot) []*Bot {
|
||
|
var ret []*Bot
|
||
|
for _, v := range c.bots {
|
||
|
if c.distance(b, v) <= b.radius {
|
||
|
ret = append(ret, v)
|
||
|
}
|
||
|
}
|
||
|
return ret
|
||
|
}
|
||
|
|
||
|
func (c *Collective) strongest() *Bot {
|
||
|
strongest := c.bots[0]
|
||
|
for _, v := range c.bots {
|
||
|
if v.radius > strongest.radius {
|
||
|
strongest = v
|
||
|
}
|
||
|
}
|
||
|
return strongest
|
||
|
}
|
||
|
|
||
|
func (c *Collective) distance(b1, b2 *Bot) int {
|
||
|
xs := math.Abs(float64(b1.x) - float64(b2.x))
|
||
|
ys := math.Abs(float64(b1.y) - float64(b2.y))
|
||
|
zs := math.Abs(float64(b1.z) - float64(b2.z))
|
||
|
return int(xs + ys + zs)
|
||
|
}
|
||
|
|
||
|
type Bot struct {
|
||
|
x, y, z int
|
||
|
radius int
|
||
|
}
|
||
|
|
||
|
func NewBot(args string) *Bot {
|
||
|
pts := strings.Split(args, ", ")
|
||
|
pos := strings.Split(pts[0][5:len(pts[0])-1], ",")
|
||
|
r := strings.Split(pts[1], "=")[1]
|
||
|
return &Bot{
|
||
|
x: atoi(pos[0]),
|
||
|
y: atoi(pos[1]),
|
||
|
z: atoi(pos[2]),
|
||
|
radius: atoi(r),
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (b *Bot) string() string {
|
||
|
return fmt.Sprintf("(%d,%d,%d : %d)", b.x, b.y, b.z, b.radius)
|
||
|
}
|
||
|
|
||
|
func stdinToStringSlice() []string {
|
||
|
var input []string
|
||
|
scanner := bufio.NewScanner(os.Stdin)
|
||
|
for scanner.Scan() {
|
||
|
input = append(input, scanner.Text())
|
||
|
}
|
||
|
return input
|
||
|
}
|
||
|
|
||
|
func atoi(i string) int {
|
||
|
var ret int
|
||
|
var err error
|
||
|
if ret, err = strconv.Atoi(i); err != nil {
|
||
|
log.Fatal("Invalid Atoi")
|
||
|
}
|
||
|
return ret
|
||
|
}
|