Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

Restricted-source, take this!

a.k.a. You Can Output Anything With Labyrinth Or HexagonyTM

Challenge

In a recent challenge, I could print any character with only half of the allowed digits with very small character count, by abusing the "digit commands" and modulo-256 output function. Now, it's about time to make a general metagolfer for this kind of challenges.

For the sake of simplicity, we only consider Labyrinth programs of the form <digits>.@, i.e. construct a number, print it modulo 256 as character code, and halt. Also, the sequence of digits acts exactly like a number literal in this case.

Now let's assume we want to solve a challenge in the form of:

Given the characters .@ and a subset of 0-9, print a character.

Given a subset of digits D and the target character c, find the shortest number N which will solve the hypothetical challenge above. In other words, N should satisfy the following:

  • All the digits of N are in D.
  • The Labyrinth program N.@ prints the character c, i.e. N % 256 == ord(c).
    • The byte value of c can be anything between 1 and 255 inclusive.
  • Out of all possible Ns satisfying the above, your program should output one that has the shortest length. If there are multiple possible answers, your program is free to output any of them.

You may take c as a character or an integer (charcode), and digits in D as integers or digit characters. Also, you may assume D is already sorted.

Assume the answer exists. Note that some conditions will lead to "no answer", e.g. only odd digits are allowed but you need to print an even character, or vice versa. You may assume that such conditions will never be given as input.

Standard rules apply. The shortest code in bytes wins.

Test cases

The value of c is given as its character code.

D = [1, 2, 3, 4, 5]
c = 1 => N = 1
c = 57 => N = 313
c = 254 => N = 254
c = 100 => N = 1124 or 4452
c = 107 => N = 1131, 2155, 2411, or 3435
D = [1, 2, 4, 6, 8]
c = 58 => N = 826
c = 71 => N = 1111111
c = 255 => N = 26111
D = [7]
c = 49 => N = 777777

Answer*

Draft saved
Draft discarded
Cancel
2
  • 1
    \$\begingroup\$ Your recursive function looks quite a bit like an until: TIO \$\endgroup\$ Commented Apr 27, 2021 at 7:36
  • \$\begingroup\$ @xnor indeed it does :D \$\endgroup\$ Commented Apr 27, 2021 at 7:38

AltStyle によって変換されたページ (->オリジナル) /