Skip to main content
4 of 6
added 56 characters in body
Mukundan314
  • 12.7k
  • 1
  • 16
  • 58

APL (Dyalog APL), 30 bytes

Requires ⎕IO to be 0.

(+,8 16{(⎕D,⎕A)[⍺⊥⍣¯1⊢⍵]}¨⊢)⊃1

Attempt This Online!

The main idea is to use an ambivalent function, where the monadic variant would return the result, and the dyadic variant would keep track of the count.

(+,8 16{(⎕D,⎕A)[⍺⊥⍣¯1⊢⍵]}¨⊢)⊃1
‎⁡
(+,8 16{(⎕D,⎕A)[⍺⊥⍣¯1⊢⍵]}¨⊢)    # ‎⁢The ambivalent function:
 +                              # ‎⁣  monadic: identity (*); dyadic: left + right
  ,                             # ‎⁤  concatenated with
   8 16{(⎕D,⎕A)[⍺⊥⍣¯1⊢⍵]}¨⊢     # ‎⁢⁡‎⁣  octal and hexadecimal versions:
       {(⎕D,⎕A)[⍺⊥⍣¯1⊢⍵]}       # ‎⁢⁢    dyadic function to convert int to base-n string:
        (⎕D,⎕A)                 # ‎⁢⁣      digits concatenated to alphabet
               [       ]        # ‎⁢⁤      indexed by
                      ⍵         # ‎⁣⁡        right argument
                 ⊥⍣¯1           # ‎⁣⁢        in base  (returns digit vector, e.g. 42 in base-16 is [2, 10])
                ⍺               # ‎⁣⁣        left argument
                         ¨      # ‎⁣⁤    applied to:
   8                      ⊢     # ‎⁤⁡      8 and right argument,
     16                   ⊢     # ‎⁤⁢      16 and right argument,
‎⁤⁣
# ‎⁤⁤‎⁤⁢To Summarize:
# ‎⁢⁡⁡‎⁤⁣  monadic version returns: [arg, oct(arg), hex(arg)]
# ‎⁢⁡⁢  dyadic version returns: [left + right, oct(right), hex(right)]
‎⁢⁡⁣
⊃   # ‎⁢⁡⁤pick first (extracts the current count)
    # when snippet is repeated this would be in front of all the dyadic calls
    # but not in front of the final monadic call

 1  # ‎⁢⁢⁡The initial count
💎

Created with the help of Luminespire.

Mukundan314
  • 12.7k
  • 1
  • 16
  • 58