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

added 749 characters in body
Source Link
TwiN
  • 4.8k
  • 18
  • 28

APL(Dyalog), 119116

f←{{⊃,/{2>|≡⍵:⊂⍵⋄⍵} ̈⍵}⍣≡1{(⍴⍵)≤y←⊃⍺:⍵⋄⍵[y]=0:0⋄x←⍵⋄x[y←⊃⍺]←0⋄∇∘x ̈0⋄x←⍵⋄x[y]←0⋄∇∘x ̈,∘⍺ ̈y+⍳y⌷⍵},⍵}
{0≡+/⍵: ̄1⋄(⌊/+/0=⍵)-+/0=x}↑⊃,/f ̈⌽ ̈f x←⎕

Test cases

 2 4 2 2 3 4 2 2
6
 1 0
 ̄1
 1 1 1 1
 ̄1
 3 1 2 0 4
3
 1
0

Approach

The approach is a brute force search using a recursive function.

Starting from position 1, set value at the current position to 0 and generate an array of the positions which can be jumped to from the current position. Pass the new position and the modified array to itself. Base cases are when the value at the current position is 0 (can't jump) or reaching the end.

Then, for each of the array generated, reverse it and do the search again. Because jumped positions are set to 0, we can't jump from there again.

For those array which we reached the end, find the ones which have the minimum number of 0's. Subtracting from it the number of 0's in the initial array gives the actual number of jumps performed.

APL(Dyalog), 119

f←{{⊃,/{2>|≡⍵:⊂⍵⋄⍵} ̈⍵}⍣≡1{(⍴⍵)≤y←⊃⍺:⍵⋄⍵[y]=0:0⋄x←⍵⋄x[y←⊃⍺]←0⋄∇∘x ̈,∘⍺ ̈y+⍳y⌷⍵},⍵}
{0≡+/⍵: ̄1⋄(⌊/+/0=⍵)-+/0=x}↑⊃,/f ̈⌽ ̈f x←⎕

Test cases

 2 4 2 2 3 4 2 2
6
 1 0
 ̄1
 1 1 1 1
 ̄1
 3 1 2 0 4
3
 1
0

APL(Dyalog), 116

f←{{⊃,/{2>|≡⍵:⊂⍵⋄⍵} ̈⍵}⍣≡1{(⍴⍵)≤y←⊃⍺:⍵⋄⍵[y]=0:0⋄x←⍵⋄x[y]←0⋄∇∘x ̈,∘⍺ ̈y+⍳y⌷⍵},⍵}
{0≡+/⍵: ̄1⋄(⌊/+/0=⍵)-+/0=x}↑⊃,/f ̈⌽ ̈f x←⎕

Test cases

 2 4 2 2 3 4 2 2
6
 1 0
 ̄1
 1 1 1 1
 ̄1
 3 1 2 0 4
3
 1
0

Approach

The approach is a brute force search using a recursive function.

Starting from position 1, set value at the current position to 0 and generate an array of the positions which can be jumped to from the current position. Pass the new position and the modified array to itself. Base cases are when the value at the current position is 0 (can't jump) or reaching the end.

Then, for each of the array generated, reverse it and do the search again. Because jumped positions are set to 0, we can't jump from there again.

For those array which we reached the end, find the ones which have the minimum number of 0's. Subtracting from it the number of 0's in the initial array gives the actual number of jumps performed.

Source Link
TwiN
  • 4.8k
  • 18
  • 28

APL(Dyalog), 119

f←{{⊃,/{2>|≡⍵:⊂⍵⋄⍵} ̈⍵}⍣≡1{(⍴⍵)≤y←⊃⍺:⍵⋄⍵[y]=0:0⋄x←⍵⋄x[y←⊃⍺]←0⋄∇∘x ̈,∘⍺ ̈y+⍳y⌷⍵},⍵}
{0≡+/⍵: ̄1⋄(⌊/+/0=⍵)-+/0=x}↑⊃,/f ̈⌽ ̈f x←⎕

Test cases

 2 4 2 2 3 4 2 2
6
 1 0
 ̄1
 1 1 1 1
 ̄1
 3 1 2 0 4
3
 1
0

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