2023-12-17 15:31:11 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math"
|
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
SPACE = '.'
|
|
|
|
V_MIRROR = '|'
|
|
|
|
H_MIRROR = '-'
|
|
|
|
F_MIRROR = '/'
|
|
|
|
B_MIRROR = '\\'
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
N = iota
|
|
|
|
S
|
|
|
|
W
|
|
|
|
E
|
|
|
|
)
|
|
|
|
|
|
|
|
// only for debugging
|
|
|
|
var steps int
|
|
|
|
var dirs = []string{"N", "S", "W", "E"}
|
|
|
|
|
2023-12-22 22:03:12 +00:00
|
|
|
func runner(mover *mover, lines []string) {
|
|
|
|
for steps < math.MaxInt && mover.Y >= 0 && mover.Y < len(lines) && mover.X >= 0 && mover.X < len(lines[mover.Y]) {
|
|
|
|
if nodeInNodes(mover.node, mover.nodes) {
|
|
|
|
log.Debug(mover.node, " in nodes, breaking.")
|
2023-12-17 15:31:11 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2023-12-22 22:03:12 +00:00
|
|
|
mover.nodes = append(mover.nodes, mover.node)
|
2023-12-17 15:31:11 +00:00
|
|
|
|
2023-12-22 22:03:12 +00:00
|
|
|
switch lines[mover.Y][mover.X] {
|
2023-12-17 15:31:11 +00:00
|
|
|
case SPACE: // '.'
|
2023-12-22 22:03:12 +00:00
|
|
|
log.Debug("we have space and direction is ", dirs[mover.direction()])
|
|
|
|
mover.move()
|
2023-12-17 15:31:11 +00:00
|
|
|
|
|
|
|
case F_MIRROR: // '/'
|
2023-12-22 22:03:12 +00:00
|
|
|
log.Debug("we have forward mirror and direction is ", dirs[mover.direction()])
|
|
|
|
switch mover.direction() {
|
2023-12-17 15:31:11 +00:00
|
|
|
case N:
|
2023-12-22 22:03:12 +00:00
|
|
|
mover.setDirection(E)
|
2023-12-17 15:31:11 +00:00
|
|
|
case S:
|
2023-12-22 22:03:12 +00:00
|
|
|
mover.setDirection(W)
|
2023-12-17 15:31:11 +00:00
|
|
|
case W:
|
2023-12-22 22:03:12 +00:00
|
|
|
mover.setDirection(S)
|
2023-12-17 15:31:11 +00:00
|
|
|
case E:
|
2023-12-22 22:03:12 +00:00
|
|
|
mover.setDirection(N)
|
2023-12-17 15:31:11 +00:00
|
|
|
}
|
2023-12-22 22:03:12 +00:00
|
|
|
log.Debug("step: ", steps, " ", string(F_MIRROR), " direction changed to ", dirs[mover.direction()])
|
|
|
|
mover.move()
|
2023-12-17 15:31:11 +00:00
|
|
|
|
|
|
|
case B_MIRROR: // '\'
|
2023-12-22 22:03:12 +00:00
|
|
|
log.Debug("we have backwards mirror and direction is ", dirs[mover.direction()])
|
|
|
|
switch mover.direction() {
|
2023-12-17 15:31:11 +00:00
|
|
|
case N:
|
2023-12-22 22:03:12 +00:00
|
|
|
mover.setDirection(W)
|
2023-12-17 15:31:11 +00:00
|
|
|
case S:
|
2023-12-22 22:03:12 +00:00
|
|
|
mover.setDirection(E)
|
2023-12-17 15:31:11 +00:00
|
|
|
case W:
|
2023-12-22 22:03:12 +00:00
|
|
|
mover.setDirection(N)
|
2023-12-17 15:31:11 +00:00
|
|
|
case E:
|
2023-12-22 22:03:12 +00:00
|
|
|
mover.setDirection(S)
|
2023-12-17 15:31:11 +00:00
|
|
|
}
|
2023-12-22 22:03:12 +00:00
|
|
|
log.Debug("step: ", steps, " ", string(B_MIRROR), " direction changed to ", dirs[mover.direction()])
|
|
|
|
mover.move()
|
2023-12-17 15:31:11 +00:00
|
|
|
|
|
|
|
case V_MIRROR: // '|'
|
2023-12-22 22:03:12 +00:00
|
|
|
log.Debug("we have vertical mirror and direction is ", dirs[mover.direction()])
|
|
|
|
if mover.direction() == N || mover.direction() == S {
|
|
|
|
mover.move()
|
2023-12-17 15:31:11 +00:00
|
|
|
continue
|
|
|
|
}
|
2023-12-22 22:03:12 +00:00
|
|
|
if mover.direction() == W || mover.direction() == E {
|
|
|
|
c := mover.coords
|
|
|
|
mover.setDirection(N)
|
|
|
|
mover.move()
|
|
|
|
runner(mover, lines)
|
|
|
|
mover.coords = c
|
|
|
|
mover.setDirection(S)
|
|
|
|
mover.move()
|
|
|
|
runner(mover, lines)
|
2023-12-17 15:31:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case H_MIRROR: // '-'
|
2023-12-22 22:03:12 +00:00
|
|
|
log.Debug("we have horizontal mirror and direction is ", dirs[mover.direction()])
|
|
|
|
if mover.direction() == W || mover.direction() == E {
|
|
|
|
mover.move()
|
2023-12-17 15:31:11 +00:00
|
|
|
continue
|
|
|
|
}
|
2023-12-22 22:03:12 +00:00
|
|
|
if mover.direction() == N || mover.direction() == S {
|
|
|
|
c := mover.coords
|
|
|
|
mover.setDirection(W)
|
|
|
|
mover.move()
|
|
|
|
runner(mover, lines)
|
|
|
|
mover.coords = c
|
|
|
|
mover.setDirection(E)
|
|
|
|
mover.move()
|
|
|
|
runner(mover, lines)
|
2023-12-17 15:31:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
log.Fatal("unknown node")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
steps++
|
|
|
|
}
|
|
|
|
|
|
|
|
// one
|
|
|
|
func one(lines []string) int {
|
2023-12-22 22:03:12 +00:00
|
|
|
mover := newMover(newNode(0, 0, E))
|
2023-12-17 15:31:11 +00:00
|
|
|
|
2023-12-22 22:03:12 +00:00
|
|
|
runner(mover, lines)
|
2023-12-17 15:31:11 +00:00
|
|
|
|
|
|
|
if log.GetLevel() == log.DebugLevel {
|
2023-12-22 22:03:12 +00:00
|
|
|
n := printDebug(mover, lines)
|
2023-12-17 15:31:11 +00:00
|
|
|
log.Debug("total: ", n)
|
|
|
|
}
|
|
|
|
|
2023-12-22 22:03:12 +00:00
|
|
|
return uniqueNodes(mover)
|
2023-12-17 15:31:11 +00:00
|
|
|
}
|