Piotr Czajkowski 1 week ago
parent
commit
73db2ee5c0
1 changed files with 12 additions and 2 deletions
  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 {