Skip to main content
Commonmark migration
Source Link
Source Link
Kevin Cruijssen
  • 136.3k
  • 14
  • 155
  • 394

#05AB1E, 36 bytes

x*ÝʒÅfIÄQ}Ii®šë1KIdiÐ`ÉiD(ì}ëD`Èi(ë¯ 

There has to be a better approach.. >.> There are six (or seven if we include 0) different scenarios for this challenge, and it's killing me..

Try it online or verify all test cases.

Explanation:

x*Ý # Create a list in the range [0, (implicit) input * input * 2] ʒ } # Filter this list by: Åf # Where the Fibonacci value at that index IÄQ # Is equal to the absolute value of the input Ii # If the input is exactly 1: ®š # Prepend -1 to the list ë # Else: 1K # Remove all 1s (only applies to input -1) Idi # If the input is non-negative: Ð`Éi } # If the found index in the list is odd: D(ì # Prepend its negative index to the list ë # Else (the input is negative): D`Èi # If the found index in the list is even: ( # Negate the found index ë # Else (found index is odd): ¯ # Push an empty array # (Output the top of the stack implicitly as result) 

Some step-by-step examples:

Input: Filtered indices: Path it follows (with actions) and result: -8 [6] NOT 1 → neg → even index → negate index: [-6] -5 [5] NOT 1 → neg → odd index → push empty array: [] -1 [1,2] NOT 1 → (remove 1) neg → even remaining index: negate index: [-2] 0 [0] NOT 1 → even index → negate index: [0] 1 [1,2] 1 → prepend -1: [-1,1,2] 5 [5] NOT 1 → non-neg → odd index → Prepend neg index: [-5,5] 8 [6] NOT 1 → non-neg → even index → (nothing): [6]