AOC2022/day15/day15.go
2022-12-16 00:29:33 +01:00

100 lines
3.2 KiB
Go

package main
import (
"AOC2022/helper"
"fmt"
"strconv"
"strings"
)
func main() {
//args := os.Args[1:]
lines := helper.ReadTextFile("day15/input")
points := getPoints(lines)
//scannedXPoints := make(map[int]bool)
scannedPoints := getScannedPointsForTagetY(points, [2]int{0, 4000000}, [2]int{0, 4000000})
//fmt.Println(scannedPoints)
fmt.Println(len(scannedPoints))
for i := 0; i <= 4000000; i++ {
for j := 0; j <= 20; j++ {
if !scannedPoints[[2]int{i, j}] {
fmt.Println([2]int{i, j})
}
}
}
}
func getScannedPointsForTagetY(points [][2][2]int, targetyRange [2]int, targetxRange [2]int) map[[2]int]bool {
scannedYPoints := make(map[[2]int]bool)
for rotation, pointPair := range points {
sensor := pointPair[0]
beacon := pointPair[1]
distanceToSensor := helper.ManHattanDistance(sensor, beacon)
distanceToTargetYLowest := helper.Abs(sensor[1] - targetyRange[0])
distanceToTargetYHighest := helper.Abs(sensor[1] - targetyRange[1])
if sensor[1] >= targetyRange[1] && distanceToSensor >= distanceToTargetYHighest {
i := targetyRange[1]
for i >= targetyRange[1]-(distanceToSensor-distanceToTargetYHighest) && i >= targetyRange[0] {
setPointsForTargetY(distanceToSensor, i, targetxRange, sensor, &scannedYPoints)
i--
}
}
if sensor[1] <= targetyRange[0] && distanceToSensor >= distanceToTargetYLowest {
i := targetyRange[0]
for i <= targetyRange[0]+(distanceToSensor-distanceToTargetYLowest) && i <= targetyRange[1] {
setPointsForTargetY(distanceToSensor, i, targetxRange, sensor, &scannedYPoints)
i++
}
}
if sensor[1] >= targetyRange[0] && sensor[1] <= targetyRange[1] {
i := sensor[1]
for i <= targetyRange[1] && i <= sensor[1]+distanceToSensor {
setPointsForTargetY(distanceToSensor, i, targetxRange, sensor, &scannedYPoints)
i++
}
i = sensor[1]
for i >= targetyRange[0] && i >= sensor[1]-distanceToSensor {
setPointsForTargetY(distanceToSensor, i, targetxRange, sensor, &scannedYPoints)
i--
}
}
fmt.Println(rotation)
}
return scannedYPoints
}
func setPointsForTargetY(distanceToSensor, targetY int, targetxRange, sensor [2]int, scannedYPoints *map[[2]int]bool) {
distanceToTargetY := helper.Abs(sensor[1] - targetY)
widthOnTargetY := helper.Abs(distanceToTargetY - distanceToSensor)
i := sensor[0] - widthOnTargetY
if i < targetxRange[0] {
i = targetxRange[0]
}
for i <= sensor[0]+widthOnTargetY && i <= targetxRange[1] {
(*scannedYPoints)[[2]int{i, targetY}] = true
i++
}
}
func getPoints(lines []string) [][2][2]int {
points := make([][2][2]int, len(lines))
for i, line := range lines {
intPair := [2][2]int{}
stringParts := strings.Split(line[12:], ":")
sensorStringNumbers := stringSliceToIntSlice(strings.Split(stringParts[0], ", y="))
beaconStringNumbers := stringSliceToIntSlice(strings.Split(strings.ReplaceAll(stringParts[1], " closest beacon is at x=", ""), ", y="))
intPair[0] = [2]int{sensorStringNumbers[0], sensorStringNumbers[1]}
intPair[1] = [2]int{beaconStringNumbers[0], beaconStringNumbers[1]}
points[i] = intPair
}
return points
}
func stringSliceToIntSlice(input []string) []int {
intSlice := []int{}
for _, str := range input {
intSlice = append(intSlice, helper.RemoveError(strconv.Atoi(str)))
}
return intSlice
}