2022 Day 15, part 1 complete
Going home from C&C
This commit is contained in:
127
2022/day15/main.go
Normal file
127
2022/day15/main.go
Normal file
@@ -0,0 +1,127 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
h "git.bullercodeworks.com/brian/adventofcode/helpers"
|
||||
)
|
||||
|
||||
var part = -1
|
||||
var testRow = 2000000
|
||||
var watch bool
|
||||
var watchRows = 1
|
||||
|
||||
func main() {
|
||||
inp := h.StdinToStringSlice()
|
||||
if len(os.Args) > 1 {
|
||||
for _, arg := range os.Args[1:] {
|
||||
if strings.HasPrefix(arg, "--part") || strings.HasPrefix(arg, "-p") {
|
||||
if strings.Contains(arg, "=") {
|
||||
pts := strings.Split(arg, "=")
|
||||
part = h.Atoi(pts[1])
|
||||
}
|
||||
} else if strings.HasPrefix(arg, "--row") || strings.HasPrefix(arg, "-r") {
|
||||
if strings.Contains(arg, "=") {
|
||||
pts := strings.Split(arg, "=")
|
||||
testRow = h.Atoi(pts[1])
|
||||
}
|
||||
} else if strings.HasPrefix(arg, "--watch") || strings.HasPrefix(arg, "-w") {
|
||||
watch = true
|
||||
if strings.Contains(arg, "=") {
|
||||
pts := strings.Split(arg, "=")
|
||||
if pts[1] == "full" {
|
||||
watchRows = -1
|
||||
} else {
|
||||
watchRows = h.Atoi(pts[1])
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
part1(inp)
|
||||
part2(inp)
|
||||
}
|
||||
|
||||
func part1(inp []string) {
|
||||
min_x, max_x := math.MaxInt, math.MinInt
|
||||
var spots [][]int
|
||||
for i := range inp {
|
||||
s, b := strToCoords(inp[i])
|
||||
xs, ys, xb, yb := s.X, s.Y, b.X, b.Y
|
||||
dist := s.Distance(b)
|
||||
l, r := xs-dist, xs+dist
|
||||
min_x = h.Min(min_x, l)
|
||||
max_x = h.Max(max_x, r)
|
||||
spots = append(spots, []int{xs, ys, xb, yb, dist})
|
||||
}
|
||||
var nopeCount int
|
||||
for x := min_x; x <= max_x; x++ {
|
||||
for _, c := range spots {
|
||||
if x == c[2] && testRow == c[3] {
|
||||
break
|
||||
}
|
||||
if h.ManhattanDistance(x, testRow, c[0], c[1]) <= c[4] {
|
||||
nopeCount++
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
fmt.Println("# Part 1")
|
||||
fmt.Println(nopeCount)
|
||||
}
|
||||
|
||||
func part2(inp []string) {
|
||||
min_x, max_x := math.MaxInt, math.MinInt
|
||||
var spots [][]int
|
||||
for i := range inp {
|
||||
s, b := strToCoords(inp[i])
|
||||
xs, ys, xb, yb := s.X, s.Y, b.X, b.Y
|
||||
dist := s.Distance(b)
|
||||
l, r := xs-dist, xs+dist
|
||||
min_x = h.Min(min_x, l)
|
||||
max_x = h.Max(max_x, r)
|
||||
spots = append(spots, []int{xs, ys, xb, yb, dist})
|
||||
}
|
||||
|
||||
max := testRow * 2
|
||||
var found h.Coordinate
|
||||
SEARCH:
|
||||
for y := 0; y <= max; y++ {
|
||||
for x := 0; x <= max; x++ {
|
||||
fmt.Printf("Testing %d,%d...", x, y)
|
||||
var nope bool
|
||||
for _, c := range spots {
|
||||
if x == c[2] && y == c[3] {
|
||||
break
|
||||
}
|
||||
if h.ManhattanDistance(x, y, c[0], c[1]) <= c[4] {
|
||||
nope = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !nope {
|
||||
fmt.Println("Yup")
|
||||
found = h.Coordinate{X: x, Y: y}
|
||||
break SEARCH
|
||||
}
|
||||
fmt.Println("Nope")
|
||||
}
|
||||
}
|
||||
fmt.Println("# Part 2")
|
||||
fmt.Println(found)
|
||||
fmt.Println((found.X * 4000000) + found.Y)
|
||||
}
|
||||
|
||||
func printMap(m [][]int) {
|
||||
}
|
||||
|
||||
func strToCoords(s string) (h.Coordinate, h.Coordinate) {
|
||||
var sensor, beacon h.Coordinate
|
||||
r := strings.NewReader(s)
|
||||
fmt.Fscanf(r, "Sensor at x=%d, y=%d: closest beacon is at x=%d, y=%d", &sensor.X, &sensor.Y, &beacon.X, &beacon.Y)
|
||||
return sensor, beacon
|
||||
}
|
||||
Reference in New Issue
Block a user