# 42 in Rod Logic

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.

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

# Brainfuck, 25

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


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

Works by adding 7 to the input 6 times.

# 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.";


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

# 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

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

## 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
_=[]

#   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 __
}

return false;
});
<label>
Enter a number:
<input type=number id=foo value=0>
</label>
<button type=button id=btn>Add 42</button>

# 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.

• 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. – rink.attendant.6 Jul 3 '15 at 23:06
• @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. – Sp3000 Jul 4 '15 at 2:52

# piet - 14

This might count. All of the lines are straight.

• The question explicitly whitelists the allowed characters. Your program doesn't use those. – Martin Ender Mar 19 '15 at 15:37

## 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;

_____(________(_) ___(_) ______(_) __(_) ___(_))

• alphanumerics are out, so no #defines I'm afraid. I don't think this is possible in C++. – Level River St Mar 19 '15 at 1:24
• This program exhibits undefined behavior and is as such not repeatable. – rightfold Jun 1 '16 at 11:52