Piotr Czajkowski 1 год назад
Родитель
Сommit
2bb84c0777
1 измененных файлов с 43 добавлено и 13 удалено
  1. 43 13
      23/code.go

+ 43 - 13
23/code.go

@@ -7,9 +7,19 @@ import (
 	"os"
 )
 
+const (
+	None = iota
+	North
+	South
+	East
+	West
+)
+
 type Point struct {
-	y, x  int
-	steps int
+	y, x       int
+	steps      int
+	direction  int
+	mustGoDown bool
 }
 
 func readInput(file *os.File) [][]byte {
@@ -44,23 +54,42 @@ const (
 func (p *Point) getDestinations(board [][]byte, height int, width int) []Point {
 	var destinations []Point
 	p.steps++
-	slope := false
+
+	if p.mustGoDown && p.y+1 < height && board[p.y+1][p.x] != Rock {
+		p.y++
+		p.direction = None
+		p.mustGoDown = false
+		destinations = append(destinations, *p)
+		return destinations
+	}
+
 	switch board[p.y][p.x] {
 	case Up:
-		p.y--
-		slope = true
+		p.direction = North
+		p.mustGoDown = true
 	case Down:
-		p.y++
-		slope = true
+		p.direction = South
+		p.mustGoDown = false
 	case Left:
-		p.x--
-		slope = true
+		p.direction = West
+		p.mustGoDown = true
 	case Right:
-		p.x++
-		slope = true
+		p.direction = East
+		p.mustGoDown = true
 	}
 
-	if slope {
+	if p.direction != None {
+		switch p.direction {
+		case North:
+			p.y--
+		case South:
+			p.y++
+		case West:
+			p.x--
+		case East:
+			p.x++
+		}
+
 		destinations = append(destinations, *p)
 		return destinations
 	}
@@ -109,6 +138,7 @@ func calculate(board [][]byte) int {
 		frontier = frontier[1:]
 
 		if current.x == goal.x && current.y == goal.y {
+			fmt.Println(current.steps)
 			if max < current.steps {
 				max = current.steps
 			}
@@ -119,7 +149,7 @@ func calculate(board [][]byte) int {
 		successors := current.getDestinations(board, height, width)
 		for i := range successors {
 			value, ok := visited[successors[i].key()]
-			if !ok || visited[successors[i].key()] > value {
+			if !ok || visited[successors[i].key()] != value {
 				visited[successors[i].key()] = successors[i].steps
 				frontier = append(frontier, successors[i])
 			}