Number conversion

JavaScript (ES6), 98 bytes

Takes input as (a)(b). Can be very slow if there's no solution, but it should eventually return \$-1\$ (at least in theory).


Try it online!

05AB1E, 38 37 bytes


Can definitely be golfed a bit more..

Thanks to @tsh for the condition of the first if-statement.

Try it online or verify a few more test cases.


3Ý             # Push the list [0,1,2,3]
  o            # For each value, take 2 to the power that value: [1,2,4,8]
   *           # Multiply each by the first (implicit) input
    Êß         # Check that the second (implicit) input is NOT in this list
8L             # Push the list [1,2,3,4,5,6,7,8]
  Iå           # Check that the second input is in this list
*i             # If both are truthy:
  ®            #  Push -1
 ë             # Else:
  ¹¸           #  Push the first input again, and wrap it into a list
    [          #  Start an infinite loop:
     D         #  Duplicate the list at the top of the stack
      Iå       #  If the second input is in this list:
        #      #   Stop the infinite loop
      ¼        #  Increase the counter variable by 1
      v        #  Loop over each number `y` in the list:
       y©      #   Store the current number `y` in variable `®` (without popping)
         v     #   Inner loop over each digit `y2` of `y`
          ®y+  #    Add digit `y2` to integer `®`
          ®y-  #    Subtract digit `y2` from integer `®`
      }}       #  After the nested loop:
        )      #  Wrap all values on the stack into a list for the next iteration
    }¾         # After the infinite loop: push the counter variable
               # (and output the top of the stack implicitly as result)

Jelly, 33 29 bytes


Try it online!

A full program that takes [[a]] as the left argument and b as the right argument.

More efficient version (36 bytes)


Code Golf