2018 day23 crazy shrinking box solution, kind of a binary search

This commit is contained in:
alexchao26
2020-12-07 22:55:43 -05:00
parent 49de18c919
commit 9f0e632b62
3 changed files with 1209 additions and 0 deletions
+1000
View File
File diff suppressed because it is too large Load Diff
+147
View File
@@ -0,0 +1,147 @@
package main
import (
"flag"
"fmt"
"math"
"strings"
"github.com/alexchao26/advent-of-code-go/util"
)
func main() {
var part int
flag.IntVar(&part, "part", 1, "part 1 or 2")
flag.Parse()
fmt.Println("Running part", part)
if part == 1 {
ans := part1(util.ReadFile("./input.txt"))
fmt.Println("Output:", ans)
} else {
ans := part2(util.ReadFile("./input.txt"))
fmt.Println("Output:", ans)
}
}
func part1(input string) int {
bots := parseInput(input)
strongestBot := bots[0]
for _, b := range bots {
if b.strength > strongestBot.strength {
strongestBot = b
}
}
var withinRange int
for _, b := range bots {
if manhattanDist(b.coords, strongestBot.coords) <= strongestBot.strength {
withinRange++
}
}
return withinRange
}
func part2(input string) int {
bots := parseInput(input)
// get the bounds of the cube that all bots are inside of
// the answer coordinate will be within this space
var minCoord, maxCoord [3]int
for i := range minCoord {
minCoord[i] = math.MaxInt16
}
for _, b := range bots {
for i := 0; i < 3; i++ {
if minCoord[i] > b.coords[i] {
minCoord[i] = b.coords[i]
}
if maxCoord[i] < b.coords[i] {
maxCoord[i] = b.coords[i]
}
}
}
var origin [3]int
// width of the box
boxWidth := maxCoord[0] - minCoord[0]
// 1. width is used to calculate the eight corners to check.
// 2. the reachable bots are counted from each corner
// 3. on each iteration, the box is centered around the best corner and the
// width is cut in half until it reaches zero
var bestGrid [3]int
for boxWidth > 0 {
var maxCount int
for x := minCoord[0]; x < maxCoord[0]+1; x += boxWidth {
for y := minCoord[1]; y < maxCoord[1]+1; y += boxWidth {
for z := minCoord[2]; z < maxCoord[2]+1; z += boxWidth {
current := [3]int{x, y, z}
var countInRange int
for _, b := range bots {
if b.canReach(current) {
countInRange++
}
}
if maxCount < countInRange ||
(maxCount == countInRange && manhattanDist(bestGrid, origin) > manhattanDist(current, origin)) {
maxCount = countInRange
bestGrid = current
}
}
}
}
// adjust box size, i.e. min and max coords
for i := 0; i < 3; i++ {
minCoord[i] = bestGrid[i] - boxWidth
maxCoord[i] = bestGrid[i] + boxWidth
}
// shrink searchable box size
boxWidth /= 2
}
return manhattanDist(bestGrid, origin)
}
type nanobot struct {
coords [3]int
strength int
}
func (b nanobot) canReach(coord [3]int) bool {
return manhattanDist(coord, b.coords) <= b.strength
}
func parseInput(input string) []nanobot {
var bots []nanobot
lines := strings.Split(input, "\n")
for _, l := range lines {
bot := nanobot{}
_, err := fmt.Sscanf(l, "pos=<%d,%d,%d>, r=%d", &bot.coords[0], &bot.coords[1], &bot.coords[2], &bot.strength)
if err != nil {
panic("parsing input line " + err.Error())
}
bots = append(bots, bot)
}
return bots
}
func manhattanDist(one, two [3]int) int {
var dist int
for i := range one {
diff := one[i] - two[i]
if diff < 0 {
diff *= -1
}
dist += diff
}
return dist
}
+62
View File
@@ -0,0 +1,62 @@
package main
import (
"testing"
"github.com/alexchao26/advent-of-code-go/util"
)
var example = `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`
var tests1 = []struct {
name string
want int
input string
}{
{"example", 7, example},
{"actual", 573, util.ReadFile("input.txt")},
}
func TestPart1(t *testing.T) {
for _, tt := range tests1 {
t.Run(tt.name, func(t *testing.T) {
if got := part1(tt.input); got != tt.want {
t.Errorf("part1() = %v, want %v", got, tt.want)
}
})
}
}
var example2 = `pos=<10,12,12>, r=2
pos=<12,14,12>, r=2
pos=<16,12,12>, r=4
pos=<14,14,14>, r=6
pos=<50,50,50>, r=200
pos=<10,10,10>, r=5`
var tests2 = []struct {
name string
want int
input string
}{
{"example", 36, example2},
{"actual", 107279292, util.ReadFile("input.txt")},
}
func TestPart2(t *testing.T) {
for _, tt := range tests2 {
t.Run(tt.name, func(t *testing.T) {
if got := part2(tt.input); got != tt.want {
t.Errorf("part2() = %v, want %v", got, tt.want)
}
})
}
}