Ver Fonte

Solved for sample

Piotr Czajkowski há 1 ano atrás
pai
commit
994f2ff0c4
1 ficheiros alterados com 102 adições e 3 exclusões
  1. 102 3
      code.go

+ 102 - 3
code.go

@@ -7,13 +7,112 @@ import (
 	"os"
 )
 
+const (
+	Up = iota
+	Down
+	Left
+	Right
+)
+
 type Point struct {
-	y, x int
+	y, x      int
+	direction int
 }
 
-func (p *Point) move(maze []string) bool {
+func (p *Point) move(maze []string, height int, width int) bool {
 	switch maze[p.y][p.x] {
+	case '|':
+		if p.direction == Up && p.y-1 >= 0 {
+			p.y--
+			return true
+		} else if p.direction == Down && p.y+1 < height {
+			p.y++
+			return true
+		}
+	case '-':
+		if p.direction == Right && p.x+1 < width {
+			p.x++
+			return true
+		} else if p.direction == Left && p.x-1 >= 0 {
+			p.x--
+			return true
+		}
+	case 'L':
+		if p.direction == Down && p.x+1 < width {
+			p.x++
+			p.direction = Right
+			return true
+		} else if p.direction == Left && p.y-1 >= 0 {
+			p.y--
+			p.direction = Up
+			return true
+		}
+	case 'J':
+		if p.direction == Down && p.x-1 >= 0 {
+			p.x--
+			p.direction = Left
+			return true
+		} else if p.direction == Right && p.y-1 >= 0 {
+			p.y--
+			p.direction = Up
+			return true
+		}
+	case '7':
+		if p.direction == Right && p.y+1 < height {
+			p.y++
+			p.direction = Down
+			return true
+		} else if p.direction == Up && p.x-1 >= 0 {
+			p.x--
+			p.direction = Left
+			return true
+		}
+	case 'F':
+		if p.direction == Up && p.x+1 < width {
+			p.x++
+			p.direction = Right
+			return true
+		} else if p.direction == Left && p.y+1 < height {
+			p.y++
+			p.direction = Down
+			return true
+		}
+
 	}
+
+	return false
+}
+
+func part1(maze []string) int {
+	biggest := 0
+	height := len(maze)
+	width := len(maze[0])
+	for y := range maze {
+		for x := range maze[y] {
+			if maze[y][x] == 'F' {
+				current := Point{y: y, x: x}
+				start := Point{y: y, x: x}
+				steps := 0
+				for {
+					if !current.move(maze, height, width) {
+						break
+					}
+					steps++
+
+					if current.x == start.x && current.y == start.y {
+						steps /= 2
+						if steps > biggest {
+							biggest = steps
+						}
+
+						break
+					}
+				}
+			}
+		}
+	}
+
+	return biggest
 }
 
 func readInput(file *os.File) []string {
@@ -45,5 +144,5 @@ func main() {
 	}
 
 	maze := readInput(file)
-	fmt.Println(maze)
+	fmt.Println("Part1:", part1(maze))
 }