2017 Day 19 Complete!
This commit is contained in:
180
2017/day19/main.go
Normal file
180
2017/day19/main.go
Normal file
@@ -0,0 +1,180 @@
|
||||
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
|
||||
|
||||
func main() {
|
||||
// Grab the window size
|
||||
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()
|
||||
PrintMap(0, 0)
|
||||
|
||||
posX, posY, dir := FindEntry(), 0, S
|
||||
|
||||
fmt.Println(posX, posY, dir)
|
||||
for dir != ERR {
|
||||
posX, posY, dir = Move(posX, posY, dir)
|
||||
|
||||
switch network[posY][posX] {
|
||||
case '|', '-', '+':
|
||||
case ' ':
|
||||
fmt.Println("Invalid Move")
|
||||
os.Exit(1)
|
||||
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])
|
||||
}
|
||||
}
|
||||
ticks++
|
||||
PrintMap(posX, posY)
|
||||
time.Sleep(time.Second / 10)
|
||||
}
|
||||
fmt.Println("")
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
func PrintMap(posX, posY int) {
|
||||
fmt.Print("\033[H\033[2J")
|
||||
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("")
|
||||
}
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user