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,15 +61,8 @@ 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)
}
// find first hit
firstY := 0
for y := 1; firstY == 0; y++ {
for x := 0; x < 10*y; x++ {
d.program.Reset() d.program.Reset()
d.program.RunIn(func(inputStep int) int64 { d.program.RunIn(func(inputStep int) int64 {
if inputStep == 1 { if inputStep == 1 {
@ -77,104 +70,53 @@ func (d *Day19) Part2() string {
} }
return int64(y) return int64(y)
}, func(val int64, state u.IntcodeProgramState) { }, func(val int64, state u.IntcodeProgramState) {
if val == 1 { ret = val == 1
if firstY == 0 {
firstY = y
d.program.Stop()
}
}
}) })
return ret
}
// find lower bound
startY := 0
startX := 0
for y := 1; startY == 0; y++ {
for x := 0; x < 10*y; x++ {
if f(x, y) {
startY = y
startX = x
break
}
} }
} }
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 !foundX {
if inputStep == 1 { foundX = true
return int64(x) startX = x
} }
return int64(y) if !f(x+99, y) {
}, func(val int64, state u.IntcodeProgramState) { break
res := val == 1
grid[y][x] = res
if res {
if lastFoundX == -1 {
firstFoundX = x
} }
lastFoundX = x if !f(x, y+99) {
} continue
})
} }
if lastFoundX-firstFoundX+1 >= 100 { fmt.Printf("x=%d, y=%d, so result=%d\n", x, y, (x*10000)+y)
if firstFoundY == 0 { done = true
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 break
} }
} }
if done { return "0"
lastX = firstFoundX + 100
lastY = y
}
}
}
// 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] {
break
}
}
found := true
for y2 := y + 1; y2 < y+100; y2++ {
if !grid[y2][x] {
found = false
break
}
}
if found {
fmt.Printf("100 down, 100 across starts at y=%d, x=%d\n", y, x)
}
}
// 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)
} }