74
\$\begingroup\$

I recently created a new language called ;# (pronounced "Semicolon Hash") which only has two commands:

; add one to the accumulator

# modulo the accumulator by 127, convert to ASCII character and output without a newline. After this, reset the accumulator to 0. Yes, 127 is correct.

Any other character is ignored. It has no effect on the accumulator and should do nothing.

Your task is to create an interpreter for this powerful language!

It should be either a full program or a function that will take a ;# program as input and produce the correct output.

Examples

Output: Hello, World!
Program: ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;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au###h;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;h;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;o
Output: Fizz Buzz output
Program: link below
Output: !
Program: ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#

Fizz Buzz up to 100

Philippos
2,6881 gold badge13 silver badges40 bronze badges
asked May 21, 2017 at 18:34
\$\endgroup\$
11
  • 1
    \$\begingroup\$ Is it acceptable if an interpreter doesn't terminate its execution at the end of the input but instead keeps looping indefinitely without producing extra output? \$\endgroup\$ Commented May 22, 2017 at 9:26
  • 6
    \$\begingroup\$ The second example makes me wonder about a program to encode a program to produce an output... recursive compilation! \$\endgroup\$ Commented May 22, 2017 at 9:29
  • 1
    \$\begingroup\$ @iamnotmaynard Semicolon Hash \$\endgroup\$ Commented May 23, 2017 at 15:09
  • 5
    \$\begingroup\$ Maybe Wink Hash would be easier to say \$\endgroup\$ Commented May 23, 2017 at 17:42
  • 2
    \$\begingroup\$ The Pastebin link appears to be dead. \$\endgroup\$ Commented May 22, 2021 at 4:29

141 Answers 141

1
2 3 4 5
25
\$\begingroup\$

Python 3, (削除) 69 (削除ここまで) 68 bytes

-1 byte thanks to @WheatWizard

i=0
for c in input():
 i+=c==';'
 if'#'==c:print(end=chr(i%127));i=0

Try it online!

answered May 21, 2017 at 19:24
\$\endgroup\$
1
  • 4
    \$\begingroup\$ You can save a byte by reversing your if. Try it online! \$\endgroup\$ Commented May 21, 2017 at 23:30
20
\$\begingroup\$

JavaScript (ES6), (削除) 76 (削除ここまで) (削除) 82 (削除ここまで) 80 bytes

s=>s.replace(/./g,c=>c=='#'?String.fromCharCode(a%(a=127)):(a+=(c==';'),''),a=0)

Demo

let f =
s=>s.replace(/./g,c=>c=='#'?String.fromCharCode(a%(a=127)):(a+=(c==';'),''),a=0)
console.log(JSON.stringify(f(";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;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;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;h;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;o")))
console.log(JSON.stringify(f(";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#")))

Recursive version, (削除) 82 (削除ここまで) 77 bytes

Saved 5 bytes thanks to Neil

This one is likely to crash for large inputs such as the Fizz Buzz example.

f=([c,...s],a=0)=>c?c=='#'?String.fromCharCode(a%127)+f(s):f(s,a+(c==';')):""
answered May 21, 2017 at 19:39
\$\endgroup\$
3
  • \$\begingroup\$ I think f(s,a+(c==';')) might knock three bytes off your recursive version. \$\endgroup\$ Commented May 22, 2017 at 8:52
  • \$\begingroup\$ @Neil It actually saves 5 bytes. :-) \$\endgroup\$ Commented May 22, 2017 at 11:10
  • \$\begingroup\$ I feel really silly now. I originally had a buggy version, and subtracted 2 bytes to fix the bug. But I had miscounted and the buggy version actually saved 7 bytes... \$\endgroup\$ Commented May 22, 2017 at 11:23
14
\$\begingroup\$

Java 8, 100 bytes

s->{int i=0;for(byte b:s.getBytes()){if(b==59)i++;if(b==35){System.out.print((char)(i%127));i=0;}}};

Try it online!

Okx
16.5k5 gold badges45 silver badges114 bronze badges
answered May 22, 2017 at 16:12
\$\endgroup\$
12
  • 3
    \$\begingroup\$ Welcome to the site! :) \$\endgroup\$ Commented May 22, 2017 at 16:25
  • \$\begingroup\$ I added a link to an online interpreter with the FizzBuzz example for you (link text was too long to fit in a comment) \$\endgroup\$ Commented May 22, 2017 at 20:26
  • \$\begingroup\$ Java uses UTF-16 for its programs. So, these aren't 100 bytes but 100 characters. \$\endgroup\$ Commented May 23, 2017 at 12:50
  • 5
    \$\begingroup\$ @GeroldBroser Unicode is a character set: UTF-8 and UTF-16 are two encodings of that character set. ASCII source is perfectly valid as a Java program, and I have plenty of Java source files encoded in ASCII (which is also valid UTF-8, hence also a Unicode encoding). \$\endgroup\$ Commented May 23, 2017 at 20:24
  • 1
    \$\begingroup\$ Entirely golfed, for 81 bytes as a Consumer<char[]>: s->{char i=0;for(int b:s){if(b==59)i++;if(b==35){System.out.print(i%=127);i=0;}}} \$\endgroup\$ Commented Aug 11, 2017 at 9:55
13
\$\begingroup\$

Japt, 18 bytes

®è'; %# d}'# ë ̄J

There's an unprintable \x7f char after %#. Test it online!

How it works

® è'; %# d}'# ë ̄ J
mZ{Zè'; %127 d}'# ë s0,J
 // Implicit: operate on input string
mZ{ }'# // Split the input at '#'s, and map each item Z to
 Zè'; // the number of semicolons in Z,
 %127 // mod 127,
 d // turned into a character.
m '# // Rejoin the list on '#'. At this point the Hello, World! example
 // would be "H#e#l#l#o#,# #W#o#r#l#d#!#" plus an null byte.
 ë // Take every other character. Eliminates the unnecessary '#'s. 
 ̄J // Slice off the trailing byte (could be anything if there are
 // semicolons after the last '#').
 // Implicit: output result of last expression
answered May 21, 2017 at 19:28
\$\endgroup\$
1
  • 2
    \$\begingroup\$ D'oh, should have checked the answers! Just spent some time on this only to find you'd beaten me to the punch. q'# ®è'; u# dì¯J also works for the same score. \$\endgroup\$ Commented May 22, 2017 at 10:35
13
\$\begingroup\$

Python, 65 bytes

This is a golf of this earlier answer.

lambda t:''.join(chr(x.count(';')%127)for x in t.split('#')[:-1])

Try it online! Python2

Try it online! Python3

Explanation

This is a pretty straightforward answer we determine how many ;s are between each # and print the chr mod 127. The only thing that might be a little bit strange is the [:-1]. We need to drop the last group because there will be no # after it.

For example

;;#;;;;#;;;;;#;;;

Will be split into

[';;',';;;;',';;;;;',';;;']

But we don't want the last ;;; because there is no # after it to print the value.

answered May 21, 2017 at 23:47
\$\endgroup\$
1
  • 1
    \$\begingroup\$ I was busy trying to get all tests in one TIO link. Was chr for chr except t and x. \$\endgroup\$ Commented May 21, 2017 at 23:54
13
\$\begingroup\$

Retina, (削除) 336 (削除ここまで) (削除) 63 (削除ここまで) (削除) 67 (削除ここまで) (削除) 65 (削除ここまで) (削除) 66 (削除ここまで) (削除) 62 (削除ここまで) 59 bytes

T`;#-ÿ` ̄_
;{127}|;+$
(^| ̄)
 ̄
+T`-~`_-`[^ ̄]
T\` ̄`

Try it online!

Readable version using hypothetical escape syntax:

T`;#\x01-ÿ`\x01 ̄_
;{127}|;+$
(^| ̄)\x01\x01
 ̄\x02
+T`\x01-~`_\x03-\x7f`[^\x01 ̄]\x01
T\` ̄`

Does not print NUL bytes, because TIO doesn't allow them in the source code. (削除) Also prints an extra newline at the end, but I guess it can't do otherwise. (削除ここまで) Trailing newline suppressed thanks to @Leo.

-273 (!) bytes thanks to @ETHproductions.

-2 bytes thanks to @ovs.

-3 bytes thanks to @Neil. Check out their wonderful 34-byte solution.

answered May 21, 2017 at 23:06
\$\endgroup\$
12
  • 1
    \$\begingroup\$ Oh my word. But can you not save a thousand bytes with +T`\x01-~`_\x03-\x7f`[^\x01¯]\x01? (including the unprintables as single chars, of course) \$\endgroup\$ Commented May 21, 2017 at 23:41
  • \$\begingroup\$ @ETHproductions Of course you can. Thank you! :) \$\endgroup\$ Commented May 21, 2017 at 23:51
  • 1
    \$\begingroup\$ Currently the last letter is always in the output, even if there is no trailing # in the input. You can fix it by changing your second stage to (;{127}|;+$) \$\endgroup\$ Commented May 22, 2017 at 5:10
  • 1
    \$\begingroup\$ Do you need the +` on the third line? As you remove the entire match, there should be nothing left to replace in the second iteration. \$\endgroup\$ Commented May 22, 2017 at 9:14
  • 1
    \$\begingroup\$ I think I can do this in 34 bytes: T`;#\x01-ÿ`\x80\x7F_ \x80+$ (empty line) \+T`\x7Fo`\x01-\x80_`\x80[^\x80] (using hexadecimal escapes to represent unprintables). Outputs \x7F instead of nulls. \$\endgroup\$ Commented May 22, 2017 at 9:45
13
\$\begingroup\$

Ruby, (削除) 41 (削除ここまで) (削除) 35 (削除ここまで) 34 characters

((削除) 40 (削除ここまで) (削除) 34 (削除ここまで) 33 characters code + 1 character command line option)

gsub(/.*?#/){putc$&.count ?;%127}

Thanks to:

  • Jordan for suggesting to use putc to not need explicit conversion with .chr (6 characters)
  • Kirill L. for finding the unnecessary parenthesis (1 character)

Sample run:

bash-4.4$ ruby -ne 'gsub(/.*?#/){putc$&.count ?;%127}' < '2d{.;#' | od -tad1
0000000 2 etb d nul nul nul nul { nul nul nul
 50 23 100 0 0 0 0 123 0 0 0
0000013

Try it online!

answered May 22, 2017 at 11:06
\$\endgroup\$
10
  • 1
    \$\begingroup\$ To my own surprise, you can actually drop parentheses after count to save a byte \$\endgroup\$ Commented Aug 10, 2018 at 11:35
  • 1
    \$\begingroup\$ Thank you, @Deadcode. BTW, gsub(/.*?#/){putc$&.count(?;)%127} is shorter. \$\endgroup\$ Commented Jan 23, 2020 at 13:46
  • 1
    \$\begingroup\$ 31 bytes \$\endgroup\$ Commented Nov 9, 2022 at 19:18
  • 1
    \$\begingroup\$ Try it online. The regex should use ; instead of . seems like to adhere to the requirements. Otherwise awesome answer \$\endgroup\$ Commented Nov 11, 2022 at 3:38
  • 1
    \$\begingroup\$ Oops, you are right, @south. However changing to ; only solves your test case, but will fail on a mixture of valid and invalid input. Try it online! So I revert to previous version. \$\endgroup\$ Commented Nov 14, 2022 at 0:14
10
\$\begingroup\$

Python 3, 69 Bytes

Improved, thanks to @Wheat Wizard, @Uriel

print(''.join(chr(s.count(';')%127)for s in input().split('#')[:-1]))
answered May 21, 2017 at 20:10
\$\endgroup\$
8
  • 3
    \$\begingroup\$ Welcome to Programming Puzzles and Code Golf! The objective here is to make the code as short as possible (in bytes), so you need to include the byte count in the header :). \$\endgroup\$ Commented May 21, 2017 at 20:13
  • \$\begingroup\$ Thanks for explaining, didn't know that. I'll work on it then. \$\endgroup\$ Commented May 21, 2017 at 20:15
  • 2
    \$\begingroup\$ You can remove the space after the :s. \$\endgroup\$ Commented May 21, 2017 at 21:02
  • 1
    \$\begingroup\$ I count 74 bytes. tio.run/nexus/… \$\endgroup\$ Commented May 21, 2017 at 22:34
  • 2
    \$\begingroup\$ Also, ';'==c saves a space, but not using if statements at all would be even shorter. \$\endgroup\$ Commented May 21, 2017 at 22:39
9
\$\begingroup\$

><>, 35 bytes

>i:0(?;:'#'=?v';'=?0
^ [0o%'␡'l~<

Try it online! Replace with 0x7F, ^?, or "delete".

Main loop

>i:0(?;:'#'=?v 
^ <

This takes a character of input (i), checks if its less than zero i.e. EOF (:0() and terminates the program if it is (?;). Otherwise, check if the input is equal to # (:'#'=). If it is, branch down and restart the loop (?v ... ^ ... <).

Counter logic

 ';'=?0
 

Check if the input is equal to ; (';'=). If it is, push a 0. Otherwise, do nothing. This restarts the main loop.

Printing logic

> '#'=?v 
^ [0o%'␡'l~<

When the input character is #, pop the input off the stack (~), get the number of members on the stack (l), push 127 ('␡'), and take the modulus (%). Then, output it as a character (o) and start a new stack ([0). This "zeroes" out the counter. Then, the loop restarts.

answered May 22, 2017 at 0:07
\$\endgroup\$
1
  • 3
    \$\begingroup\$ Poor ><>. It's sad :0( :( \$\endgroup\$ Commented May 22, 2017 at 6:41
9
\$\begingroup\$

Röda, (削除) 44 (削除ここまで) (削除) 39 (削除ここまで) 38 bytes

5 bytes saved thanks to @fergusq

{(_/`#`)|{|d|d~="[^;]",""chr #d%127}_}

Try it online!

Anonymous function that takes the input from the stream.


If other characters do not have to be ignored, I get this:

Röda, 20 bytes

{(_/`#`)|chr #_%127}
answered May 21, 2017 at 18:58
\$\endgroup\$
0
8
\$\begingroup\$

Jelly, 13 bytes

ṣ"#Ṗċ€";%127Ọ

Try it online!

How it works

ṣ"#Ṗċ€";%127Ọ Main link. Argument: s (string)
ṣ"# Split s at hashes.
 Ṗ Pop; remove the last chunk.
 ċ€"; Count the semicola in each chunk.
 %127 Take the counts modulo 127.
 Ọ Unordinal; cast integers to characters.
answered May 21, 2017 at 20:56
\$\endgroup\$
4
  • 2
    \$\begingroup\$ The word semicola doesn't exist it's semicolons. \$\endgroup\$ Commented Jun 11, 2017 at 17:51
  • 3
    \$\begingroup\$ @EriktheOutgolfer en.m.wiktionary.org/wiki/semicola \$\endgroup\$ Commented Jun 11, 2017 at 17:52
  • \$\begingroup\$ Hmm, weird word. \$\endgroup\$ Commented Jun 11, 2017 at 17:53
  • \$\begingroup\$ @EriktheOutgolfer Someone on Wiktionary was probably trying to make the Latin plural valid in English, but cola and semicola spellings should be proscribed. \$\endgroup\$ Commented Sep 9, 2017 at 14:23
7
\$\begingroup\$

05AB1E, (削除) 16 (削除ここまで) (削除) 15 (削除ここまで) 14 bytes

Code:

'#¡ ̈ʒ';127ドル%ç?

Explanation:

'#¡ # Split on hashtags
 ̈ # Remove the last element
 ʒ # For each element (actually a hacky way, since this is a filter)
 ';¢ # Count the number of occurences of ';'
 127% # Modulo by 127
 ç # Convert to char
 ? # Pop and print without a newline

Uses the 05AB1E-encoding. Try it online!

answered May 21, 2017 at 19:35
\$\endgroup\$
7
\$\begingroup\$

x86 machine code on MS-DOS - 29 bytes

00000000 31 d2 b4 01 cd 21 73 01 c3 3c 3b 75 06 42 80 fa |1....!s..<;u.B..|
00000010 7f 74 ed 3c 23 75 eb b4 02 cd 21 eb e3 |.t.<#u....!..|
0000001d

Commented assembly:

bits 16
org 100h
start:
 xor dx,dx ; reset dx (used as accumulator)
readch:
 mov ah,1
 int 21h ; read character
 jnc semicolon
 ret ; quit if EOF
semicolon:
 cmp al,';' ; is it a semicolon?
 jne hash ; if not, skip to next check
 inc dx ; increment accumulator
 cmp dl,127 ; if we get to 127, reset it; this saves us the
 je start ; hassle to perform the modulo when handling #
hash:
 cmp al,'#' ; is it a hash?
 jne readch ; if not, skip back to character read
 mov ah,2 ; print dl (it was choosen as accumulator exactly
 int 21h ; because it's the easiest register to print)
 jmp start ; reset the accumulator and go on reading
answered May 22, 2017 at 22:15
\$\endgroup\$
7
\$\begingroup\$

Retina 0.8.2, (削除) 34 (削除ここまで) 32 bytes

T`;#\x00-\xFF`\x7F\x00_
\+T`\x7Eo`\x00-\x7F_`\x7F[^\x7F]|\x7F$

Try it online! Includes test case. Edit: Saved 2 bytes with some help from @MartinEnder. Now uses TIO link with null byte support thanks to @Deadcode. Note: Code includes unprintables, which I have replaced with hex escapes in the post. Explanation: The first line cleans up the input: ; is changed to \x7F, # to \x00 and everything else is deleted. Then whenever we see an \x7F that is not before another \x7F, we delete it and cyclically increment the code of any next character. This is iterated until there are no more \x7F characters left.

answered May 22, 2017 at 10:38
\$\endgroup\$
6
  • \$\begingroup\$ You can save a byte by combining the last two stages with \x80([^\x80]|$) in the last stage. \$\endgroup\$ Commented May 23, 2017 at 9:19
  • \$\begingroup\$ @MartinEnder Thanks! Annoyingly, \s+T`\x7Fo`\x01-\x80_`\x80(?!\x80).? also only saves one byte. \$\endgroup\$ Commented May 23, 2017 at 9:30
  • \$\begingroup\$ Ah, but [^\x80]|\x80$ saves two bytes, I think. \$\endgroup\$ Commented May 23, 2017 at 9:33
  • \$\begingroup\$ Ah nice, yeah the last one works. I had also tried the negative lookahead, but the s is annoying. \$\endgroup\$ Commented May 23, 2017 at 9:33
  • \$\begingroup\$ TIO does allow NULs, and the third test case works. \$\endgroup\$ Commented Jan 23, 2020 at 8:56
6
\$\begingroup\$

05AB1E, (削除) 25 (削除ここまで) (削除) 21 (削除ここまで) 19 bytes

-2 bytes thanks to Adnan

Îvy';Q+y'#Qi127%ç?0

Explanation:

Î Initialise stack with 0 and then push input
 v For each character
 y';Q+ If equal to ';', then increment top of stack
 y'#Qi If equal to '#', then
 127% Modulo top of stack with 127
 ç Convert to character
 ? Print without newline
 0 Push a 0 to initialise the stack for the next print

Try it online!

answered May 21, 2017 at 18:57
\$\endgroup\$
1
  • 1
    \$\begingroup\$ I think you can replace i>} by +. \$\endgroup\$ Commented May 21, 2017 at 19:58
6
\$\begingroup\$

R, (削除) 97 (削除ここまで) (削除) 90 (削除ここまで) (削除) 86 (削除ここまで) 84 bytes

A function:

function(s)for(i in utf8ToInt(s)){F=F+(i==59);if(i==35){cat(intToUtf8(F%%127));F=0}}

When R starts, F is defined as FALSE (numeric 0).

Ungolfed:

function (s)
 for (i in utf8ToInt(s)) {
 F = F + (i == 59)
 if (i == 35) {
 cat(intToUtf8(F%%127))
 F = 0
 }
 }
answered May 22, 2017 at 8:09
\$\endgroup\$
11
  • \$\begingroup\$ Shouldn't this be R+pryr? \$\endgroup\$ Commented May 22, 2017 at 8:31
  • \$\begingroup\$ @L3viathan Since pryr is an R package, it is still R code. \$\endgroup\$ Commented May 22, 2017 at 8:58
  • \$\begingroup\$ It is R code, but it requires the installation of an additional library. \$\endgroup\$ Commented May 22, 2017 at 9:05
  • \$\begingroup\$ @L3viathan Do you think my answer is invalid? Should I avoid using additional packages? \$\endgroup\$ Commented May 22, 2017 at 9:11
  • 2
    \$\begingroup\$ @BLT There is no difference. In my opinion it is no problem to use additional packages that were created before the challenge. This is true for all languages. In Python you have to use import while in R you can use :: to directly access function in packages. You can often see the use of additional packages here (e.g., for Python and Java). However, I changed my former post because I don't want to engage in discussion. \$\endgroup\$ Commented May 23, 2017 at 5:04
5
\$\begingroup\$

Plain TeX, 156 bytes

\newcount\a\def\;{\advance\a by 1\ifnum\a=127\a=0\fi}\def\#{\message{\the\a}\a=0}\catcode`;=13\catcode35=13\let;=\;\let#=\#\loop\read16 to\>\>\iftrue\repeat

Readable

\newcount\a
\def\;{
 \advance\a by 1
 \ifnum \a=127 \a=0 \fi
}
\def\#{
 \message{\the\a}
 \a=0
}
\catcode`;=13
\catcode35=13
\let;=\;
\let#=\#
\loop
 \read16 to \> \>
 \iftrue \repeat
answered May 21, 2017 at 22:08
\$\endgroup\$
1
  • \$\begingroup\$ Can it print characters symbolically? \$\endgroup\$ Commented May 21, 2017 at 23:20
5
\$\begingroup\$

Python, 82 bytes

lambda t:''.join(chr(len([g for g in x if g==';'])%127)for x in t.split('#')[:-1])
answered May 21, 2017 at 19:01
\$\endgroup\$
1
  • 1
    \$\begingroup\$ @WheatWizard since you already posted this as an answer, I believe the right action for me would be to upvote it rather than update \$\endgroup\$ Commented May 22, 2017 at 11:36
5
\$\begingroup\$

;#+, 59 bytes

;;;;;~+++++++>~;~++++:>*(~<:-+!(<-;->(;))::<+-::!(<#>)-:-*)

Try it online! Input is terminated with a null byte.

Explanation

The generation is the same as from my Generate ;# code answer. The only difference here is is the iteration.

Iteration

*(~<:-+!(<-;->(;))::<+-::!(<#>)-:-*)
*( *) take input while != 0
 ~ swap
 < read value from memory (;)
 : move forward to the accumulator memory spot (AMS)
 - flip Δ
 + subtract two accumulators into A
 ! flip A (0 -> 1, else -> 0)
 ( (;)) if A is nonzero, or, if A == ';'
 < read from AMS
 -;- increment
 > write to AMS
 :: move to cell 0 (#)
 < read value from memory (#)
 + subtract two accumulators into A
 - flip Δ
 :: move to AMS
 !( ) if A == '#'
 < read from AMS
 # output mod 127, and clear
 > write to AMS
 -:- move back to initial cell
caird coinheringaahing
50.9k11 gold badges133 silver badges364 bronze badges
answered May 25, 2017 at 22:04
\$\endgroup\$
0
4
\$\begingroup\$

C (gcc), 58 bytes

a;f(char*s){a+=*s^35?*s==59:-putchar(a%127);a=*s&&f(s+1);}

Try it online! (Hint: click ▼ Footer to collapse it.)

answered May 22, 2017 at 0:47
\$\endgroup\$
0
4
\$\begingroup\$

MATL, 29 bytes

';#'&mXz!"@o?T}vn127\c&YD]]vx

Input is a string enclosed in single quotes.

Try it online!

The FizzBuzz program is too long for the online interpreters; see it working offline in this gif:

enter image description here

Explanation

The accumulator value is implemented as the number of elements in the stack. This makes the program slower than if the accumulator value was a single number in the stack, it but saves a few bytes.

';#' % Push this string
&m % Input string (implicit). Pushes row vector array of the same size with 
 % entries 1, 2 or 0 for chars equal to ';', '#' or others, respectively
Xz % Remove zeros. Gives a column vector
! % Transpose into a row vector
" % For each entry
 @ % Push current entry
 o? % If odd
 T % Push true. This increases the accumulator (number of stack elements)
 } % Else
 v % Concatenate stack into a column vector
 n % Number of elements
 127\ % Modulo 127
 c % Convert to char
 &YD % Display immediately without newline
 ] % End
] % End
vx % Concatenate stack and delete. This avoids implicit display
answered May 21, 2017 at 20:30
\$\endgroup\$
0
4
\$\begingroup\$

Perl, 25 bytes

$_=chr(y%;%%%127)x/#/

Run with perl -043pe (counted as 4 bytes, since perl -e is standard).

Explanation: -043 sets the line-terminator to # (ASCII 043). -p iterates over the input "lines" (actually #-delimited strings, now). y%;%% counts the number of ; in each "line". x/#/ makes sure that we don’t print an extra character for programs that don’t end in a # (like the third testcase). %127 should be fairly obvious. $_= is the usual boilerplate.

answered May 22, 2017 at 14:45
\$\endgroup\$
3
  • \$\begingroup\$ Impressing one, though there is glitch: for ;;#;;; it outputs #5 instead of #2. \$\endgroup\$ Commented May 22, 2017 at 15:24
  • \$\begingroup\$ How did you get this result? echo -n ';;#;;;' | perl -043pe '$_=chr(y%;%%%127)x/#/' | xxd correctly outputs 00000000: 02 on my machine. If you left off the 043, or are using a codepage where # is not ASCII 043, that would explain your result. \$\endgroup\$ Commented May 22, 2017 at 15:27
  • 1
    \$\begingroup\$ Oops. Sorry, I had a typo in my test. Your code works perfectly. \$\endgroup\$ Commented May 22, 2017 at 15:32
4
\$\begingroup\$

CJam, 27 bytes

0q{";#"#") 127%co0 "S/=~}%;

Explanation:

0 e# Push 0
 q e# Push the input
 { e# For each character in the input:
 ";#"# e# Index of character in ";#", -1 if not found
 ") 127%co0 "S/ e# Push this string, split on spaces
 = e# Array access (-1 is the last element)
 ~ e# Execute as CJam code. ")" increments the accumulator,
 e# and "127%co0" preforms modulo by 127, converts to character, pops and outputs, and then pushes 0.
 }% e# End for
 ; e# Delete the accumulator

Alternative Solution, 18 bytes

q'#/);{';e=127%c}%

Explanation:

q e# Read the whole input
 '#/ e# Split on '#'
 ); e# Delete the last element
 { e# For each element:
 ';e= e# Count number of ';' in string
 127% e# Modulo by 127
 c e# Convert to character code
 }% e# End for
answered May 21, 2017 at 19:07
\$\endgroup\$
0
4
\$\begingroup\$

Brainfuck, 135 bytes

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

Ungolfed

Memory layout
0 1 2 3 4 5 6 7
1 f1 f2 in tmp acc d acc%d 
flag1 indicates hash
flag2 indicates semicolon
+[ infinite loop
 >+ set flag1
 >>, input
 >+++++[<------->-]< subtract 35 (hash)
 [ not hash
 <+> set flag2
 >++++[<------>-]< subtract 24 more (semicolon)
 [ not semicolon
 <-> clear flag2
 [-] clear input
 ]
 < goto flag2
 [ semicolon
 >>>+<<<- inc acc and clear flag2
 ]
 <- clear flag1
 >>[-] clear input
 ]
 << goto flag1
 [ hash
 >>>--[>>+<<--]> set d 127 and goto acc
 [>->+<[>]>[<+>-]<<[<]>-] mod
 >[-]> clear d and goto acc%d 
 .[-] print and clear result
 <<<<<<- clear flag1
 ]
<]
answered May 23, 2017 at 18:53
\$\endgroup\$
4
\$\begingroup\$

F#, (削除) 79 (削除ここまで) (削除) 91 (削除ここまで) 93 bytes

let rec r a=function|[]->()|';'::t->r(a+1)t|'#'::t->printf"%c"(char(a%127));r 0 t|_::y->r a y

Ungolfed

let rec run acc = function
 | [] -> ()
 | ';'::xs ->
 run (acc + 1) xs
 | '#'::xs ->
 printf "%c" (char(acc % 127))
 run 0 xs
 | x::xs -> run acc xs

Try it online!

Edit: Was treating any other char than ';' as '#'. Changed it so that it's ignoring invalid chars.

Alternative

F#, (削除) 107 (削除ここまで) 104 bytes

let r i=
 let a=ref 0
 [for c in i do c|>function|';'->a:=!a+1|'#'->printf"%c"(!a%127|>char);a:=0|_->()]

Use of reference cell saves 3 bytes

Ungolfed

let run i =
 let a = ref 0;
 [for c in i do
 match c with
 | ';' -> a := !a + 1
 | '#' ->
 printf "%c" (char(!a % 127))
 a := 0
 |_->()
 ]

Try it online

answered May 21, 2017 at 20:05
\$\endgroup\$
4
\$\begingroup\$

Processing.js (Khanacademy version), 118 bytes

var n="",a=0;for(var i=0;i<n.length;i++){if(n[i]===";"){a++;}if(n[i]==="#"){println(String.fromCharCode(a%127));a=0;}}

Try it online!

As the version of processing used does not have any input methods input is placed in n.

answered May 21, 2017 at 19:16
\$\endgroup\$
8
  • \$\begingroup\$ You could technically forge your own input method with keyTyped=function(){ ... } :P \$\endgroup\$ Commented May 21, 2017 at 19:18
  • \$\begingroup\$ @ETHproductions This is a look of disgust. \$\endgroup\$ Commented May 21, 2017 at 19:19
  • \$\begingroup\$ @RandomUser yay! I have done it! I like to answer in Processing (check my answers) \$\endgroup\$ Commented May 21, 2017 at 19:37
  • 2
    \$\begingroup\$ @RandomUser Not just 1000 rep.. but 2^10 rep ( ͡° ͜ʖ ͡°) \$\endgroup\$ Commented May 22, 2017 at 17:46
  • \$\begingroup\$ @Midnightas Ohhh yeah \$\endgroup\$ Commented May 22, 2017 at 18:03
4
\$\begingroup\$

Labyrinth, (削除) 61 (削除ここまで) 47 bytes

_36},)@
; {
; 42_-
"#- 1
_ ; 72
_ ) %
"""".

Try it online!

Explanation

color coded image of the solution code

Code execution begins in the top left corner and the first semicolon discards an implicit zero off the stack and continues to the right.

Orange

  • _36 pushes 36 onto the stack. This is for comparing the input with #
  • } moves the top of the stack to the secondary stack
  • , pushes the integer value of the character on the stack
  • ) increments the stack (if it's the end of the input, this will make the stack 0 and the flow of the program will proceed to the @ and exit)
  • { moves the top of the secondary stack to the top of the primary stack
  • - pop y, pop x, push x - y. This is for comparing the input with # (35 in ascii). If the input was # the code will continue to the purple section (because the top of the stack is 0 the IP continues in the direction it was moving before), otherwise it will continue to the green section.

Purple

  • 127 push 127 to the stack
  • % pop x, pop y, push x%y
  • . pop the top of the stack (the accumulator) and output as a character

From here the gray code takes us to the top left corner of the program with nothing on the stack.

Green

  • _24 push 24 onto the stack
  • - pop x, pop y, push x-y. 24 is the difference between # and ; so this checks if the input was ;. If it was ; the code continues straight towards the ). Otherwise it will turn to the # which pushes the height of the stack (always a positive number, forcing the program to turn right at the next intersection and miss the code which increments the accumulator)
  • ; discard the top of the stack
  • ) increment the top of the stack which is either an implicit zero or it is a previously incremented zero acting as the accumulator for output

From here the gray code takes us to the top left corner of the program with the stack with only the accumulator on it.

Gray

Quotes are no-ops, _ pushes a 0 to the stack, and ; discards the top of the stack. All of this is just code to force the control-flow in the right way and discarding anything extra from the top of the stack.

answered May 24, 2017 at 17:06
\$\endgroup\$
2
  • \$\begingroup\$ Out of curiosity, how did you generate the explanation image? Did you create it yourself? \$\endgroup\$ Commented Aug 5, 2018 at 9:16
  • 2
    \$\begingroup\$ @Stefnotch, I used a text editor to put a tab between each character and then pasted the code into Microsoft Excel which put each character into it's own cell. I selected all cells to give them equal width and height. Then I adjusted colors and borders and took a screenshot. \$\endgroup\$ Commented Aug 7, 2018 at 16:20
4
\$\begingroup\$

Awk, 57 bytes

BEGIN{RS="#"}RT=="#"{printf("%c",gsub(";","")%127)}

Explanation:

  • BEGIN{RS="#"}: make # record delimeter
  • RT=="#": ignore the record without ending #
  • gsub(";","")%127: count ; and mod 127
  • printf("%c",_): print as ASCII
answered Sep 18, 2018 at 4:30
\$\endgroup\$
4
\$\begingroup\$

Perl 5 with -043paF(?!^);, 17 bytes

Golfed down a bit by dom-hastings

$_=/#/&&chr@F%127

Try it online!

answered Apr 20, 2020 at 10:13
\$\endgroup\$
4
  • \$\begingroup\$ It seems you're missing a %127 \$\endgroup\$ Commented Apr 21, 2020 at 5:49
  • \$\begingroup\$ fair point, added it \$\endgroup\$ Commented Apr 21, 2020 at 5:54
  • \$\begingroup\$ There's a 25 bytes answer: here just FYI :) \$\endgroup\$ Commented Apr 21, 2020 at 12:18
  • 1
    \$\begingroup\$ Using that other one as a template and since flags count as a separate version of the language here's one for 17 bytes. \$\endgroup\$ Commented Apr 21, 2020 at 12:29
4
\$\begingroup\$

Vyxal, 10 / 20 bytes

There's two main schools of thought for interpreting ;#:

  • Go through every command and apply it (Iterative)
  • Split the program into strings of ;, then count and output. (Split 'n Count)

Here's both!


Iterative, 20 bytes

0$(\;n=ß›\#n=[7‹%C₴0

Try it Online!

Explanation:

 # Implicit input
0$ # Initialize accumulator at the bottom of the stack
 ( # For each command in the program
 \;n=ß # If command = ';':
 › # Increment accumulator
 \#n=[ # If command = '#':
 7‹% # Accumulator % 127
 C # Convert accumulator to character
 ₴ # Print accumulator w/o newline
 0 # Reset accumulator

Split 'n Count K, (削除) 14 (削除ここまで) (削除) 12 (削除ここまで) 10 bytes

-1 byte thanks to @caird coinheringaahing.

-1 byte by using Map Lambda instead of a for loop.

-2 bytes by using Keg mode and removing the n I left in from the for loop.

35€ƛ59O7‹%

Try it Online!

Output is a list of characters.

Explanation:

 # Implicit input; characters are converted to ordinal values
35€ # Split input program at '#'
 ƛ # For every section of program:
 59O # Count ';'
 7‹% # Modulus 127
 # 'K' flag - ordinal values are converted to characters
answered Apr 22, 2021 at 3:13
\$\endgroup\$
4
  • \$\begingroup\$ Would the "split on #, count ; for each, mod 127, remove last, to chars" approach be any shorter? See my Add++ answer, or Dennis' Jelly answer for a better explanation \$\endgroup\$ Commented Apr 22, 2021 at 13:15
  • \$\begingroup\$ @cairdcoinheringaahing As it turns out, yes! \$\endgroup\$ Commented Apr 22, 2021 at 14:45
  • \$\begingroup\$ Can you use O to count the number of semicola, rather than filter-length? \$\endgroup\$ Commented May 6, 2021 at 23:11
  • \$\begingroup\$ @cairdcoinheringaahing Not sure how I missed that, but yes! Thanks! Turns out, I can also replace the for loop with a map lambda to get a list, and concatenate with a flag to save another byte. \$\endgroup\$ Commented May 7, 2021 at 1:45
1
2 3 4 5

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.