Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

Return to Answer

Commonmark migration
Source Link

#JavaScript (ES6), 44 bytes

JavaScript (ES6), 44 bytes

Shorter version suggested by @nwellnhof

Expects a deck with 1-indexed cards as input.

f=(a,x=1)=>a[x]-2&&1+f(a,x*2%(a.length-1|1))

Try it online!

Given a deck \$[c_0,\ldots,c_{L-1}]\$ of length \$L\$, we define:

$$x_n=\begin{cases} 2^n\bmod L&\text{if }L\text{ is odd}\\ 2^n\bmod (L-1)&\text{if }L\text{ is even}\\ \end{cases}$$

And we look for \$n\$ such that \$c_{x_n}=2\$.


#JavaScript (ES6), (削除) 57 52 (削除ここまで) 50 bytes

JavaScript (ES6), (削除) 57 52 (削除ここまで) 50 bytes

Expects a deck with 0-indexed cards as input.

f=(a,x=1,k=a.length-1|1)=>a[1]-x%k&&1+f(a,x*-~k/2)

Try it online!

###How?

How?

Since JS is lacking native support for extracting array slices with a custom stepping, simulating the entire riffle-shuffle would probably be rather costly (but to be honest, I didn't even try). However, the solution can also be found by just looking at the 2nd card and the total number of cards in the deck.

Given a deck of length \$L\$, this code looks for \$n\$ such that:

$$c_2\equiv\left(\frac{k+1}{2}\right)^n\pmod k$$

where \$c_2\$ is the second card and \$k\$ is defined as:

$$k=\begin{cases} L&\text{if }L\text{ is odd}\\ L-1&\text{if }L\text{ is even}\\ \end{cases}$$

#JavaScript (ES6), 44 bytes

Shorter version suggested by @nwellnhof

Expects a deck with 1-indexed cards as input.

f=(a,x=1)=>a[x]-2&&1+f(a,x*2%(a.length-1|1))

Try it online!

Given a deck \$[c_0,\ldots,c_{L-1}]\$ of length \$L\$, we define:

$$x_n=\begin{cases} 2^n\bmod L&\text{if }L\text{ is odd}\\ 2^n\bmod (L-1)&\text{if }L\text{ is even}\\ \end{cases}$$

And we look for \$n\$ such that \$c_{x_n}=2\$.


#JavaScript (ES6), (削除) 57 52 (削除ここまで) 50 bytes

Expects a deck with 0-indexed cards as input.

f=(a,x=1,k=a.length-1|1)=>a[1]-x%k&&1+f(a,x*-~k/2)

Try it online!

###How?

Since JS is lacking native support for extracting array slices with a custom stepping, simulating the entire riffle-shuffle would probably be rather costly (but to be honest, I didn't even try). However, the solution can also be found by just looking at the 2nd card and the total number of cards in the deck.

Given a deck of length \$L\$, this code looks for \$n\$ such that:

$$c_2\equiv\left(\frac{k+1}{2}\right)^n\pmod k$$

where \$c_2\$ is the second card and \$k\$ is defined as:

$$k=\begin{cases} L&\text{if }L\text{ is odd}\\ L-1&\text{if }L\text{ is even}\\ \end{cases}$$

JavaScript (ES6), 44 bytes

Shorter version suggested by @nwellnhof

Expects a deck with 1-indexed cards as input.

f=(a,x=1)=>a[x]-2&&1+f(a,x*2%(a.length-1|1))

Try it online!

Given a deck \$[c_0,\ldots,c_{L-1}]\$ of length \$L\$, we define:

$$x_n=\begin{cases} 2^n\bmod L&\text{if }L\text{ is odd}\\ 2^n\bmod (L-1)&\text{if }L\text{ is even}\\ \end{cases}$$

And we look for \$n\$ such that \$c_{x_n}=2\$.


JavaScript (ES6), (削除) 57 52 (削除ここまで) 50 bytes

Expects a deck with 0-indexed cards as input.

f=(a,x=1,k=a.length-1|1)=>a[1]-x%k&&1+f(a,x*-~k/2)

Try it online!

How?

Since JS is lacking native support for extracting array slices with a custom stepping, simulating the entire riffle-shuffle would probably be rather costly (but to be honest, I didn't even try). However, the solution can also be found by just looking at the 2nd card and the total number of cards in the deck.

Given a deck of length \$L\$, this code looks for \$n\$ such that:

$$c_2\equiv\left(\frac{k+1}{2}\right)^n\pmod k$$

where \$c_2\$ is the second card and \$k\$ is defined as:

$$k=\begin{cases} L&\text{if }L\text{ is odd}\\ L-1&\text{if }L\text{ is even}\\ \end{cases}$$

added a description for the shorter version
Source Link
Arnauld
  • 205.5k
  • 21
  • 187
  • 670

#JavaScript (ES6), 44 bytes

Shorter version suggested by @nwellnhof

Expects a deck with 1-indexed deckcards as input.

f=(a,x=1)=>a[x]-2&&1+f(a,x*2%(a.length-1|1))

Try it online!

Given a deck \$[c_0,\ldots,c_{L-1}]\$ of length \$L\$, we define:

$$x_n=\begin{cases} 2^n\bmod L&\text{if }L\text{ is odd}\\ 2^n\bmod (L-1)&\text{if }L\text{ is even}\\ \end{cases}$$

And we look for \$n\$ such that \$c_{x_n}=2\$.


#JavaScript (ES6), (削除) 57 52 (削除ここまで) 50 bytes

Expects a deck with 0-indexed deckcards as input.

f=(a,x=1,k=a.length-1|1)=>a[1]-x%k&&1+f(a,x*-~k/2)

Try it online!

###How?

Since JS is lacking native support for extracting array slices with a custom stepping, simulating the entire riffle-shuffle would probably be rather costly (but to be honest, I didn't even try). However, the solution can also be found by just looking at the 2nd card and the total number of cards in the deck.

Given a deck of length \$L\$, this code looks for \$n\$ such that:

$$c_2\equiv\left(\frac{k+1}{2}\right)^n\pmod k$$

where \$c_2\$ is the second card and \$k\$ is defined as:

$$k=\begin{cases} L&\text{if }L\text{ is odd}\\ L-1&\text{if }L\text{ is even}\\ \end{cases}$$

#JavaScript (ES6), 44 bytes

Shorter version suggested by @nwellnhof

Expects a 1-indexed deck as input.

f=(a,x=1)=>a[x]-2&&1+f(a,x*2%(a.length-1|1))

Try it online!


#JavaScript (ES6), (削除) 57 52 (削除ここまで) 50 bytes

Expects a 0-indexed deck as input.

f=(a,x=1,k=a.length-1|1)=>a[1]-x%k&&1+f(a,x*-~k/2)

Try it online!

###How?

Since JS is lacking native support for extracting array slices with a custom stepping, simulating the entire riffle-shuffle would probably be rather costly (but to be honest, I didn't even try). However, the solution can also be found by just looking at the 2nd card and the total number of cards in the deck.

Given a deck of length \$L\$, this code looks for \$n\$ such that:

$$c_2\equiv\left(\frac{k+1}{2}\right)^n\pmod k$$

where \$c_2\$ is the second card and \$k\$ is defined as:

$$k=\begin{cases} L&\text{if }L\text{ is odd}\\ L-1&\text{if }L\text{ is even}\\ \end{cases}$$

#JavaScript (ES6), 44 bytes

Shorter version suggested by @nwellnhof

Expects a deck with 1-indexed cards as input.

f=(a,x=1)=>a[x]-2&&1+f(a,x*2%(a.length-1|1))

Try it online!

Given a deck \$[c_0,\ldots,c_{L-1}]\$ of length \$L\$, we define:

$$x_n=\begin{cases} 2^n\bmod L&\text{if }L\text{ is odd}\\ 2^n\bmod (L-1)&\text{if }L\text{ is even}\\ \end{cases}$$

And we look for \$n\$ such that \$c_{x_n}=2\$.


#JavaScript (ES6), (削除) 57 52 (削除ここまで) 50 bytes

Expects a deck with 0-indexed cards as input.

f=(a,x=1,k=a.length-1|1)=>a[1]-x%k&&1+f(a,x*-~k/2)

Try it online!

###How?

Since JS is lacking native support for extracting array slices with a custom stepping, simulating the entire riffle-shuffle would probably be rather costly (but to be honest, I didn't even try). However, the solution can also be found by just looking at the 2nd card and the total number of cards in the deck.

Given a deck of length \$L\$, this code looks for \$n\$ such that:

$$c_2\equiv\left(\frac{k+1}{2}\right)^n\pmod k$$

where \$c_2\$ is the second card and \$k\$ is defined as:

$$k=\begin{cases} L&\text{if }L\text{ is odd}\\ L-1&\text{if }L\text{ is even}\\ \end{cases}$$

minor update
Source Link
Arnauld
  • 205.5k
  • 21
  • 187
  • 670

#JavaScript (ES6), 44 bytes

Shorter version thanks tosuggested by @nwellnhof

Expects a 1-indexed deck as input.

f=(a,x=1)=>a[x]-1&&1+f2&&1+f(a,x*2%(a.length-1|1))

Try it online! Try it online!


#JavaScript (ES6), (削除) 57 52 (削除ここまで) 50 bytes

Expects a 0-indexed deck as input.

f=(a,x=1,k=a.length-1|1)=>a[1]-x%k&&1+f(a,x*-~k/2)

Try it online!

###How?

Since JS is lacking native support for extracting array slices with a custom stepping, simulating the entire riffle-shuffle would probably be rather costly (but to be honest, I didn't even try). However, the solution can also be found by just looking at the 2nd card and the total number of cards in the deck.

Given a deck of length \$L\$, this code looks for \$n\$ such that:

$$c_2\equiv\left(\frac{k+1}{2}\right)^n\pmod k$$

where \$c_2\$ is the second card and \$k\$ is defined as:

$$k=\begin{cases} L&\text{if }L\text{ is odd}\\ L-1&\text{if }L\text{ is even}\\ \end{cases}$$

#JavaScript (ES6), 44 bytes

Shorter version thanks to @nwellnhof

f=(a,x=1)=>a[x]-1&&1+f(a,x*2%(a.length-1|1))

Try it online!


#JavaScript (ES6), (削除) 57 52 (削除ここまで) 50 bytes

Expects a 0-indexed deck as input.

f=(a,x=1,k=a.length-1|1)=>a[1]-x%k&&1+f(a,x*-~k/2)

Try it online!

###How?

Since JS is lacking native support for extracting array slices with a custom stepping, simulating the entire riffle-shuffle would probably be rather costly (but to be honest, I didn't even try). However, the solution can also be found by just looking at the 2nd card and the total number of cards in the deck.

Given a deck of length \$L\$, this code looks for \$n\$ such that:

$$c_2\equiv\left(\frac{k+1}{2}\right)^n\pmod k$$

where \$c_2\$ is the second card and \$k\$ is defined as:

$$k=\begin{cases} L&\text{if }L\text{ is odd}\\ L-1&\text{if }L\text{ is even}\\ \end{cases}$$

#JavaScript (ES6), 44 bytes

Shorter version suggested by @nwellnhof

Expects a 1-indexed deck as input.

f=(a,x=1)=>a[x]-2&&1+f(a,x*2%(a.length-1|1))

Try it online!


#JavaScript (ES6), (削除) 57 52 (削除ここまで) 50 bytes

Expects a 0-indexed deck as input.

f=(a,x=1,k=a.length-1|1)=>a[1]-x%k&&1+f(a,x*-~k/2)

Try it online!

###How?

Since JS is lacking native support for extracting array slices with a custom stepping, simulating the entire riffle-shuffle would probably be rather costly (but to be honest, I didn't even try). However, the solution can also be found by just looking at the 2nd card and the total number of cards in the deck.

Given a deck of length \$L\$, this code looks for \$n\$ such that:

$$c_2\equiv\left(\frac{k+1}{2}\right)^n\pmod k$$

where \$c_2\$ is the second card and \$k\$ is defined as:

$$k=\begin{cases} L&\text{if }L\text{ is odd}\\ L-1&\text{if }L\text{ is even}\\ \end{cases}$$

added a shorter version
Source Link
Arnauld
  • 205.5k
  • 21
  • 187
  • 670
Loading
minor update
Source Link
Arnauld
  • 205.5k
  • 21
  • 187
  • 670
Loading
more consistent variable naming
Source Link
Arnauld
  • 205.5k
  • 21
  • 187
  • 670
Loading
saved 2 bytes and added an explanation
Source Link
Arnauld
  • 205.5k
  • 21
  • 187
  • 670
Loading
saved 5 bytes
Source Link
Arnauld
  • 205.5k
  • 21
  • 187
  • 670
Loading
Source Link
Arnauld
  • 205.5k
  • 21
  • 187
  • 670
Loading

AltStyle によって変換されたページ (->オリジナル) /