Piotr Czajkowski 1 hete
szülő
commit
c88bed9988
1 módosított fájl, 14 hozzáadás és 9 törlés
  1. 14 9
      13/code.go

+ 14 - 9
13/code.go

@@ -76,17 +76,22 @@ func calculate(machine Machine, button int) [2]int {
 		start = 100
 	}
 
-	for start > 0 {
-		if (machine.x-start*machine.buttons[button].x)%machine.buttons[otherButton].x == 0 && (machine.y-start*machine.buttons[button].y)%machine.buttons[otherButton].y == 0 {
-			otherPushes := (machine.x - start*machine.buttons[button].x) / machine.buttons[otherButton].x
-			if otherPushes <= 100 {
-				results[button] = start
-				results[otherButton] = otherPushes
-				return results
+	for ; start > 0; start-- {
+		deltaX := machine.x - start*machine.buttons[button].x
+		if deltaX%machine.buttons[otherButton].x == 0 {
+			otherPushes := deltaX / machine.buttons[otherButton].x
+			if otherPushes > 100 {
+				continue
+			}
+
+			if machine.y-start*machine.buttons[button].y != otherPushes*machine.buttons[otherButton].y {
+				continue
 			}
-		}
 
-		start--
+			results[button] = start
+			results[otherButton] = otherPushes
+			return results
+		}
 	}
 
 	return results