diff --git a/day15/day15.go b/day15/day15.go index 3e9a87b..09b44b9 100644 --- a/day15/day15.go +++ b/day15/day15.go @@ -3,30 +3,60 @@ package main import ( "AOC2022/helper" "fmt" + "os" "strconv" "strings" ) func main() { - //args := os.Args[1:] - lines := helper.ReadTextFile("day15/input") + args := os.Args[1:] + lines := helper.ReadTextFile(args[0]) 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}) + scannedPoints := getScannedPointsForTagetY(points, [2]int{2000000, 2000000}, [2]int{-100000000, 100000000}) + fmt.Println(len(scannedPoints) - 1) + part2(points) +} + +// Fichtes Solution +func part2(points [][2][2]int) { + scannerWithDistances := [][3]int{} + for _, scannedPoint := range points { + distance := helper.ManHattanDistance(scannedPoint[0], scannedPoint[1]) + scannerWithDistances = append(scannerWithDistances, [3]int{scannedPoint[0][0], scannedPoint[0][1], distance}) + } + startPoint := [2]int{0, 0} + finish := false + for !finish { + scanner := checkIfPointIsInScannerReach(startPoint, scannerWithDistances) + if scanner[2] == -1 { + finish = true + } else { + distanceToTargetY := helper.Abs(scanner[1] - startPoint[1]) + widthOnTargetY := helper.Abs(distanceToTargetY - scanner[2]) + newXValue := scanner[0] + widthOnTargetY + 1 + if newXValue > 4000000 { + startPoint = [2]int{0, startPoint[1] + 1} + } else { + startPoint[0] = newXValue } } } + fmt.Println(startPoint[0]*4000000 + startPoint[1]) +} + +func checkIfPointIsInScannerReach(point [2]int, scanners [][3]int) [3]int { + for _, scannerWithDistance := range scanners { + if helper.ManHattanDistance(point, [2]int{scannerWithDistance[0], scannerWithDistance[1]}) <= scannerWithDistance[2] { + return scannerWithDistance + } + } + return [3]int{0, 0, -1} } 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 { + for _, pointPair := range points { sensor := pointPair[0] beacon := pointPair[1] distanceToSensor := helper.ManHattanDistance(sensor, beacon) @@ -58,7 +88,6 @@ func getScannedPointsForTagetY(points [][2][2]int, targetyRange [2]int, targetxR i-- } } - fmt.Println(rotation) } return scannedYPoints }