diff --git a/day13/day13 b/day13/day13 index 187b430..06dc84d 100755 Binary files a/day13/day13 and b/day13/day13 differ diff --git a/day13/main.go b/day13/main.go index 6117303..9c3722d 100644 --- a/day13/main.go +++ b/day13/main.go @@ -9,8 +9,11 @@ import ( "time" "github.com/fatih/color" + termbox "github.com/nsf/termbox-go" ) +var tWidth, tHeight int + // Puzzle 1 Input: 1364 31 39 // Puzzle 1 Test Input: 10 7 4 func main() { @@ -26,6 +29,11 @@ func main() { } f := CreateFloor(1, 1, destX, destY, seed) + if err := termbox.Init(); err != nil { + panic(err) + } + tWidth, tHeight = termbox.Size() + termbox.Close() switch mode { case "solve": if f.Solve(f.start.x, f.start.y, 0, true) { @@ -76,15 +84,7 @@ func (p *Path) Pop() Coord { return ret } -func (p *Path) ContainsCoord(c *Coord) bool { - for i := range p.coords { - if p.coords[i].Equals(c) { - return true - } - } - return false -} -func (p *Path) ContainsCoordXY(x, y int) bool { +func (p *Path) ContainsCoord(x, y int) bool { for i := range p.coords { if p.coords[i].Is(x, y) { return true @@ -122,7 +122,7 @@ func CreateFloor(stX, stY, endX, endY, seed int) *Floor { func (f *Floor) Walk(x, y, dist, maxDist int, print bool) { wrkCoord := Coord{x, y, dist} - if f.IsWall(x, y) || f.testedPath.ContainsCoordXY(x, y) { + if f.IsWall(x, y) || f.testedPath.ContainsCoord(x, y) { return } if dist == maxDist { @@ -182,7 +182,7 @@ func (f *Floor) Solve(x, y, dist int, print bool) bool { if f.end.Is(x, y) { return true } - if f.IsWall(x, y) || f.testedPath.ContainsCoordXY(x, y) { + if f.IsWall(x, y) || f.testedPath.ContainsCoord(x, y) { return false } // Test if there is a shorter path to this coordinate @@ -266,7 +266,7 @@ func (f *Floor) Solve(x, y, dist int, print bool) bool { // So we have a hard cutoff at: var MaxInt = int(^uint(0) >> 1) if len(f.testedPath.coords) >= MaxInt { - fmt.Println("Couldn't find a path.") + fmt.Println("ERROR: Couldn't find a path.") os.Exit(1) } if f.Solve(x+1, y, wrkCoord.dist+1, print) { @@ -295,19 +295,38 @@ func (f *Floor) Print() { g := color.New(color.BgGreen).Add(color.FgBlack) g.Add(color.Bold) r := color.New(color.BgRed) - topY, topX := f.end.y+10, f.end.x+10 - - for y := 0; y < topY; y++ { - for x := 0; x < topX; x++ { + b := color.New(color.BgBlue).Add(color.BgBlack) + b.Add(color.Bold) + topY, topX := tHeight, tWidth + botY, botX := 0, 0 + // We want to center approx 20x20 on our current location + // f.testedPath[len(f.testedPath)-1]? + if len(f.testedPath.coords) > 0 { + cntrCoord := f.testedPath.coords[len(f.testedPath.coords)-1] + if topY < cntrCoord.y+(tHeight/2) { + topY = cntrCoord.y + (tHeight / 2) + } + if topY > tHeight { + botY = topY - tHeight + } + if topX < cntrCoord.x+(tWidth/2) { + topX = cntrCoord.x + (tWidth / 2) + } + if topX > tWidth { + botX = topX - tWidth + } + } + for y := botY; y < topY; y++ { + for x := botX; x < topX; x++ { if f.dispCoord != nil && f.dispCoord.Is(x, y) { g.Print("O") - } else if f.solvePath.ContainsCoordXY(x, y) { + } else if f.solvePath.ContainsCoord(x, y) { g.Print(".") - } else if f.testedPath.ContainsCoordXY(x, y) { + } else if f.testedPath.ContainsCoord(x, y) { r.Print(" ") } else { if f.end.Is(x, y) { - space.Print("X") + b.Print("X") } else if f.IsWall(x, y) { wall.Print(" ") } else {