Code Golf Asked on October 27, 2021
In your language of choice: create a program that outputs 1
This 1
may either be a string or value equivalent to the number one.
If you take the unicode codepoint (or whatever codepoint encoding your languages uses if not UTF) for each character in your program, and shift each of those values by the same non-zero amount, then the result will be another program (potentially executable in different language) that also outputs 1
.
Find the unicode codepoint of a character: here.
E.g;
If your program looked like: X?$A
, and somehow output 1
, and it also miraculously outputs 1
after shifting all of it’s Unicode indices up by, say, 10; then that process of shifting looks like this:
original program: X?$A
letter codepoint shift new-codepoint new-letter
X 88 +10 98 b
? 63 73 I
$ 36 46 .
A 65 75 K
new program: BI.K
Note: The Unicode codepoint will often be represented in the form similar to U+0058
. 58
is the hexadecimal codepoint . In decimal, that’s 88
. The link above will list 88
under the UTF (decimal)
encoding section. That is the number you want to increment or decrement!
1
"1"
'1'
[1]
(1)
1.0
00000001
one
Note: If your language only supports the output of true
as an equivalent to 1
, that is acceptable. Exit-codes are also valid outputs.
1
Vyxal pads the stack with 0s when there's no input, so the list includes but is not limited to...
›
- 0 incrementedċ
- 0 != 1≥
- 0 >= 0≤
- 0 <= 0=
- 0 == 0¬
- !0₃
- 0 % 3 == 0₂
- 0 % 2 == 0₅
- 0 % 5 == 0L
- len(str(0))⌐
- 1 - 0e
- 0 ** 0ż
, if I can output in a singleton listAnswered by emanresu A on October 27, 2021
1
X
For whatever reason, CJam has X
as a builtin for 1, and since it outputs implicitly, you can just use those two. However, I thought it'd be more interesting to find a 2-byte solution.
XR
Offset by +38:
2,
Explanations:
X Push 1 to the stack R Push an empty array to the stack (implicit) Output the stack
2 Push 2 to the stack
, Pop and push range from 0 to 1 less than the popped number
(implicit) Output the stack
Note that this is not only my first time golfing, but also my first time coding a program (well, programs) in CJam, so let me know how I did!
Answered by Ethan Chapman on October 27, 2021
Uses a shift of 11.
1n;0c&
Puts 1 on the stack, outputs it as a number and stops.
<yF;n1
<
makes the direction change and start executing code from the end of the line. Puts 1 on the stack, outputs it as a number and stops.
Answered by Aaron on October 27, 2021
æ
1
I'm not sure what the offset is, but any two single-character programs can be shifted to each other.
Answered by recursive on October 27, 2021
Answered by Dallan on October 27, 2021
JavaScript, 3
3-2
becomes 2,1
shifted by -1.
1+0
becomes 2,1
shifted by +1.
Which is cool because 1+0
shifted by one becomes 2,1
shifted by one becomes 3-2
all three produce 1
let code = '1+0';
console.log (code, eval(code));
code = code.split('').map(c => String.fromCharCode(c.charCodeAt(0) + 1)).join('');
console.log (code, eval(code));
code = code.split('').map(c => String.fromCharCode(c.charCodeAt(0) + 1)).join('');
console.log (code, eval(code));
Answered by C5H8NNaO4 on October 27, 2021
-d output_buffering=on
, 21 bytes<?=1;#;><*na^bkd`m'(:
Shifted by 1
:
=@>2<$<?=+ob_clean();
Using the same technique as my Shifting Oritented Programming
answer, it's possible to get all 256 shifts for 5,366 bytes:
Answered by Dom Hastings on October 27, 2021
Answered by Dom Hastings on October 27, 2021
Edit: -2 bytes and much nicer printable programs thanks to Dom Hastings
Each program requires input of 1 byte, or a single carriage-return keypress. I've counted this as +1 byte, but I'm not terribly sure how valid this is...
$_++#^**
Shifted +1:
%`,,$_++
One might (validly) argue that since the extra input/keypress is part of the byte-count, it should also be shifted along with the codepoints of the program. Fortunately, there are inputs for which this works Ok:
echo 'a' | perl -pe '$_++#^**'
# 1
echo 'b' | perl -pe '%`,,$_++'
# 1
Answered by Dominic van Essen on October 27, 2021
Same code for javascript and brainfuck (shifted 0)
+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[1.>>0]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]
+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[1.>>0]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]
Answered by Yaroslav Gaponov on October 27, 2021
-M5.10.0
, 11 bytesUses a shift of 3
.
p^v,, say//
say//#vd|22
p^v
computes the bitwise XOR
of p
and v
then calls say//
. //
returns 1
because the empty string exists within $_
(which is the empty string).
In the shifted program, say//
is called immediately and the rest of the program is commented out.
Answered by Dom Hastings on October 27, 2021
say 1#BELp^vGS.
BEL
= ASCII 'BEL' character, octal byte 007
GS
= ASCII 'GS' character, octal byte 035
Shifted +3:
#vd|#4&
say 1
Same approach as my R answer, but with slightly (+4 characters) more verbose outputting in Perl -M5.010.
Equivalent Perl 5 program-pair (using print
instead of say
) is print 1#BELmofkqGS.
and, shifted +3, #sulqw#4&LFprint 1
.
Test at bash command-line using echo
(or gecho
):
echo -e ' say 1# 007p^v 035.' > prog1.pl
echo -e '#vd|#4&