Piotr Czajkowski 1 周之前
父节点
当前提交
73db2ee5c0
共有 1 个文件被更改,包括 12 次插入2 次删除
  1. 12 2
      14/code.go

+ 12 - 2
14/code.go

@@ -50,12 +50,22 @@ func getMaxXY(robots []Robot) (int, int) {
 }
 
 func robotsAfter(robots []Robot, maxX, maxY, after int) []Robot {
-	for _, robot := range robots {
+	robotsMoved := make([]Robot, len(robots))
+	for i, robot := range robots {
 		robot.x = (robot.vX*after + robot.x) % maxX
+		if robot.x < 0 {
+			robot.x = maxX + robot.x
+		}
+
 		robot.y = (robot.vY*after + robot.y) % maxY
+		if robot.y < 0 {
+			robot.y = maxY + robot.y
+		}
+
+		robotsMoved[i] = robot
 	}
 
-	return robots
+	return robotsMoved
 }
 
 func part1(robots []Robot, maxX, maxY, after int) int {