2
\$\begingroup\$

Write the shortest possible program which takes a numeric input and outputs the result of adding 42 to the input.

f(x) -> x + 42

There's only one restriction. Your program can only be composed of rod logic characters...


The definition for a rod logic character is a one that is composed purely of straight lines. They come from the crazy Temporally Quaquaversal Virtual Nanomachines talk by Damian Conway. To simplify things The following will be considered the set of all valid rod logic characters.

_ | - = \ / < > [ ] ^ +

Damian writes a rod logic program in Perl which takes a numeric input and outputs the result of adding 42 in 170 lines. He might be great at quantum mechanics in Perl, but I'm not sure he's played much code golf. The challenge is to improve on that.

Like Damian's example we'll say that you have access to two predefined functions:

  • __ - which prints arguments
  • ___ - which reads and returns user input

If these aren't valid function identifiers in your language, you can replace them with other (2 and 3 byte ones)

Shortest program wins.

\$\endgroup\$
  • \$\begingroup\$ Do rod characters prohibit crossing lines? For example '4' or 'x'? What about serifs in things like '1'? \$\endgroup\$ – orlp Mar 18 '15 at 23:06
  • \$\begingroup\$ @orlp maybe not, but alphanumeric characters are forbidden anyway \$\endgroup\$ – edc65 Mar 18 '15 at 23:08
  • \$\begingroup\$ No way to do this in C like languages (C, C++, Java, Javascript) as calling a function require () and these have curves \$\endgroup\$ – edc65 Mar 18 '15 at 23:09
  • \$\begingroup\$ Crossing lines is fine! Serifs aren't. Alphanumerics are gone to try and make that murky territory a bit easier. \$\endgroup\$ – Dan Prince Mar 18 '15 at 23:09
  • 2
    \$\begingroup\$ @DanPrince I'm sorry, this golf question got a downvote from me, it seems to be too arbitrary to be fun to me. \$\endgroup\$ – orlp Mar 18 '15 at 23:17
9
\$\begingroup\$

Brainfuck, 25

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

Counting , as ___ (input) and . as __ (output).

Works by adding 7 to the input 6 times.

\$\endgroup\$
8
\$\begingroup\$

CJam, 22 21 bytes

CJam only supports single-character function (block) names, all of which must be uppercase letters. I've used A in place of ___ and counted it as 3 bytes:

]_=__+__+++__++_+A+

Test it here.

Explanation

]_=                  "Make an empty array, duplicate, check for equality, pushing 1.";
   __+               "Get two copies and add them, leaving the stack as [1 2].";
      __+++          "Make two copies add it all up, yielding 7.";
           __++      "Make two copies, add them up, yielding 21.";
               _+    "Make a copy add it, yielding 42.";
                 A+  "Read the input and add it.";

CJam automatically prints the stack contents at the end of the program.

\$\endgroup\$
3
\$\begingroup\$

JavaScript, 64+3+2=69

+[+[++[++[++[+[]==[]][+[]]][+[]]][+[]]+[++[+[]==[]][+[]]]]][+[]]

To test it, write a number followed by the whole program in your browser's console.
I didn't explicitly use input and output functions, but I added 3 and 2 bytes to account for them.

Explanation:

+[] ➜ 0
0==[] ➜ true
++[true][0] ➜ 2
++[2][0] ➜ 3
++[3][0] ➜ 4
4+[2] ➜ "42"
+["42"] ➜ 42
x+[42][0] ➜ x+42
\$\endgroup\$
  • \$\begingroup\$ I tried beautifying the code to step through it but I gave up after a little. Props to extreme type coercion. \$\endgroup\$ – rink.attendant.6 Jul 3 '15 at 21:49
  • \$\begingroup\$ @rink.attendant.6 thanks, updated now \$\endgroup\$ – aditsu Jul 4 '15 at 6:15
  • \$\begingroup\$ I eventually figured it out though I was a bit surprised that 4+[2] evaluates to "42". \$\endgroup\$ – rink.attendant.6 Jul 4 '15 at 6:18
  • \$\begingroup\$ +[]==[] => []<=[]? \$\endgroup\$ – l4m2 Nov 18 '18 at 13:40
2
\$\begingroup\$

JavaScript / TypeScript / CoffeeScript, 38 bytes

_ is a valid identifier in JS.

The question is somewhat unclear but I'm assuming that ___ stores input and __ stores output.

The newlines are necessary (and counted as 1 byte each) regardless of whether you're running it as JavaScript (for ASI) or CoffeeScript (for compilation to JS).

_=[]
___+=++_<<++_<<++_
__=___+_+_+++_

Explanation

# assigns empty array to _ which will be coerced to number 0 later
_=[]

# Add 32 to input:
#   0+1 << (0+1)+1 << ((0+1)+1)+1 = 1 << 2 << 3
#    = 1 << 2 << 3
#    = 4 << 3
#    = 32 or 0b00100000
# (Also note that _ = 3 now)
___+=++_<<++_<<++_

# Previous value + 3 + 3 + 4
# Total additions: 32 + 3 + 3 + 4 = 42
__=___+_+_+++_

Demo

This is the best I can try to demonstrate it:

function ____(___) {
  _ = []
  ___ += ++_ << ++_ << ++_
  __ = ___ + _ + _ + ++_
  return __
}

document.getElementById('btn').addEventListener('click', function() {
  window.alert(____(+document.getElementById('foo').value));
  return false;
});
<label>
  Enter a number:
  <input type=number id=foo value=0>
</label>
<button type=button id=btn>Add 42</button>

\$\endgroup\$
2
\$\begingroup\$

Pyth, 54 + 3 bytes = 57

+vz-+^/+\\\\\\/+++\\\\\\\\\\^/++\\\\\\\\/++\\\\\\\\/\\\\

Just to show that it can be done in Pyth. ___ isn't valid in Pyth, so I'm using vz.

/ is the only command which returns a number, when used as / <string1> <string2> to mean string1.count(string2), so we use it excessively to create numbers. The 42 is calculated as 2^4 + 3^3 - 1.

Try it online.

\$\endgroup\$
  • \$\begingroup\$ I don't know Pyth but if you use a letter like L, T, E, etc. it would be more fitting with the straight lines concept. \$\endgroup\$ – rink.attendant.6 Jul 3 '15 at 23:06
  • \$\begingroup\$ @rink.attendant.6 Q is evaluated input in Pyth, which is why I chose it. vz works just as well, so I've changed it to that. \$\endgroup\$ – Sp3000 Jul 4 '15 at 2:52
0
\$\begingroup\$

piet - 14

This might count. All of the lines are straight.

enter image description here

\$\endgroup\$
  • 4
    \$\begingroup\$ The question explicitly whitelists the allowed characters. Your program doesn't use those. \$\endgroup\$ – Martin Ender Mar 19 '15 at 15:37
-2
\$\begingroup\$

C++

This is a work in progress... how can I replace () with []? Is it possible?

#include <iostream>
using namespace std;

#define _____(a) int main(){a return 0;}
#define ___(y) cin>>y;
#define __(x) cout<<x<<endl;
#define ______(z) z+=42;
#define ________(p) int p;

_____(________(_) ___(_) ______(_) __(_) ___(_))
\$\endgroup\$
  • 2
    \$\begingroup\$ alphanumerics are out, so no #defines I'm afraid. I don't think this is possible in C++. \$\endgroup\$ – Level River St Mar 19 '15 at 1:24
  • \$\begingroup\$ This program exhibits undefined behavior and is as such not repeatable. \$\endgroup\$ – rightfold Jun 1 '16 at 11:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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