protected override void SolvePartTwo() { BigInteger deckSize = BigInteger.Parse("119315717514047"); BigInteger shuffleNtimes = BigInteger.Parse("101741582076661"); BigInteger findCard = 2020; var shuffleSequence = new ShuffleSequence(shuffleMoves, deckSize); shuffleSequence.CompressSequence(); var repeatedShuffles = SequenceAfterNRepetitions(shuffleSequence, shuffleNtimes); resultPartTwo = repeatedShuffles.CardPositionBeforeShuffle(findCard).ToString(); }
public static ShuffleSequence SequenceAfterNRepetitions(ShuffleSequence shufSeq, BigInteger reps) { List <ShuffleSequence> shufflesPerBit = new List <ShuffleSequence> { new ShuffleSequence(shufSeq) }; int bitsNeeded = 0; BigInteger previousBitValue = 1; BigInteger bitValue = 2; //Create shufflesequences for each bit value up to and including the largest bit value smaller than "reps" while (bitValue < reps) { var next = new ShuffleSequence(shufflesPerBit[bitsNeeded]); next.AddToSequence(next); next.CompressSequence(); shufflesPerBit.Add(next); previousBitValue = bitValue; bitValue = previousBitValue * 2; bitsNeeded++; } //Combine shufflesequences for each activated bit in the bit-representation of "reps" var repeatedShuffleSequence = new ShuffleSequence(shufSeq.DeckSize); BigInteger remaining = reps; for (int i = shufflesPerBit.Count - 1; i >= 0; i--) { if (previousBitValue <= remaining) { repeatedShuffleSequence.AddToSequence(shufflesPerBit[i]); repeatedShuffleSequence.CompressSequence(); remaining = remaining - previousBitValue; } previousBitValue = previousBitValue / 2; } return(repeatedShuffleSequence); }