improved day19 part2 solution

This commit is contained in:
Alex Chao
2020-08-07 09:58:37 -04:00
parent 7e23ba3c00
commit 765b74e940
+26 -58
View File
@@ -25,72 +25,40 @@ func main() {
inputNumbers[i], _ = strconv.Atoi(v) inputNumbers[i], _ = strconv.Atoi(v)
} }
// x, y := 1135, 1625 // wrapper to produce drones
deployDrone := droneFactory(inputNumbers)
allHorizontalRanges := [][2]int{ // follow the bottom edge of the tractor beam
[2]int{0, 1}, for y := 100; ; {
} for x := 0; ; {
// fmt.Printf("Coordinates x: %v, y %v\n", x, y)
for len(allHorizontalRanges) < 5000 { if !deployDrone(x, y) {
startOfLastRange := allHorizontalRanges[len(allHorizontalRanges)-1][0] // cell not being pulled, move right
horizontalRange := getHorizontalRange(inputNumbers, startOfLastRange, len(allHorizontalRanges)) x++
allHorizontalRanges = append(allHorizontalRanges, horizontalRange) // cell being pulled, check 2 spots, 100 up & 100 up and right
} else {
if len(allHorizontalRanges) > 100 { if deployDrone(x, y-99) && deployDrone(x+99, y-99) {
// indexes of the current line and 100 rows up fmt.Println("Top left of 100x100 square is:", x, y-99)
currentIndex := len(allHorizontalRanges) - 1 return
index100RowsUp := len(allHorizontalRanges) - 100 }
// otherwise move down
// check if a square can fit from the start of the current range to the end of the range 100 rows up y++
if allHorizontalRanges[index100RowsUp][1]-allHorizontalRanges[currentIndex][0] >= 99 {
// Print the AoC format
fmt.Println("AoC answer:", allHorizontalRanges[currentIndex][0]*10000+index100RowsUp)
break
} }
} }
} }
} }
func getHorizontalRange(inputNumbers []int, startX, y int) [2]int { // takes in the inputNumbers, returns a function that initializes a new intcode computer
horizontalRange := [2]int{} // returned function takes in x and y values, returns true if that cell is being "pulled"
func droneFactory(inputNumbers []int) func(int, int) bool {
var lastOutput int return func(x, y int) bool {
drone := MakeComputer(inputNumbers)
// step until the output is a one drone.Step(x)
// OR exit condition of 10 cells have all returned zeroes (this will apply for some of the first levels) drone.Step(y)
for x := startX; lastOutput == 0; x++ { lastOutput := drone.Outputs[len(drone.Outputs)-1]
lastOutput = makeDroneAndTest(inputNumbers, x, y)
// when the first element is found that is "pulled", set the start of the range
if lastOutput == 1 {
horizontalRange[0] = x
}
// NOTE edge case for the first few rows if none of the drones are pulled
if x > startX+20 {
return [2]int{0, 0}
}
return lastOutput == 1
} }
for x := horizontalRange[0] + 1; lastOutput != 0; x++ {
lastOutput = makeDroneAndTest(inputNumbers, x, y)
if lastOutput == 0 {
horizontalRange[1] = x - 1
}
}
return horizontalRange
}
func makeDroneAndTest(inputNumbers []int, x, y int) int {
drone := MakeComputer(inputNumbers)
drone.Step(x)
drone.Step(y)
lastOutput := drone.Outputs[len(drone.Outputs)-1]
return lastOutput
} }
/* /*