Switching pcs
This commit is contained in:
103
2018/day23/day23.go
Normal file
103
2018/day23/day23.go
Normal file
@@ -0,0 +1,103 @@
|
||||
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
|
||||
}
|
1000
2018/day23/input
Normal file
1000
2018/day23/input
Normal file
File diff suppressed because it is too large
Load Diff
9
2018/day23/testinput
Normal file
9
2018/day23/testinput
Normal file
@@ -0,0 +1,9 @@
|
||||
pos=<0,0,0>, r=4
|
||||
pos=<1,0,0>, r=1
|
||||
pos=<4,0,0>, r=3
|
||||
pos=<0,2,0>, r=1
|
||||
pos=<0,5,0>, r=3
|
||||
pos=<0,0,3>, r=1
|
||||
pos=<1,1,1>, r=1
|
||||
pos=<1,1,2>, r=1
|
||||
pos=<1,3,1>, r=1
|
Reference in New Issue
Block a user