Piotr Czajkowski 1 year ago
parent
commit
132aa70f57
1 changed files with 18 additions and 63 deletions
  1. 18 63
      09/code.go

+ 18 - 63
09/code.go

@@ -39,15 +39,15 @@ type point struct {
 	y int
 }
 
-func tailInVicinity(leader *point, tail point) bool {
-	if leader == nil {
-		return false
-	}
-
+func tailInVicinity(leader point, tail point) bool {
 	return tail.x >= leader.x-1 && tail.x <= leader.x+1 && tail.y >= leader.y-1 && tail.y <= leader.y+1
 }
 
-func catchUp(leader *point, tail point) point {
+func catchUp(leader point, tail point) point {
+	if tailInVicinity(leader, tail) {
+		return tail
+	}
+
 	if tail.x > leader.x {
 		tail.x--
 	} else if tail.x < leader.x {
@@ -63,67 +63,23 @@ func catchUp(leader *point, tail point) point {
 	return tail
 }
 
-func moveRight(leader *point, tail point) point {
-	if !tailInVicinity(leader, tail) {
-		if leader != nil {
-			tail = catchUp(leader, tail)
-		} else {
-			tail.x++
-		}
-	}
-
-	return tail
-}
-
-func moveLeft(leader *point, tail point) point {
-	if !tailInVicinity(leader, tail) {
-		if leader != nil {
-			tail = catchUp(leader, tail)
-		} else {
-			tail.x--
-		}
-	}
-
-	return tail
-}
-
-func moveUp(leader *point, tail point) point {
-	if !tailInVicinity(leader, tail) {
-		if leader != nil {
-			tail = catchUp(leader, tail)
-		} else {
-			tail.y++
-		}
-	}
-
-	return tail
-}
-
-func moveDown(leader *point, tail point) point {
-	if !tailInVicinity(leader, tail) {
-		if leader != nil {
-			tail = catchUp(leader, tail)
-		} else {
-			tail.y--
-		}
-	}
-
-	return tail
-}
-
-func drawTail(leader *point, tail point, action move) point {
+func moveLead(leader point, action move) point {
 	switch action.direction {
 	case 'R':
-		return moveRight(leader, tail)
+		leader.x++
+		return leader
 	case 'L':
-		return moveLeft(leader, tail)
+		leader.x--
+		return leader
 	case 'U':
-		return moveUp(leader, tail)
+		leader.y++
+		return leader
 	case 'D':
-		return moveDown(leader, tail)
+		leader.y--
+		return leader
 	}
 
-	return tail
+	return leader
 }
 
 func wayOfTail(moves []move, snake []point) int {
@@ -134,11 +90,10 @@ func wayOfTail(moves []move, snake []point) int {
 		for s := 0; s < moves[i].steps; s++ {
 			for j := range snake {
 				if j == 0 {
-					snake[j] = drawTail(nil, snake[j], moves[i])
+					snake[j] = moveLead(snake[j], moves[i])
 				} else {
-					snake[j] = drawTail(&snake[j-1], snake[j], moves[i])
+					snake[j] = catchUp(snake[j-1], snake[j])
 				}
-
 			}
 
 			trail[snake[last]] = true