Working d10p1, bug if I'm on the edges
This commit is contained in:
parent
5ca089afb4
commit
e0e4bbc958
@ -16,6 +16,13 @@ type Maze struct {
|
|||||||
steps int
|
steps int
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var fromWhere = map[rune]int8{
|
||||||
|
'N': 0,
|
||||||
|
'S': 1,
|
||||||
|
'W': 2,
|
||||||
|
'E': 3,
|
||||||
|
}
|
||||||
|
|
||||||
var mapDirections = map[rune]int8{
|
var mapDirections = map[rune]int8{
|
||||||
'|': 0, // Vertical
|
'|': 0, // Vertical
|
||||||
'-': 1, // Horizontal
|
'-': 1, // Horizontal
|
||||||
@ -47,30 +54,97 @@ func timer(name string) func() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (mz *Maze) ChooseFirstDirection(startPoint [2]int) [2]int {
|
func (mz *Maze) ChooseFirstDirection(startPoint [2]int) ([2]int, rune) {
|
||||||
// For the first step, we don't care about directions, we only need a tile
|
// For the first step, we don't care about directions, we only need a tile
|
||||||
// were we can step on. E.g., if we have a J North of S we CAN'T go. A 7 would
|
// were we can step on. E.g., if we have a J North of S we CAN'T go. A 7 would
|
||||||
// be OK.
|
// be OK.
|
||||||
x := startPoint[0]
|
y := startPoint[0]
|
||||||
y := startPoint[1]
|
x := startPoint[1]
|
||||||
// If x - 1 (going west), only 1, 2, 5 allowed
|
// If x - 1 (going west), only 1, 2, 5 allowed
|
||||||
temp := mapDirections[mz.direction[x-1][y]]
|
temp := mapDirections[mz.direction[y][x-1]]
|
||||||
if temp == 1 || temp == 2 || temp == 5 {
|
if temp == 1 || temp == 2 || temp == 5 {
|
||||||
PrintAndWait("We go West boi")
|
// We came from East, we also return 'E'
|
||||||
|
newSpot := [2]int{y,x-1}
|
||||||
|
return newSpot, 'E'
|
||||||
// If x + 1 (going east), only 1, 3, 4 allowed
|
// If x + 1 (going east), only 1, 3, 4 allowed
|
||||||
} else if temp := mapDirections[mz.direction[x+1][y]]; temp == 1 || temp == 3 || temp == 4 {
|
} else if temp := mapDirections[mz.direction[y][x+1]]; temp == 1 || temp == 3 || temp == 4 {
|
||||||
PrintAndWait("We go East boi")
|
// We came from West
|
||||||
|
newSpot := [2]int{y,x+1}
|
||||||
|
return newSpot, 'W'
|
||||||
// If y - 1 (going north), only 0, 4, 5 allowed
|
// If y - 1 (going north), only 0, 4, 5 allowed
|
||||||
} else if temp := mapDirections[mz.direction[x][y-1]]; temp == 0 || temp == 4 || temp == 5 {
|
} else if temp := mapDirections[mz.direction[y-1][x]]; temp == 0 || temp == 4 || temp == 5 {
|
||||||
PrintAndWait("We go North boi")
|
// We came from South
|
||||||
|
newSpot := [2]int{y-1,x}
|
||||||
|
return newSpot, 'S'
|
||||||
} else {
|
} else {
|
||||||
log.Fatal("How is this even possible..?")
|
log.Fatal("How is this even possible..?")
|
||||||
}
|
}
|
||||||
|
s := [2]int{0, 0}
|
||||||
|
return s, 'X'
|
||||||
|
}
|
||||||
|
|
||||||
|
func (mz *Maze) WeGo(loc [2]int, direction rune) ([2]int, rune) {
|
||||||
|
mapDir := fromWhere[direction]
|
||||||
|
y, x := loc[0], loc[1]
|
||||||
|
thisPipe := mapDirections[mz.direction[y][x]]
|
||||||
|
goNorth := [2]int{y-1, x}
|
||||||
|
goSouth := [2]int{y+1, x}
|
||||||
|
goWest := [2]int{y, x-1}
|
||||||
|
goEast := [2]int{y, x+1}
|
||||||
|
switch mapDir {
|
||||||
|
case 0:
|
||||||
|
switch thisPipe {
|
||||||
|
case 0:
|
||||||
|
return goSouth, 'N'
|
||||||
|
case 2:
|
||||||
|
return goEast, 'W'
|
||||||
|
case 3:
|
||||||
|
return goWest, 'E'
|
||||||
|
}
|
||||||
|
case 1:
|
||||||
|
switch thisPipe {
|
||||||
|
case 0:
|
||||||
|
return goNorth, 'S'
|
||||||
|
case 4:
|
||||||
|
return goWest, 'E'
|
||||||
|
case 5:
|
||||||
|
return goEast, 'W'
|
||||||
|
}
|
||||||
|
case 2:
|
||||||
|
switch thisPipe {
|
||||||
|
case 1:
|
||||||
|
return goEast, 'W'
|
||||||
|
case 3:
|
||||||
|
return goNorth, 'S'
|
||||||
|
case 4:
|
||||||
|
return goSouth, 'N'
|
||||||
|
}
|
||||||
|
case 3:
|
||||||
|
switch thisPipe {
|
||||||
|
case 1:
|
||||||
|
return goWest, 'E'
|
||||||
|
case 2:
|
||||||
|
return goNorth, 'S'
|
||||||
|
case 5:
|
||||||
|
return goSouth, 'N'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
s := [2]int{0, 0}
|
||||||
|
return s, 'X'
|
||||||
}
|
}
|
||||||
|
|
||||||
func (mz *Maze) FindPath(startPoint [2]int) {
|
func (mz *Maze) FindPath(startPoint [2]int) {
|
||||||
nextCheck := startPoint
|
defer timer("FindPath")()
|
||||||
nextCheck = mz.ChooseFirstDirection(nextCheck)
|
y, x := startPoint[0], startPoint[1]
|
||||||
|
nextCheck, direction := mz.ChooseFirstDirection(startPoint)
|
||||||
|
mz.steps++
|
||||||
|
curY, curX := nextCheck[0], nextCheck[1]
|
||||||
|
for {
|
||||||
|
nextCheck, direction = mz.WeGo(nextCheck, direction)
|
||||||
|
curY, curX = nextCheck[0], nextCheck[1]
|
||||||
|
mz.steps++
|
||||||
|
if y == curY && x == curX { break }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@ -84,7 +158,7 @@ func main() {
|
|||||||
|
|
||||||
lines := make([]string, 0)
|
lines := make([]string, 0)
|
||||||
|
|
||||||
mz := Maze{}
|
mz := Maze{ steps: 0, }
|
||||||
|
|
||||||
scanner := bufio.NewScanner(file)
|
scanner := bufio.NewScanner(file)
|
||||||
for scanner.Scan() {
|
for scanner.Scan() {
|
||||||
@ -113,4 +187,5 @@ func main() {
|
|||||||
// Going west: x - 1 | Going east: x + 1
|
// Going west: x - 1 | Going east: x + 1
|
||||||
// Going north: y - 1 | Going south: y + 1
|
// Going north: y - 1 | Going south: y + 1
|
||||||
mz.FindPath(startPoint)
|
mz.FindPath(startPoint)
|
||||||
|
fmt.Printf("Number of steps: %d", mz.steps / 2)
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user