20
\$\begingroup\$

I have a problem for a cyber club that asks you to print:

ZYXWVUTSRQPONMLKJIHGFEDCBA@?>=<;:9876543210/.-,+*)('&%$#"!

Using Brainf**k in 29 bytes or less without using the ',' character.

I have a working code:

++++++++++[>++++++>+++++++++<<-]>-->.<[>-.<-]

However my loop is too long, sending me 16 bytes over the limit.

Is there a more efficient way to set the second and third cell to 58 and 90 so I can run my second loop? Or is there a better way to do this all together that I am just not seeing?

\$\endgroup\$
4
  • 1
    \$\begingroup\$ As for your question, while it only saves four bytes, it's actually better to generate 59 and 90 and change the -. to .-, dropping the >.<. \$\endgroup\$ Commented Aug 27, 2016 at 15:05
  • \$\begingroup\$ I got +[--->++<]>++++[.-] for 19 bytes but it prints the control chars as well... \$\endgroup\$
    – Timtech
    Commented Aug 27, 2016 at 18:52
  • \$\begingroup\$ @MartinEnder Yeah sorry about that, I saw your comment on the other post and found this group which seemed much more appropriate, I will delete the one on SO since it didn't generate many answers. \$\endgroup\$
    – Nick Rod
    Commented Aug 28, 2016 at 4:57
  • 2
    \$\begingroup\$ I'm honestly curious what this "cyber club" is, because boy they must have some really good golfers! \$\endgroup\$
    – Sp3000
    Commented Aug 28, 2016 at 11:40

3 Answers 3

24
+100
\$\begingroup\$

27 24 bytes

++[<++[++<]>>>+]<[-<-.>]

Spent a whole day basically writing up a brute forcer and watching the results come in. Now I can get back to doing some actual work... Another day of brute forcing later...

Try it online!

The component ++[<++[++<]>>>+] initialises the tape to

[130, 0, 0, 0, 91, 59, 0]
                       ^

which is just perfect for what we need!

\$\endgroup\$
4
  • 8
    \$\begingroup\$ Wizardry. (∩`-´)⊃━☆゚.*・。゚ \$\endgroup\$
    – primo
    Commented Aug 28, 2016 at 10:57
  • \$\begingroup\$ @primo To be fair I have no idea what's going on structure-wise in the first half, and I think the fact that you're able to come up with your own algorithms/techniques is amazing :) \$\endgroup\$
    – Sp3000
    Commented Aug 28, 2016 at 11:21
  • \$\begingroup\$ Tape at the end of each loop: codepad.org/ZoJUlQ8M. It's not at all intuitive that it would terminate at the values that it does, or even at all ;) \$\endgroup\$
    – primo
    Commented Aug 28, 2016 at 11:40
  • 1
    \$\begingroup\$ An alternative 24: +[[>++<<]>->+>+]<[-<-.>] \$\endgroup\$
    – primo
    Commented May 3, 2018 at 11:19
15
\$\begingroup\$

30 bytes

-<-[>+>-[---<]>]>++>+[---<.->]

Try it online!

85 and 171 are fairly easy to generate in brainfuck (modular inverses of 3 and -3). 85 is pretty close to 90, and 171 is pretty close to 177 (59·3), which is used here. With a bit of hackery, I'm able to produce 88 and 176 instead.

Still one byte short of the target, though.

Other suggestions

In general, it's shorter to iterate over a list, multiplying by a constant, rather than the other way. This is especially true for 3 or more values. For example, this:

++++++++++[>++++++>+++++++++<<-]

can be written as:

+>++++[+++++[>++++++++++<-]<]>

There were only two inner values, so it's not much of an improvement in this case. In fact, just refactoring is one byte shorter:

+++++[>++++++[>++>+++<<-]<-]>

Multiplying 30 by 2 and 3, rather than 10 by 6 and 9. With Martin Büttner's suggestion, this is already down to 38 bytes, without much change to the original:

+++++[>++++++[>++>+++<<-]<-]>>-[>.-<-]
\$\endgroup\$
1
  • \$\begingroup\$ I never would have thought about multiplying up the 58 and iterating by a larger number, that's much more efficient. \$\endgroup\$
    – Nick Rod
    Commented Aug 28, 2016 at 5:13
4
\$\begingroup\$

34 bytes

Saved you 11 bytes, but still 5 bytes too long...

+[--->++<]>++++<+[--------->.-<]>.

I've spent hours already, hopefully someone can improve on this.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.