TransWikia.com

All you have to do is print the number 1! ...Twice

Code Golf Asked on October 27, 2021

Your task

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.

The shifting catch

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!

Examples of valid outputs

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.

Scoring

  • This is , so lowest bytes wins!
  • Brownie points for creativity & if the two programs are in separate languages.

41 Answers

Javastack , 1 byte

1

Vyxal, 1 byte

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 == 0
  • L - len(str(0))
  • - 1 - 0
  • e - 0 ** 0
  • ż, if I can output in a singleton list

Answered by emanresu A on October 27, 2021

CJam, 1 byte

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

Try it online!

Offset by +38:

2,

Try it online!

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

><> x2, 6 bytes

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

Stax, 1 bytes

æ

Run and debug it

Stax, 1 bytes

1

Run and debug it

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

Befunge-93, 7 bytes

Original code:

>1.@B03

Try it online!

Shifted by -2:

</,>@.1

Try it online!

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

PHP + -d output_buffering=on, 21 bytes

<?=1;#;><*na^bkd`m'(:

Try it online!

Shifted by 1:

=@>2<$<?=+ob_clean();

Try it online!

Using the same technique as my Shifting Oritented Programming answer, it's possible to get all 256 shifts for 5,366 bytes:

Try it online!

Answered by Dom Hastings on October 27, 2021

Retina, 1 byte

^

Try it online!

Shifted by 30:

|

Try it online!

Answered by Dom Hastings on October 27, 2021

Perl 5 -p, 8 bytes + 1 byte/keypress input = 9 bytes

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

$_++#^**

Try it online!

Shifted +1:

%`,,$_++

Try it online!

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)

JavaScript (V8), 152 bytes

+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[1.>>0]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]

Try it online!

brainfuck, 152 bytes

+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[+[1.>>0]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]

Try it online!

Answered by Yaroslav Gaponov on October 27, 2021

Perl 5 + -M5.10.0, 11 bytes

Uses a shift of 3.

p^v,, say//

Try it online!

say//#vd|22

Try it online!

Explanation

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

Perl -M5.010, 13 bytes

Perl 5, 21 bytes

 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^v035.' > prog1.pl
echo -e '#vd|#4&012say 1' > prog2.pl
perl -M5.010 prog1.pl
# 1
perl -M5.010 prog2.pl
# 1

Answered by Dominic van Essen on October 27, 2021

R, 4 bytes

61433077

(octal bytes, equivalent to: '1' '#' CAN '?')

Shifted -14:

43251261

(octal bytes, equivalent to '#' NAK LF '1')

Unshifted program consists of number 1 (which is outputted unchanged), followed by # (comment character) and 'comments' of CAN (ASCII code 30) and '?'.

Shifted +14 program consists of # (comment character) and 'comment' of NAK (ASCII code 25), followed by a new line. On the next line is the number 1 (which is outputted unchanged).

Test at bash command-line using echo (or gecho):

echo -e '61433077' >prog1.r
echo -e '43251261' >prog2.r
Rscript prog1.r
# [1] 1
Rscript prog2.r
# [1] 1

Answered by Dominic van Essen on October 27, 2021

Turing Machine Code, 19 bytes

0 _ 1 r 1
1!'!2!s!2

Try it online!

Nothing too fancy. If the code is shifted by -1, the second line transforms into the code of the first line. The first line transforms into an unexecutable bit of code. The interpreter can, and does, run just fine with unexecutable code dispersed here and there, and so it is just executing the same code either way.

Answered by ouflak on October 27, 2021

pdfTeX -halt-on-error, 1 byte

_

and

^

Both versions will throw an error as _ and ^ are only allowed in math-mode. Will return a 1 as exit code (due to the error).

Answered by Skillmon on October 27, 2021

Brainetry, 23 bytes

Golfed version (both lines end with a space):

# # # #  
# # # # # # # 

We must shift the #s so they become spaces for the program to work again, so the required shift is -3.

The base program from which I derived the above:

This Brainetry program takes 
no input and prints the codepoint 1. 

Answered by RGS on October 27, 2021

Hexagony, 5 bytes

=-1!@

Try it online!

Hexagony, 5 bytes, Shifted -12

1!%4

Hexdump:

00000000  31 21 25 15 34     |1!%.4|

Try it online!

Explanation

Because of the small size and lack of mirrors, the programs are mostly executed linearly.

Non-shifted Program

=       Reverse direction of memory pointer
 -      Sets the current memory edge to the difference of the left and right neighbors (0 - 0 = 0)
  1     Multiply current memory edge by 10 then add 1 (0 * 10 + 1 = 1)
   !    Outputs the value of current memory edge to stdout as an integer
    @   Terminate the program

Shifted Program

1       Multiply current memory edge by 10 then add 1 (0 * 10 + 1 = 1)
 !      Outputs the value of current memory edge to stdout as an integer
  %     Sets the current memory edge to the modulo of the left and right neighbours
        (1 % 0) causes program to exit because of division by 0
    4   Does not matter since the program as already exited

Answered by Mukundan314 on October 27, 2021

HTML, 8 bytes

<ol><li>


05AB1E, 8 bytes

!TQ#!QN#

Try it online!


Jelly, 8 bytes

([X*(XU*

Try it online!


Jelly, 8 bytes

5he75eb7

Try it online!


Jelly, 8 bytes

;nk=;kh=

Try it online!


05AB1E, 8 bytes

@spB@pmB

Try it online!


Jelly, 8 bytes

H{xJHxuJ

Try it online!


05AB1E, 8 bytes

Outputs ["1"].

Q„SQ~S

Try it online!


05AB1E, 8 bytes

V‰†XV†ƒX

Try it online!


05AB1E, 8 bytes

X‹ˆZXˆ…Z

Try it online!


05AB1E, 8 bytes

]_]Š_

Try it online!


05AB1E, 8 bytes

a”‘ca‘Žc

Try it online!


05AB1E, 8 bytes

e˜•ge•’g

Try it online!


05AB1E, 8 bytes

kž›mk›˜m

Try it online!


("Pffffft! Of course I know how 05AB1E and Jelly work! I totally didn't just brute-force a bunch of combinations on TIO. That would be crazy! It would never work!")

Answered by darrylyeo on October 27, 2021

;#+, 4 bytes

9n;p

Try it online!

;#+, 4 bytes, Shift +2

;p=r

Try it online!


Explanation

; - increments the counter
p - outputs the counter as a number

9, n, = and r are not commands in ;#+ so they can be ignored.

Answered by Mukundan314 on October 27, 2021

Jelly, 1 byte

¬ (logical NOT) vs (increment)

Try ¬ online! or Try online!

This works because given no input a Jelly program has a default argument of 0.

There are $binom{21}{2}=210$ different pairs of single-byte programs to choose from since there are $21$ single bytes on Jelly's code-page which yield 1 with no input:

Answered by Jonathan Allan on October 27, 2021

JavaScript (web), 19 bytes

Form #1

`kdqs_0_:`;alert`1`

Form #2 (#1 shifted by +1)

alert`1`;a<bmfsua2a

This took me longer than what I'd like to admit, but it was a fun challenge. ?

Both forms throw a ReferenceError, but that seems to be allowed.

Answered by D. Pardal on October 27, 2021

naz, 4 bytes

1a1o

Explanation

1a # Add 1 to the register
1o # Output once

05AB1E, 4 bytes

2b2p

A shift of 1 Unicode codepoint forward from the original.

Explanation

2    # Push 2
 b   # Convert to binary
  2  # Push 2
   p # Push isPrime(2)
# ...after which the result is output implicitly

Answered by sporeball on October 27, 2021

Batch, 27 17 bytes

:_]bi�+�4
@echo 1

The : introduces a label of unprintables, so the line is ignored, and the second line prints 1. Shifted by 6:

@echo 1
:�Fkinu&7

Much the same, except this time the second line is ignored.

Unfortunately I've mangled the unprintables. Sorry about that. Feel free to fix it.

Answered by Neil on October 27, 2021

Polyglot, 3 bytes

Shift of 2. Works in R, Octave, Japt, and probably others.

1+0
3-2

Try it online (Octave)

Try it online (R)

Try it online (Japt)

Answered by Robin Ryder on October 27, 2021

bc (?), 3 bytes

1+0

Shift 2:

3-2

Use as echo 1+0 | bc in bash.

Answered by tsh on October 27, 2021

CSS, 48 bytes

body:after{content:"1"}z|ancx9`esdqzbnmsdms9!0!|

cpez;bgufs|dpoufou;#2#~{}body:after{content:"1"}

Answered by tsh on October 27, 2021

V (vim), 6 bytes

i1<esc><nul>h0

Answered by tsh on October 27, 2021

Unary, 84 bytes

000000000000000000000000000000000000000000000000000000000000000000000000000000000000

Outputs the character with codepoint 1 (equivalent brainfuck: +.). Since Unary cares only about the length of the program, a shift of any number will not change the output.

Answered by The Fifth Marshal on October 27, 2021

Python 3, 19 17 bytes

-2 bytes thanks to Jonathan Allan

#]pal )!␛
exit(1)

Try it online!

Shifted +8

+exit(1)#␒m␣q|091

Try it online!

where ␛, ␒ and ␣ are literal x1b, x12 and x80 bytes respectively.

Not much by the way of trickery going on here except prepending the print in the shift version with a + so that when we shift it the first character of the second program to the # character it doesn't send any characters into a negative codepoint (if we naïvely shifted e back to #, ( would be sent to x- which doesn't exist). Outputs by exit code.

Answered by boboquack on October 27, 2021

Java, 62 bytes

interface M{static void main(String[]a){System.out.print(1);}}

Try it online.

05AB1E, 62 bytes

agXeYTVXι@nfgTgVιibWι`Ta₂FgeaZNPT₃nFlfgX`!bhg!ceag₂$₃.pp

Uses the 05AB1E encoding, with the codepoints all decreased by 13:

  • interface M{static void main(String[]a){System.out.print(1);}} has the codepoints [105,110,116,101,114,102,97,99,101,32,77,123,115,116,97,116,105,99,32,118,111,105,100,32,109,97,105,110,40,83,116,114,105,110,103,91,93,97,41,123,83,121,115,116,101,109,46,111,117,116,46,112,114,105,110,116,40,49,41,59,125,125]
  • agXeYTVXι@nfgTgVιibWι`Ta₂FgeaZNPT₃nFlfgX`!bhg!ceag₂$₃.pp has the codepoints [92,97,103,88,101,89,84,86,88,19,64,110,102,103,84,103,92,86,19,105,98,92,87,19,96,84,92,97,27,70,103,101,92,97,90,78,80,84,28,110,70,108,102,103,88,96,33,98,104,103,33,99,101,92,97,103,27,36,28,46,112,112].

Try it online.

Explanation:

Java:

interface M{                   // Full program:
  static void main(String[]a){ //  Mandatory main-method:
    System.out.print(          //   Print without trailing newline:
      1);}}                    //    Print 1

05AB1E:

                  # Discard the top of the stack (no-op, since it's already empty)
                   #  STACK: []
 a                 # Check if it only consists of letters (resulting in falsey/0
                   # for an empty string "", which is used implicitly without input)
                   #  STACK: [0]
  g                # Push and push its length, which is 1
                   #  STACK: [1]
   X               # Push variable `X`, which is 1 by default
                   #  STACK: [1,1]
    e              # Push the number of permutations n!/(n-r)! with both 1s, which is 1
                   #  STACK: [1]
     Y             # Push variable `Y`, which is 2 by default
                   #  STACK: [1,2]
      T            # Push builtin 10
                   #  STACK: [1,2,10]
       V           # Pop and store it in variable `Y`
                   #  STACK: [1,2]
        X          # Push variable `X` again, which is 1 by default
                   #  STACK: [1,2,1]
         ι         # Uninterleave using the 2 and 1, resulting in ["2"]
                   #  STACK: [1,["2"]]
          @        # Check whether 1 is >= ["2"], resulting in [0]
                   #  STACK: [[0]]
           n       # Square it
                   #  STACK: [[0]]
            f      # Get a list of all prime factors (none for 0), which results in []
                   #  STACK: [[[]]]
             g     # Pop and push its length
                   #  STACK: [1]
              T    # Push builtin 10
                   #  STACK: [1,10]
               g   # Pop and push its length
                   #  STACK: [1,2]
                  # Discard it
                   #  STACK: [1]
                 V # Pop and store it in variable `Y`
                   #  STACK: []

From here on out I can't really explain it anymore, since it does things I wasn't expecting:

ι                  # Uninterleave (would take either one or two arguments, but since the
                   # stack is empty, it somehow remembered the 1 that was previously on
                   # the stack and results in ["1"] - 
                   # A program `ι` without input would result in an error instead..)
                   #  STACK: [["1"]]
 i                 # If-statement, which will be entered if the top is 1;
                   # since it's ["1"] instead of 1, it won't enter
                   #  STACK: []
  bWι`Ta₂FgeaZNPT₃nFlfgX`!bhg!ceag₂$₃.pp
                   #  No-ops within the if-statement
                   # It again somehow remembers the previous ["1"] that was on the stack,
                   # which is output implicitly as result

Answered by Kevin Cruijssen on October 27, 2021

Io, 6 bytes

1print

Try it online!

Commentator, (Shifted -17)

 _aX]c

Try it online!

Answered by user92069 on October 27, 2021

brainfuck, 3 bytes

Outputs the character with the codepoint 1. This is allowed by default.

(+.

Try it online!

Shifted +3

+.1

Try it online!

Explanation

The + character increments the current item of the tape, and . outputs that value as a character. All other characters are ignored.

Answered by user92069 on October 27, 2021

Keg, 1 byte (SBCS)

1

Try it online!

Implicitly outputs 1

Shifted +198

?

Try it online!

Uses the push'n'print to print 1

Answered by lyxal on October 27, 2021

J, 3 2 bytes

=0

Monadic = means self-classify. It compares each item with each other item to see if it's the same. 0 is 0. It returns 1.

Shifted +1

>1

Unboxes 1, which does nothing, because it wasn't in a box in the first place.

Alternate 2-byters:

!1 (1 factorial) shifted by 2 is #3 (amount of items in 3)

!0 (0 factorial) shifted by 2 is #2 (amount of items in 2) shifted by 7 is *9 (sign of 9)

Answered by PkmnQ on October 27, 2021

Whitespace, 2 x 18 bytes

" " " ␋ ␌
␋   ␌
" ␋ 

Try it online..

All codepoints decreased by 2 will result in:

 ␟ ␟ ␟  ␇
␈   ␇
␈ ␟ ␇

Try it online.

Both programs contain unprintables. The first program contains characters with the codepoints: [34,32,34,32,34,32,11,9,12,10,11,9,12,10,34,32,11,9]. The second program with codepoints: [32,30,32,30,32,30,9,7,10,8,9,7,10,8,32,30,9,7]. In Whitespace, all characters except for spaces (codepoint 32), tabs (codepoint 9), and newlines (codepoint 10) are ignored, so both programs are actually the following:

SSSTN
TN
ST

Where S, T, and N are spaces, tabs, and newlines respectively.

This program will:

  • SSSTN: Push 1
  • TNST: Print it as integer to STDOUT

It's actually possible to create 3 x 27 bytes, 4 x 36 bytes, and even 5 x 45 bytes programs by having the codepoints apart by 2, still resulting in the same basic program above after all non-whitespace characters are ignored.

Answered by Kevin Cruijssen on October 27, 2021

!@#$%^&*()_+, 4 bytes

1@/>

Try it online!

Explanation

1    # Pushes 1
 @   # Prints top of the stack (1)
  /> # Pushes some meaningless stuff

Shifted +2

3B1@

Try it online!

Explanation

3B   # Pushes some meaningless stuff
  1  # Pushes 1
   @ # Prints top of the stack (1)

Answered by PkmnQ on October 27, 2021

Pyth, 2 bytes

s1

Try it online!

Pyth, 2 bytes, Shifted +1

t2

Try it online!


First Program translates to floor(1)
Second Program translates to 2 - 1

Answered by Mukundan314 on October 27, 2021

APL (Dyalog Unicode), 2x2 bytes

*0
+1

Try it online!

*0 computes e^0, and +1 computes complex conjugate of 1. *0 has Unicode codepoint 42 and 48, and +1 has 43 and 49, so the two are different by exactly one.

Also works in many different flavors of APL, including... (copied from Adám's APL bounty)

Dyalog APL Classic/Unicode/Extended/Prime, APL2, APL+, APLSE, GNU/APL, Sharp APL, sAPL, SAX, NARS, APLX, A+, dzaima/APL, ngn/APL, APLiv, Watcom APL, or APL360.

... which makes this a polyglot of at least 19 languages!

Answered by Bubbler on October 27, 2021

Japt, 1 byte

Among many others:

1

Test it

Ä

Test it

l

Test it

Answered by Shaggy on October 27, 2021

05AB1E, 2x 1 byte

Without an input, any of these single characters will output 1, so just pick two you like. :)

  • 1 (self explanatory): Try it online.
  • X (variable, which is 1 by default): Try it online.
  • (!= 1 check; without input it will do "" != 1, resulting in truthy/1): Try it online.
  • @ (>= check; without input it will do "" >= "", resulting in truthy/1): Try it online.
  • Q (== check; without input it will do "" == "", resulting in truthy/1): Try it online.

Answered by Kevin Cruijssen on October 27, 2021

05AB1E, 3 bytes

1*1

(Works in Japt too.)

Try it online!

Japt, 3 bytes

6/6

Try it online!

Derived from the 05AB1E program by shifting by 5 Unicode codepoints.

The Japt program performs division, but don't be fooled into thinking that the 05AB1E program is performing multiplication. The * (square) operator acts only on the first 1; the output is actually just an implicit print of the second 1.

The same idea works with the 05AB1E/Japt program pairs 1-1 and 3/3 (shift 2) and 1+1 and 5/5 (shift 4).

Answered by Dingus on October 27, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP