I realized that I don't need to use ?
to represent the absent digit, so I used one that is a multiple of 9 after subtracting 48: x
This let me golf off the 3+
, but it only saved me 1 byte because of the length of the first line before the conditional :(
A port of my Befunge-98 answer:
5 more bytes in order to check if we've reached EOF,
1 more byte to push 48 ("0"
vs '0
),
1 more byte to print the answer with .@
,
and 1 more byte, because the second line has a space
for a total of 8 more bytes.
~:0`!#|_"0"-+
@.%9-<
Try it online!
Outputs 0
if the missing digit could be 0 or 9.
This only works in the PyFunge interpreter for reasons explained below.
Explanation
Much of this explanation is copy-pasted from my Befunge-98 explanation, as that program is very similar to this one. shameless plug
In the first line of the program, we sum the digits, including the x, which is treated like a 72 because of it's ASCII value. However, the sum will be the same once we've modded by 9, so this is irrelevant.
~:0`!#|_"0"-+ THE FIRST LINE
~ Gets a character from input - If it is negative, we've reached EOF
:0`! Pushes 0 if the character is positive, 0 otherwise
#|_ Goes to the next line if the value if 0
This also gets the negative value off the stack by using a |
"0"- Subtracts 48 to account for taking in ASCII values
+ Adds this adjusted value to the sum
If we just modded by 9, we would be left with the wrong digit, because we want 9 - (sum % 9)
. However, we can do better than 9\-
, which would subtract the remainder from 9: if we make the sum negative before modding by 9, we will get a positive result, equivalent to 9 - (sum % 9)
in some interpreters. This is what requires us to use the PyFunge interpreters for both Befunge 93 and 98, as it is the only one on TIO that does this. The others give us a value between -8 and 8 instead of 0 and 8
@.%9-< THE SECOND LINE
< Redirects the IP onto this line
- Subtracts the sum from an implicit 0, making it negative
%9 Mods the sum by 9
@. Prints the digit and exits
0
? What about[0, 9]
(array or list of 2 numbers)? \$\endgroup\$?
a possible input? \$\endgroup\$