Skip to main content
added 177 characters in body
Source Link
Neil
  • 177.2k
  • 12
  • 74
  • 281

Charcoal, 32 2929 27 bytes

≔⁰θFS≧⁺⁺∧﹪℅ι²⊗θ℅ιθ‹³﹪θ⁸§1ijkθ≔⁰θFS≔⁺×θX³℅ι℅ιθ‹³﹪θ⁸§1ijkθ

Try it online!Try it online! Link is to verbose version of code. Explanation: The values are encoded to integers equivalent to 0..7 (modulo 8) in the order 1, i, j, k, -1, -i, -j, -k. The multiplications by i, j and k have the following effect on the integer:

  • Multiplying by i is equivalent to tripling the integer and adding 1.
  • Multiplying by j is equivalent to adding 2 to the integer.
  • Multiplying by k is equivalent to tripling the integer and adding 3.

Edit: Thanks to @NickKennedy for saving 3 bytes by pointing out that the ordinals of i, j and k are equivalent (mod 8) to 1, 2 and 3 respectively. This means that the integer needs to be tripled if the ordinal is odd, and then the ordinal can be added to it.

Edit: Furthermore, tripling the integer (mod 8) if the ordinal is odd is equivalent to multiplying the integer by 3 to the power of the ordinal, for a further 2 byte saving.

≔⁰θ

Start with 0.

FS

Loop over the input...

≧⁺⁺∧﹪℅ι²⊗θ℅ιθ≔⁺×θX³℅ι℅ιθ

... adding twicemultiply the current value ifby 3 to the power of the ordinal is odd, plusthen add the ordinal.

‹³﹪θ⁸

Output a - sign if the result (mod 8) is greater than 3. (There are other ways to express this in Charcoal but sadly I couldn't do better than 5 bytes.)

§1ijkθ

Output 1, i, j, or k, depending on the result (mod 4).

Charcoal, 32 29 bytes

≔⁰θFS≧⁺⁺∧﹪℅ι²⊗θ℅ιθ‹³﹪θ⁸§1ijkθ

Try it online! Link is to verbose version of code. Explanation: The values are encoded to integers equivalent to 0..7 (modulo 8) in the order 1, i, j, k, -1, -i, -j, -k. The multiplications by i, j and k have the following effect on the integer:

  • Multiplying by i is equivalent to tripling the integer and adding 1.
  • Multiplying by j is equivalent to adding 2 to the integer.
  • Multiplying by k is equivalent to tripling the integer and adding 3.

Edit: Thanks to @NickKennedy for saving 3 bytes by pointing out that the ordinals of i, j and k are equivalent (mod 8) to 1, 2 and 3 respectively. This means that the integer needs to be tripled if the ordinal is odd, and then the ordinal can be added to it.

≔⁰θ

Start with 0.

FS

Loop over the input...

≧⁺⁺∧﹪℅ι²⊗θ℅ιθ

... adding twice the current value if the ordinal is odd, plus the ordinal.

‹³﹪θ⁸

Output a - sign if the result (mod 8) is greater than 3. (There are other ways to express this in Charcoal but sadly I couldn't do better than 5 bytes.)

§1ijkθ

Output 1, i, j, or k, depending on the result (mod 4).

Charcoal, 32 29 27 bytes

≔⁰θFS≔⁺×θX³℅ι℅ιθ‹³﹪θ⁸§1ijkθ

Try it online! Link is to verbose version of code. Explanation: The values are encoded to integers equivalent to 0..7 (modulo 8) in the order 1, i, j, k, -1, -i, -j, -k. The multiplications by i, j and k have the following effect on the integer:

  • Multiplying by i is equivalent to tripling the integer and adding 1.
  • Multiplying by j is equivalent to adding 2 to the integer.
  • Multiplying by k is equivalent to tripling the integer and adding 3.

Edit: Thanks to @NickKennedy for saving 3 bytes by pointing out that the ordinals of i, j and k are equivalent (mod 8) to 1, 2 and 3 respectively. This means that the integer needs to be tripled if the ordinal is odd, and then the ordinal can be added to it.

Edit: Furthermore, tripling the integer (mod 8) if the ordinal is odd is equivalent to multiplying the integer by 3 to the power of the ordinal, for a further 2 byte saving.

≔⁰θ

Start with 0.

FS

Loop over the input...

≔⁺×θX³℅ι℅ιθ

... multiply the current value by 3 to the power of the ordinal, then add the ordinal.

‹³﹪θ⁸

Output a - sign if the result (mod 8) is greater than 3. (There are other ways to express this in Charcoal but sadly I couldn't do better than 5 bytes.)

§1ijkθ

Output 1, i, j, or k, depending on the result (mod 4).

deleted 348 characters in body
Source Link
Neil
  • 177.2k
  • 12
  • 74
  • 281

Charcoal, 3232 29 bytes

≔⁰θFS≧⁺⁺∧﹪℅ι²⊕⊗θ&℅ι²θ‹³﹪θ⁸§1ijkθ≔⁰θFS≧⁺⁺∧﹪℅ι²⊗θ℅ιθ‹³﹪θ⁸§1ijkθ

Try it online!Try it online! Link is to verbose version of code. Explanation: The values are encoded to integers equivalent to 0..7 (modulo 8) in the order 1, i, j, k, -1, -i, -j, -k. The multiplications by i, j and k have the following effect on the integer:

  • Multiplying by i is equivalent to tripling the integer and adding 1.
  • Multiplying by j is equivalent to adding 2 to the integer.
  • Multiplying by k is equivalent to tripling the integer and adding 3.

This is further simplifiedEdit: Thanks to @NickKennedy for saving 3 bytes by noting that k=ij, which meanspointing out that at each step if the input isordinals of i or k then the integer is tripled and incremented, and if the input is j orand k thenare equivalent 2 is added(mod 8) to it.

Furthermore, the ASCII ordinals of the letters i, j1, and k have the convenient property that i2 and k3 are both odd (indicatingrespectively. This means that the integer needs to be tripled and incremented) while j and k haveif the next bit setordinal is odd, thus a bitwise And with 2 results inand then the extra amount toordinal can be added when the input is j or kto it.

≔⁰θ

Start with 0.

FS

Loop over the input...

≧⁺⁺∧﹪℅ι²⊕⊗θ&℅ι²θ≧⁺⁺∧﹪℅ι²⊗θ℅ιθ

... adding twice the current value plus 1 and/or 2 to the current value depending onif the least significant bits ofordinal is odd, plus the ordinal.

‹³﹪θ⁸

Output a - sign if the result (mod 8) is greater than 3. (There are other ways to express this in Charcoal but sadly I couldn't do better than 5 bytes.)

§1ijkθ

Output 1, i, j, or k, depending on the result (mod 4).

Charcoal, 32 bytes

≔⁰θFS≧⁺⁺∧﹪℅ι²⊕⊗θ&℅ι²θ‹³﹪θ⁸§1ijkθ

Try it online! Link is to verbose version of code. Explanation: The values are encoded to integers equivalent to 0..7 (modulo 8) in the order 1, i, j, k, -1, -i, -j, -k. The multiplications by i, j and k have the following effect on the integer:

  • Multiplying by i is equivalent to tripling the integer and adding 1.
  • Multiplying by j is equivalent to adding 2 to the integer.
  • Multiplying by k is equivalent to tripling the integer and adding 3.

This is further simplified by noting that k=ij, which means that at each step if the input is i or k then the integer is tripled and incremented, and if the input is j or k then 2 is added to it.

Furthermore, the ASCII ordinals of the letters i, j, and k have the convenient property that i and k are both odd (indicating that the integer needs to be tripled and incremented) while j and k have the next bit set, thus a bitwise And with 2 results in the extra amount to be added when the input is j or k.

≔⁰θ

Start with 0.

FS

Loop over the input...

≧⁺⁺∧﹪℅ι²⊕⊗θ&℅ι²θ

... adding twice the current value plus 1 and/or 2 to the current value depending on the least significant bits of the ordinal.

‹³﹪θ⁸

Output a - sign if the result (mod 8) is greater than 3. (There are other ways to express this in Charcoal but sadly I couldn't do better than 5 bytes.)

§1ijkθ

Output 1, i, j, or k, depending on the result (mod 4).

Charcoal, 32 29 bytes

≔⁰θFS≧⁺⁺∧﹪℅ι²⊗θ℅ιθ‹³﹪θ⁸§1ijkθ

Try it online! Link is to verbose version of code. Explanation: The values are encoded to integers equivalent to 0..7 (modulo 8) in the order 1, i, j, k, -1, -i, -j, -k. The multiplications by i, j and k have the following effect on the integer:

  • Multiplying by i is equivalent to tripling the integer and adding 1.
  • Multiplying by j is equivalent to adding 2 to the integer.
  • Multiplying by k is equivalent to tripling the integer and adding 3.

Edit: Thanks to @NickKennedy for saving 3 bytes by pointing out that the ordinals of i, j and k are equivalent (mod 8) to 1, 2 and 3 respectively. This means that the integer needs to be tripled if the ordinal is odd, and then the ordinal can be added to it.

≔⁰θ

Start with 0.

FS

Loop over the input...

≧⁺⁺∧﹪℅ι²⊗θ℅ιθ

... adding twice the current value if the ordinal is odd, plus the ordinal.

‹³﹪θ⁸

Output a - sign if the result (mod 8) is greater than 3. (There are other ways to express this in Charcoal but sadly I couldn't do better than 5 bytes.)

§1ijkθ

Output 1, i, j, or k, depending on the result (mod 4).

Source Link
Neil
  • 177.2k
  • 12
  • 74
  • 281

Charcoal, 32 bytes

≔⁰θFS≧⁺⁺∧﹪℅ι²⊕⊗θ&℅ι²θ‹³﹪θ⁸§1ijkθ

Try it online! Link is to verbose version of code. Explanation: The values are encoded to integers equivalent to 0..7 (modulo 8) in the order 1, i, j, k, -1, -i, -j, -k. The multiplications by i, j and k have the following effect on the integer:

  • Multiplying by i is equivalent to tripling the integer and adding 1.
  • Multiplying by j is equivalent to adding 2 to the integer.
  • Multiplying by k is equivalent to tripling the integer and adding 3.

This is further simplified by noting that k=ij, which means that at each step if the input is i or k then the integer is tripled and incremented, and if the input is j or k then 2 is added to it.

Furthermore, the ASCII ordinals of the letters i, j, and k have the convenient property that i and k are both odd (indicating that the integer needs to be tripled and incremented) while j and k have the next bit set, thus a bitwise And with 2 results in the extra amount to be added when the input is j or k.

≔⁰θ

Start with 0.

FS

Loop over the input...

≧⁺⁺∧﹪℅ι²⊕⊗θ&℅ι²θ

... adding twice the current value plus 1 and/or 2 to the current value depending on the least significant bits of the ordinal.

‹³﹪θ⁸

Output a - sign if the result (mod 8) is greater than 3. (There are other ways to express this in Charcoal but sadly I couldn't do better than 5 bytes.)

§1ijkθ

Output 1, i, j, or k, depending on the result (mod 4).