2017-12-19 13:51:24 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
termbox "github.com/nsf/termbox-go"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
N = iota
|
|
|
|
E
|
|
|
|
S
|
|
|
|
W
|
|
|
|
ERR
|
|
|
|
)
|
|
|
|
|
|
|
|
var network [][]byte
|
|
|
|
var message string
|
|
|
|
var ticks int
|
|
|
|
var nodes []byte
|
|
|
|
var width, height int
|
2017-12-19 14:03:06 +00:00
|
|
|
var printMap bool
|
2017-12-19 13:51:24 +00:00
|
|
|
|
|
|
|
func main() {
|
|
|
|
// Grab the window size
|
2017-12-19 14:03:06 +00:00
|
|
|
if len(os.Args) > 1 {
|
|
|
|
if os.Args[1] == "-print" {
|
2017-12-19 14:05:29 +00:00
|
|
|
fmt.Println("Not working right now... Sorry.")
|
|
|
|
os.Exit(1)
|
2017-12-19 14:03:06 +00:00
|
|
|
printMap = true
|
|
|
|
}
|
|
|
|
}
|
2017-12-19 13:51:24 +00:00
|
|
|
if err := termbox.Init(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
event := termbox.PollEvent()
|
|
|
|
if event.Key == termbox.KeyCtrlC {
|
|
|
|
termbox.Close()
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
width, height = termbox.Size()
|
|
|
|
|
|
|
|
network = StdinTo2DBytes()
|
2017-12-19 14:03:06 +00:00
|
|
|
if printMap {
|
|
|
|
ClearScreen()
|
|
|
|
PrintMap(0, 0)
|
|
|
|
PrintStatus()
|
|
|
|
}
|
2017-12-19 13:51:24 +00:00
|
|
|
|
|
|
|
posX, posY, dir := FindEntry(), 0, S
|
|
|
|
|
|
|
|
for dir != ERR {
|
|
|
|
posX, posY, dir = Move(posX, posY, dir)
|
|
|
|
|
2017-12-19 14:03:06 +00:00
|
|
|
ticks++
|
2017-12-19 13:51:24 +00:00
|
|
|
switch network[posY][posX] {
|
|
|
|
case '|', '-', '+':
|
|
|
|
case ' ':
|
2017-12-19 14:03:06 +00:00
|
|
|
dir = ERR
|
|
|
|
break
|
2017-12-19 13:51:24 +00:00
|
|
|
default:
|
|
|
|
if len(nodes) == 0 || nodes[len(nodes)-1] != network[posY][posX] {
|
|
|
|
message = fmt.Sprint("Found node ", string(network[posY][posX]), "!")
|
|
|
|
nodes = append(nodes, network[posY][posX])
|
|
|
|
}
|
|
|
|
}
|
2017-12-19 14:03:06 +00:00
|
|
|
ClearScreen()
|
|
|
|
if printMap {
|
|
|
|
PrintMap(posX, posY)
|
|
|
|
time.Sleep(time.Second / 30)
|
|
|
|
}
|
|
|
|
PrintStatus()
|
|
|
|
}
|
|
|
|
fmt.Println("Ctrl+C to Quit")
|
2017-12-19 13:51:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func GetNodesAsString(nodes []byte) string {
|
|
|
|
ret := "[ "
|
|
|
|
for i := range nodes {
|
|
|
|
ret += string(nodes[i]) + " "
|
|
|
|
}
|
|
|
|
return ret + "]"
|
|
|
|
}
|
|
|
|
|
|
|
|
func Move(posX, posY, dir int) (int, int, int) {
|
|
|
|
chkX, chkY := posX, posY
|
|
|
|
switch dir {
|
|
|
|
case N:
|
|
|
|
chkY--
|
|
|
|
case E:
|
|
|
|
chkX++
|
|
|
|
case S:
|
|
|
|
chkY++
|
|
|
|
case W:
|
|
|
|
chkX--
|
|
|
|
}
|
|
|
|
|
|
|
|
if chkX < 0 || chkY < 0 || chkX > len(network[0]) || chkY > len(network) {
|
|
|
|
// Went off the map
|
|
|
|
return posX, posY, ERR
|
|
|
|
}
|
|
|
|
|
|
|
|
if network[chkY][chkX] == '+' {
|
|
|
|
// Which direction do we need to turn?
|
|
|
|
switch dir {
|
|
|
|
case N, S:
|
|
|
|
if CanMoveTo(chkX+1, chkY) {
|
|
|
|
dir = E
|
|
|
|
} else if CanMoveTo(chkX-1, chkY) {
|
|
|
|
dir = W
|
|
|
|
}
|
|
|
|
case E, W:
|
|
|
|
fmt.Println(len(network), chkY+1)
|
|
|
|
if CanMoveTo(chkX, chkY+1) {
|
|
|
|
dir = S
|
|
|
|
} else if CanMoveTo(chkX, chkY-1) {
|
|
|
|
dir = N
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return chkX, chkY, dir
|
|
|
|
}
|
|
|
|
|
|
|
|
func CanMoveTo(chkX, chkY int) bool {
|
|
|
|
if chkY < 0 && chkX < 0 && chkY > len(network) && chkX > len(network[chkY]) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
fmt.Println(chkX, chkY)
|
|
|
|
return network[chkY][chkX] != ' '
|
|
|
|
}
|
|
|
|
|
|
|
|
func FindEntry() int {
|
|
|
|
for i := range network[0] {
|
|
|
|
if network[0][i] == '|' {
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
2017-12-19 14:03:06 +00:00
|
|
|
func ClearScreen() {
|
2017-12-19 13:51:24 +00:00
|
|
|
fmt.Print("\033[H\033[2J")
|
2017-12-19 14:03:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func PrintMap(posX, posY int) {
|
2017-12-19 13:51:24 +00:00
|
|
|
stX, stY := 0, 0
|
|
|
|
useHeight, useWidth := height-2, width
|
|
|
|
if posY > useHeight/2 {
|
|
|
|
stY = posY - (useHeight / 2)
|
|
|
|
}
|
|
|
|
if posX > width/2 {
|
|
|
|
stX = posX - (width / 2)
|
|
|
|
}
|
|
|
|
totHeight := useHeight/2 + posY
|
|
|
|
if totHeight < useHeight {
|
|
|
|
useHeight = useHeight + (useHeight - totHeight) + 4
|
|
|
|
}
|
|
|
|
totWidth := useWidth/2 + posX
|
|
|
|
if totWidth < useWidth {
|
|
|
|
useWidth = useWidth + (useWidth - totWidth) + 4
|
|
|
|
}
|
|
|
|
for y := stY; y < posY+(useHeight/2); y++ {
|
|
|
|
for x := stX; x < posX+(useWidth/2); x++ {
|
|
|
|
if len(network) > y {
|
|
|
|
if len(network[y]) > x {
|
|
|
|
if y == posY && x == posX {
|
|
|
|
fmt.Print("*")
|
|
|
|
} else {
|
|
|
|
fmt.Print(string(network[y][x]))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fmt.Println("")
|
|
|
|
}
|
2017-12-19 14:03:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func PrintStatus() {
|
2017-12-19 13:51:24 +00:00
|
|
|
fmt.Print(ticks, " Ticks ", GetNodesAsString(nodes), "\n")
|
|
|
|
fmt.Println(message)
|
|
|
|
}
|
|
|
|
|
|
|
|
func StdinTo2DBytes() [][]byte {
|
|
|
|
var ret [][]byte
|
|
|
|
scanner := bufio.NewScanner(os.Stdin)
|
|
|
|
for scanner.Scan() {
|
|
|
|
bts := scanner.Bytes()
|
|
|
|
t := make([]byte, len(bts))
|
|
|
|
copy(t, bts)
|
|
|
|
ret = append(ret, t)
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|