Skip to content

Commit 2ca9a32

Browse files
committed
got rid of a while loop
1 parent 2498408 commit 2ca9a32

File tree

1 file changed

+7
-8
lines changed
  • src/main/java/com/sbaars/adventofcode/year22/days

1 file changed

+7
-8
lines changed

src/main/java/com/sbaars/adventofcode/year22/days/Day17.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,14 @@ public Object part2() {
3131
long numberOfRocks = 1000000000000L;
3232
State cycleStart = simulateShapeMoves(numberOfRocks, s -> s.cycleReset && s.fallenRocks != 0);
3333
State nextCycle = simulateShapeMoves(numberOfRocks, s -> s.cycleReset && s.fallenRocks > cycleStart.fallenRocks);
34-
long rocks = cycleStart.fallenRocks;
35-
long height = cycleStart.height;
36-
while(rocks<numberOfRocks){
37-
rocks += nextCycle.fallenRocks - cycleStart.fallenRocks;
38-
height += nextCycle.height - cycleStart.height;
39-
}
40-
long overshoot = rocks - numberOfRocks;
34+
long rocksPerCycle = nextCycle.fallenRocks - cycleStart.fallenRocks;
35+
long numberOfCycles = numberOfRocks / rocksPerCycle;
36+
long totalRocks = rocksPerCycle * numberOfCycles + cycleStart.fallenRocks;
37+
long heightPerCycle = nextCycle.height - cycleStart.height;
38+
long totalHeight = heightPerCycle * numberOfCycles + cycleStart.height;
39+
long overshoot = totalRocks - numberOfRocks;
4140
State atOvershoot = simulateShapeMoves(numberOfRocks, s -> s.fallenRocks == cycleStart.fallenRocks - overshoot);
42-
return height - (cycleStart.height - atOvershoot.height);
41+
return totalHeight - (cycleStart.height - atOvershoot.height);
4342
}
4443

4544
private State simulateShapeMoves(long iterations, Predicate<State> exitCondition) {

0 commit comments

Comments
 (0)