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 {
grid := make([][]bool, 5000)
for y := 0; y < len(grid); y++ {
grid[y] = make([]bool, 5000)
f := func(x, y int) bool {
ret := false
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
firstY := 0
for y := 1; firstY == 0; y++ {
// find lower bound
startY := 0
startX := 0
for y := 1; startY == 0; y++ {
for x := 0; x < 10*y; 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) {
if val == 1 {
if firstY == 0 {
firstY = y
d.program.Stop()
}
}
})
if f(x, y) {
startY = y
startX = x
break
}
}
}
done := false
y := firstY
x := 0
firstFoundX := 0
firstFoundY := 0
lastX := 0
lastY := 0
for y = firstY; !done; y++ {
lastFoundX := -1
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
for y := startY; !done; y++ {
foundX := false
for x := startX; ; x++ {
if !f(x, y) {
if !foundX {
continue
} else {
break
}
}
}
if !foundX {
foundX = true
startX = x
}
// if lastFoundX-firstFoundX+1 >= 100 {
// 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] {
if !f(x+99, y) {
break
}
}
found := true
for y2 := y + 1; y2 < y+100; y2++ {
if !grid[y2][x] {
found = false
break
if !f(x, y+99) {
continue
}
}
if found {
fmt.Printf("100 down, 100 across starts at y=%d, x=%d\n", y, x)
fmt.Printf("x=%d, y=%d, so result=%d\n", x, y, (x*10000)+y)
done = true
break
}
}
// fmt.Println("part 2 tractor view:")
// 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)
return "0"
}