diff --git a/day15/day15.go b/day15/day15.go new file mode 100644 index 0000000..3e9a87b --- /dev/null +++ b/day15/day15.go @@ -0,0 +1,99 @@ +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 +} diff --git a/day15/input b/day15/input new file mode 100644 index 0000000..7f37eaa --- /dev/null +++ b/day15/input @@ -0,0 +1,33 @@ +Sensor at x=1943362, y=12808: closest beacon is at x=1861152, y=-42022 +Sensor at x=906633, y=3319637: closest beacon is at x=2096195, y=3402757 +Sensor at x=2358896, y=2158796: closest beacon is at x=2331052, y=2934800 +Sensor at x=1787606, y=3963631: closest beacon is at x=2096195, y=3402757 +Sensor at x=2282542, y=3116014: closest beacon is at x=2331052, y=2934800 +Sensor at x=173912, y=1873897: closest beacon is at x=429790, y=2000000 +Sensor at x=3391153, y=3437167: closest beacon is at x=3720655, y=3880705 +Sensor at x=3834843, y=2463103: closest beacon is at x=2971569, y=2563051 +Sensor at x=3917316, y=3981011: closest beacon is at x=3720655, y=3880705 +Sensor at x=1466100, y=1389028: closest beacon is at x=429790, y=2000000 +Sensor at x=226600, y=3967233: closest beacon is at x=85598, y=4102832 +Sensor at x=1757926, y=2834180: closest beacon is at x=2331052, y=2934800 +Sensor at x=2176953, y=3240563: closest beacon is at x=2096195, y=3402757 +Sensor at x=2883909, y=2533883: closest beacon is at x=2971569, y=2563051 +Sensor at x=376161, y=2533578: closest beacon is at x=429790, y=2000000 +Sensor at x=3015271, y=3913673: closest beacon is at x=3720655, y=3880705 +Sensor at x=490678, y=388548: closest beacon is at x=429790, y=2000000 +Sensor at x=2725765, y=2852933: closest beacon is at x=2331052, y=2934800 +Sensor at x=86373, y=2839828: closest beacon is at x=429790, y=2000000 +Sensor at x=1802070, y=14830: closest beacon is at x=1861152, y=-42022 +Sensor at x=19628, y=1589839: closest beacon is at x=429790, y=2000000 +Sensor at x=2713787, y=3381887: closest beacon is at x=2096195, y=3402757 +Sensor at x=2148471, y=3729393: closest beacon is at x=2096195, y=3402757 +Sensor at x=3999318, y=3263346: closest beacon is at x=3720655, y=3880705 +Sensor at x=575700, y=1390576: closest beacon is at x=429790, y=2000000 +Sensor at x=273266, y=2050976: closest beacon is at x=429790, y=2000000 +Sensor at x=3008012, y=993590: closest beacon is at x=2971569, y=2563051 +Sensor at x=3306379, y=2782128: closest beacon is at x=2971569, y=2563051 +Sensor at x=44975, y=3820788: closest beacon is at x=85598, y=4102832 +Sensor at x=2941700, y=2536797: closest beacon is at x=2971569, y=2563051 +Sensor at x=2040164, y=102115: closest beacon is at x=1861152, y=-42022 +Sensor at x=3928008, y=3692684: closest beacon is at x=3720655, y=3880705 +Sensor at x=3905950, y=222812: closest beacon is at x=4759853, y=-796703 \ No newline at end of file diff --git a/day15/testinput b/day15/testinput new file mode 100644 index 0000000..652e631 --- /dev/null +++ b/day15/testinput @@ -0,0 +1,14 @@ +Sensor at x=2, y=18: closest beacon is at x=-2, y=15 +Sensor at x=9, y=16: closest beacon is at x=10, y=16 +Sensor at x=13, y=2: closest beacon is at x=15, y=3 +Sensor at x=12, y=14: closest beacon is at x=10, y=16 +Sensor at x=10, y=20: closest beacon is at x=10, y=16 +Sensor at x=14, y=17: closest beacon is at x=10, y=16 +Sensor at x=8, y=7: closest beacon is at x=2, y=10 +Sensor at x=2, y=0: closest beacon is at x=2, y=10 +Sensor at x=0, y=11: closest beacon is at x=2, y=10 +Sensor at x=20, y=14: closest beacon is at x=25, y=17 +Sensor at x=17, y=20: closest beacon is at x=21, y=22 +Sensor at x=16, y=7: closest beacon is at x=15, y=3 +Sensor at x=14, y=3: closest beacon is at x=15, y=3 +Sensor at x=20, y=1: closest beacon is at x=15, y=3 \ No newline at end of file diff --git a/helper/helper.go b/helper/helper.go index 22b6e99..d250621 100644 --- a/helper/helper.go +++ b/helper/helper.go @@ -123,3 +123,14 @@ func SetValueOf2DMap[T any](location [2]int, value T, map2D *[][]T) int { (*map2D)[location[0]][location[1]] = value return 0 } + +func Abs[T constraints.Integer](x T) T { + if x < 0 { + return -x + } + return x +} + +func ManHattanDistance(p1, p2 [2]int) int { + return Abs(p1[0]-p2[0]) + Abs(p1[1]-p2[1]) +}