Day 19 functional

We're at about 1s runtime on my macbook, but we do get the right answer now.
This commit is contained in:
2022-06-21 00:11:22 -05:00
parent cbaab27054
commit 9947a9b559

View File

@ -61,120 +61,62 @@ func (d *Day19) Part1() string {
} }
func (d *Day19) Part2() string { func (d *Day19) Part2() string {
grid := make([][]bool, 5000) f := func(x, y int) bool {
for y := 0; y < len(grid); y++ { ret := false
grid[y] = make([]bool, 5000) d.program.Reset()
d.program.RunIn(func(inputStep int) int64 {
if inputStep == 1 {
return int64(x)
}
return int64(y)
}, func(val int64, state u.IntcodeProgramState) {
ret = val == 1
})
return ret
} }
// find first hit // find lower bound
firstY := 0 startY := 0
for y := 1; firstY == 0; y++ { startX := 0
for y := 1; startY == 0; y++ {
for x := 0; x < 10*y; x++ { for x := 0; x < 10*y; x++ {
d.program.Reset() if f(x, y) {
d.program.RunIn(func(inputStep int) int64 { startY = y
if inputStep == 1 { startX = x
return int64(x) break
} }
return int64(y)
}, func(val int64, state u.IntcodeProgramState) {
if val == 1 {
if firstY == 0 {
firstY = y
d.program.Stop()
}
}
})
} }
} }
done := false done := false
y := firstY for y := startY; !done; y++ {
x := 0 foundX := false
firstFoundX := 0 for x := startX; ; x++ {
firstFoundY := 0 if !f(x, y) {
lastX := 0 if !foundX {
lastY := 0 continue
for y = firstY; !done; y++ { } else {
lastFoundX := -1 break
for x = firstFoundX; lastFoundX == -1 || lastFoundX == x-1; x++ {
d.program.Reset()
d.program.RunIn(func(inputStep int) int64 {
if inputStep == 1 {
return int64(x)
}
return int64(y)
}, func(val int64, state u.IntcodeProgramState) {
res := val == 1
grid[y][x] = res
if res {
if lastFoundX == -1 {
firstFoundX = x
}
lastFoundX = x
}
})
}
if lastFoundX-firstFoundX+1 >= 100 {
if firstFoundY == 0 {
firstFoundY = y
}
done = grid[y-100][firstFoundX+100]
if done {
for checkY := y - 100; checkY <= y; checkY++ {
if !grid[checkY][firstFoundX+100] || !grid[checkY][firstFoundX] {
done = false
break
}
}
if done {
lastX = firstFoundX + 100
lastY = y
} }
} }
} if !foundX {
foundX = true
startX = x
}
// if lastFoundX-firstFoundX+1 >= 100 { if !f(x+99, y) {
// fmt.Printf("At y=%d, x reached %d for %d in a row\n", y, lastFoundX, lastFoundX-firstFoundX+1)
// }
}
for y := firstFoundY; y <= lastY; y++ {
for x = 0; ; x++ {
if grid[y][x] {
break break
} }
} if !f(x, y+99) {
continue
found := true
for y2 := y + 1; y2 < y+100; y2++ {
if !grid[y2][x] {
found = false
break
} }
}
if found { fmt.Printf("x=%d, y=%d, so result=%d\n", x, y, (x*10000)+y)
fmt.Printf("100 down, 100 across starts at y=%d, x=%d\n", y, x) done = true
break
} }
} }
// fmt.Println("part 2 tractor view:") return "0"
// for y := 0; y < len(grid); y++ {
// for x := 0; x < len(grid[y]); x++ {
// if grid[y][x] {
// fmt.Print("█")
// } else {
// fmt.Print(" ")
// }
// }
// fmt.Println()
// }
origX := lastX - 100
origY := lastY - 100
result := (origX * 10000) + origY
return fmt.Sprintf("x=%d, y=%d, orig x=%d, orig y=%d, %s%d%s", lastX, lastY, origX, origY, u.TextBold, result, u.TextReset)
} }