File tree Expand file tree Collapse file tree 1 file changed +7
-8
lines changed
src/main/java/com/sbaars/adventofcode/year22/days Expand file tree Collapse file tree 1 file changed +7
-8
lines changed Original file line number Diff line number Diff line change @@ -31,15 +31,14 @@ public Object part2() {
31
31
long numberOfRocks = 1000000000000L ;
32
32
State cycleStart = simulateShapeMoves (numberOfRocks , s -> s .cycleReset && s .fallenRocks != 0 );
33
33
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 ;
41
40
State atOvershoot = simulateShapeMoves (numberOfRocks , s -> s .fallenRocks == cycleStart .fallenRocks - overshoot );
42
- return height - (cycleStart .height - atOvershoot .height );
41
+ return totalHeight - (cycleStart .height - atOvershoot .height );
43
42
}
44
43
45
44
private State simulateShapeMoves (long iterations , Predicate <State > exitCondition ) {
You can’t perform that action at this time.
0 commit comments