Fairly straightforward, but completely useless for part 2.
importarrow.core.getOrElseimportarrow.core.toOptionimportjava.nio.file.Filesimportjava.nio.file.PathssealedclassShuffleMethods:(List<Int>)->List<Int>{objectDealIntoStack:ShuffleMethods(){overridefuninvoke(p1:List<Int>)=p1.reversed()}classCut(valn:Int):ShuffleMethods(){overridefuninvoke(p1:List<Int>):List<Int>{returnif(n>=0){p1.drop(n)+p1.take(n)}else{p1.takeLast(-1*n)+p1.dropLast(-1*n)}}}classDealIncrement(valn:Int):ShuffleMethods(){overridefuninvoke(p1:List<Int>):List<Int>{returnp1.withIndex().sortedBy{(it.index*n)%p1.size}.map{it.value}}}}valregexIncrement="""deal with increment (\d+)""".toRegex()constvaldealIntoStack="deal into new stack"valregexCut="""cut (-?\d+)""".toRegex()funString.toShuffleMethod():ShuffleMethods{if(this==dealIntoStack){returnShuffleMethods.DealIntoStack}returnwhen(valincrementMatch=regexIncrement.matchEntire(this)){null->{valcutMatch=regexCut.matchEntire(this).toOption()ShuffleMethods.Cut(cutMatch.getOrElse{throwerror("unknown style: $this")}.groupValues[1].toInt())}else->ShuffleMethods.DealIncrement(incrementMatch.groupValues[1].toInt())}}objectDay22{privateconstvalFILENAME="src/main/resources/day22.txt"valfileDataget()=Files.readAllLines(Paths.get(FILENAME))funpart1(input:List<String>,deck:List<Int>=(0until10007).toList()):List<Int>{valmethods=input.map{it.toShuffleMethod()}returnshuffle(methods,deck)}privatefunshuffle(methods:List<ShuffleMethods>,deck:List<Int>):List<Int>{returnmethods.fold(deck){acc,curr->curr(acc)}}}funmain(){println(Day22.part1(Day22.fileData).withIndex().first{(i,v)->v==2019})}
Ok, so... There's a thing called an "inverse mod" that only works on relatively prime numbers.
Additionally, all of the three shuffling techniques can be expressed as an index offset and the number of mod actions performed.
A cut adds an offset and a single mod (think of applying the size of the cut to the indexes and then just pulling the new list from x = 0... offset of n, mod of 1)
A deal into n stacks shuffle just multiplies the index by n and then you scoop up the values in order. This is a mod shift of n. Trust me.
A deal into new stack goes backwards one, and flips the mod direction of our stack. (-1,-1)
so, any given index can be extracted.
Sample 1: (7,0)->(-1,-1)->(-1,-1) becomes (-7,-7)->(-1,-1) which becomes (7,0) => Every time through, the value of a given starting index changes by i*7%cards.
Sample 2: (1,-6),(7,0),(-1,-1) -> (7,-6),(-1,-1) -> (-7,-3) -> (-7i-3) % cards
Now, the problem here is going backwards, unrolling this. That's where inverse mod comes in, and makes this problem not actually work with non-relatively prime numbers. After n shuffles of c cards, that multiplier will be (-7^n % c) ... unfortunately, the addition part is (mod_a * offset_b + offset_a) % c, which is hard to write here as it's kind of a series of functions.
Anyway, I'm not 100% sure I really understand it, but maybe someone mathier can correct what I'm saying.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Kotlin solution for part 1.
Fairly straightforward, but completely useless for part 2.
Ok, this is the first one I thought was unfair.
The answer to part 2 requires you to know (or to be able to find) a lot of set theory.
Spoilers after my kotlin code:
Ok, so... There's a thing called an "inverse mod" that only works on relatively prime numbers.
Additionally, all of the three shuffling techniques can be expressed as an index offset and the number of mod actions performed.
A cut adds an offset and a single mod (think of applying the size of the cut to the indexes and then just pulling the new list from x = 0... offset of n, mod of 1)
A deal into n stacks shuffle just multiplies the index by n and then you scoop up the values in order. This is a mod shift of n. Trust me.
A deal into new stack goes backwards one, and flips the mod direction of our stack. (-1,-1)
so, any given index can be extracted.
Sample 1:
(7,0)->(-1,-1)->(-1,-1)
becomes(-7,-7)->(-1,-1)
which becomes(7,0)
=> Every time through, the value of a given starting index changes byi*7%cards
.Sample 2:
(1,-6),(7,0),(-1,-1)
->(7,-6),(-1,-1)
->(-7,-3)
->(-7i-3) % cards
Now, the problem here is going backwards, unrolling this. That's where inverse mod comes in, and makes this problem not actually work with non-relatively prime numbers. After n shuffles of c cards, that multiplier will be
(-7^n % c)
... unfortunately, the addition part is(mod_a * offset_b + offset_a) % c
, which is hard to write here as it's kind of a series of functions.Anyway, I'm not 100% sure I really understand it, but maybe someone mathier can correct what I'm saying.