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

\$\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\$
    – Leo
    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\$
    – frarugi87
    May 22, 2017 at 9:29
  • 1
    \$\begingroup\$ @iamnotmaynard Semicolon Hash \$\endgroup\$ May 23, 2017 at 15:09
  • 5
    \$\begingroup\$ Maybe Wink Hash would be easier to say \$\endgroup\$ May 23, 2017 at 17:42
  • 1
    \$\begingroup\$ The Pastebin link appears to be dead. \$\endgroup\$
    – EasyasPi
    May 22, 2021 at 4:29

139 Answers 139

2
\$\begingroup\$

Stax, 12 bytes

î╟°♪ƒ▌○«*G<╠

Run and debug it

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

APL, 41 bytes/chars

{⎕AV[⍋⎕NXLATE 0][127|≢¨⌽⊂⍨⌽2=⍵(⊢⍳∩)';#']}

Explanation:

vector of ASCII characters

{⎕AV[⍋⎕NXLATE 0]                          }

select elements from that vector

{                 [                         ]}

keep only characters from the stream belonging to the set ';#'

{                 [               ⍵(  ∩)';#']}

find position of each character of the stream in the set ';#'

{                 [               ⍵(⊢⍳ )';#']}

0 where there's ';' and 1 where there's '#'

{                 [             2=          ]}

reverse the stream, partition beginning at every 1, reverse back; note that trailing ';'s get discarded this way

{                 [       ⌽⊂⍨⌽            ]}

length of each partition modulo 127

{                 [127|≢¨                   ]}
\$\endgroup\$
1
2
\$\begingroup\$

Haskell, 70 bytes

(-7 thanks to BWO)

n#(o:p)|o==';'=(n+1)#p|o=='#'=toEnum(mod n 127):0#p|1>0=n#p
_#p=p
(0#)

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ Huh, I always thought you had to use a space to call a function. \$\endgroup\$
    – mrFoobles
    Aug 12, 2018 at 16:00
  • 2
    \$\begingroup\$ n#(o:p)|o=='#'=toEnum(mod n 127):0#p|o/=';'=n#p|w<-n+1=w#p works for 69 bytes. \$\endgroup\$
    – ovs
    Aug 12, 2018 at 17:22
2
\$\begingroup\$

JavaScript (V8), 77 bytes

p=>([...p].map(e=>e==';'?i++:(s+=String.fromCharCode(i%127),i=0),i=0,s=''),s)

Try it online!

JavaScript (V8), 77 bytes

p=>([...p].map(e=>i+=e==';'?1:(s+=String.fromCharCode(i%127),-i),i=0,s=''),s)

Try it online!

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

Keg, -ir, -lp, 18 bytes

0&(¦\;⑹|\#⑻\ø%,0⑾™

Try it online!

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

VBScript, 109 bytes

a=0
For i=0 to len(s)
x=Mid(s,i,1)
if x=";" then a=a+1
if x="#" then wscript.stdout.write chr(a mod 127)
next
\$\endgroup\$
2
\$\begingroup\$

GNU AWK, 43 bytes

BEGIN{FS=";";RS="#"}RT{printf"%c",NF-1%127}

Try it online!

A different approach to this answer, golfing 14 bytes. Works only on GNU AWK (gawk).

BEGIN{FS=";";RS="#"}    ; are fields' and # are records' separators

RT{                     This skips the last ; sequence without a #
   printf"%c",NF-1%127  Prints the number of fields (i.e., semicolons) minus 1,
  }                     converted to ASCII, of course
\$\endgroup\$
2
  • 1
    \$\begingroup\$ As RT is GNU Awk extension, there isn't much reason anyway in testing with other Awk implementation. \$\endgroup\$
    – manatwork
    May 7, 2021 at 1:28
  • \$\begingroup\$ @manatwork yep, you are right. Thanks for pointing that out. \$\endgroup\$ May 7, 2021 at 14:12
2
\$\begingroup\$

C (gcc), 72 67 bytes

-5 thanks to @ceilingcat

a;main(i){for(;i=getchar()-10;a=i-49?i-25?a:0*putchar(a%127):a+1);}

Try it online!

Took longer than I should have for this one.

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

Lua, 95 bytes

s=...n=0load(s:gsub('[^#;]',''):gsub('.',{[';']='n=n+1',['#']='io.write(s.char(n%127))n=0'}))()

Try it online!

Removes all non # or ; characters, replaces the remaining characters with their respective Lua instructions, and then runs it.

\$\endgroup\$
1
\$\begingroup\$

C, 74 72 70 bytes

a;f(char*s){for(;*s;s++){if(*s==59)a++;if(*s==35)putchar(a%127),a=0;}}

Test cases:

Output: Hello, World!
f(";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#;;;;;;;;;;;;;;;;;;;;;;;;;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;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;###ffh#h#;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;ffea;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;aa;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#au###h;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;h;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;o");

Output: !
f(";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#");
\$\endgroup\$
1
\$\begingroup\$

shortC, 51 bytes

a;f(C*s){O;*s;s++){F*s==59)a++;F*s==35)Pa%127),a=0;
\$\endgroup\$
1
\$\begingroup\$

J, 29 bytes

[:u:127|[:+/"1[:';'&=;.1'#',]

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ The third test case outputs 2┴d{I, but I think it might be because of J's alphabet a. \$\endgroup\$
    – miles
    May 23, 2017 at 2:33
1
\$\begingroup\$

C, 56 54 bytes

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

Solution is based on Dennis's one ("C (gcc), 58 bytes"), but with elimination of two characters a=

Two more symbols can be taken off with substitution putchar with putch

\$\endgroup\$
2
  • 1
    \$\begingroup\$ a= is required to make the function reusable; since a is global, it has to be reset to 0. \$\endgroup\$
    – Dennis
    May 23, 2017 at 4:08
  • \$\begingroup\$ putch is not standard C. \$\endgroup\$
    – MD XF
    May 23, 2017 at 21:19
1
\$\begingroup\$

Cheddar, 57 bytes

@.split("#").head(-1).map(s->@"(s.count(";")%127)).join()

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Aceto, 38 31 bytes

The program reads and processes one character at a time, so it will work with infinite ;# programs. Run with -F if you want immediate results (because buffering), but it works without it if the program is long enough.

  Ox%-12
  =_cpF7
d'#'O
,;=|xIO

, reads a character. We duplicate it and compare it with ;. If it's equal, we mirror to the right side (=|), drop (x) the duplicate, Increment the value on the stack (implicitly zero), and go back to the Origin.

If it's not equal, we compare the copy with #. If that is equal, we mirror upwards (=_; somewhere in empty space above, but the next non-nop we read will be the 2). We push a 2 and a 7, compute 2^7 (F), and substract 1 (1-). We then do a modulo operation (%) convert it to a character, print it (without a newline) and go back to the Origin.

If it's none of those characters, we drop the value on the stack (x) and go back to the Origin.

\$\endgroup\$
1
\$\begingroup\$

Gema, 63 characters

\A=@set{a;0}
\;=@incr{a}
\#=@int-char{@mod{$a;127}}@set{a;0}
?=

Sample run:

bash-4.4$ gema '\A=@set{a;0};\;=@incr{a};\#=@int-char{@mod{$a;127}}@set{a;0};?=' < '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
\$\endgroup\$
1
  • \$\begingroup\$ I do so love Gema answers. \$\endgroup\$
    – Jordan
    May 24, 2017 at 2:01
1
\$\begingroup\$

PHP, 61 Bytes

for(;~$c=a^$argn[$i++];)$n+=$c^r?$c==Z:-$n*print chr($n%127);

Try it online!

PHP, 68 Bytes

for(;a&$c=$argn[$i++];)$c!=";"?$c!="#"?:$n=1-print chr($n%127):++$n;

Try it online!

\$\endgroup\$
4
  • 2
    \$\begingroup\$ Doesn't work with 0. Fixed and slightly golfed: for(;~$c=a^$argn[$i++];)$c^r?$n+=$c==Z:print~$n=~chr($n%127);. \$\endgroup\$
    – user63956
    May 22, 2017 at 6:10
  • \$\begingroup\$ @user63956 It tooks a long time to understand how this works. Nice Thank You \$\endgroup\$ May 22, 2017 at 10:21
  • 1
    \$\begingroup\$ Yields warnings A non-numeric value encountered in PHP 7.1. Use $n+=$c^r?$c==Z:-$n*print chr($n%127); (same length) to fix. \$\endgroup\$
    – Titus
    May 22, 2017 at 13:47
  • \$\begingroup\$ @Titus Done Thank You \$\endgroup\$ May 22, 2017 at 14:05
1
\$\begingroup\$

Clojure, 71 bytes

#(for[i(re-seq #"[^#]*#"%)](print(char(mod(count(filter #{\;}i))127))))

Splits into chunks by regex, counts how many ;s there are in each chunk.

\$\endgroup\$
1
\$\begingroup\$

Haxe, 145 136 bytes

function(s){var a=0;for(i in 0...s.length){var c=s.charAt(i);if(c==';')a++;else if(c=='#'){Sys.print(String.fromCharCode(a%127));a=0;}}}

-9 bytes thanks to Haxe's "inline function" (lambda) feature

Don't think I've ever seen an answer given in Haxe before, probably because it's not very golfy, and not that well known.

The code's really quite self-explanatory, here's the un-golfed version:

function(s) {
    var a=0;
    for(i in 0...s.length) {
        var c=s.charAt(i);
        if(c==';') {
            a++;
        }
        else if(c=='#') {
            Sys.print(String.fromCharCode(a%127));
            a=0;
        }
    }
}
\$\endgroup\$
1
\$\begingroup\$

Rust, 109 bytes

Rust was not created for golfing...

fn f(s:&str){let mut a=0u8;for c in s.chars(){if c==';'{a+=1;}if c=='#'{a%=127;print!("{}",a as char);a=0;}}}
\$\endgroup\$
1
\$\begingroup\$

jq, 54 characters

(49 characters code + 5 characters command line options)

[scan(".*?#")|gsub("[^;]";"")|length%127]|implode

Sample run:

bash-4.4$ jq -Rrj '[scan(".*?#")|gsub("[^;]";"")|length%127]|implode' < '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

On-line test

\$\endgroup\$
1
\$\begingroup\$

Mathematica, 81 bytes

StringReplace[a=0;#,{";":>(a++;""),"#":>FromCharacterCode@Mod[a,a=0;127],_:>""}]&

Pure function expecting a string argument.

Explanation

The basic evaluation procedure (it can be more complicated) for an expression in Mathematica is to evaluate the head of the epxression, then evaluate each of its arguments in order, then apply any definitions, then evaluate the entire expression again until nothing changes.

So first we evaluate StringReplace (no-op). The first argument is a=0;#, which sets the accumulator a to 0 and returns the first argument (denoted #) to the function. The second argument is a list of string replacement rules, which are applied in order starting at the beginning of the string.

If we encounter the substring ";", we replace it with (a++;""). Since we used :> instead of ->, this replacement expression is only evaluated whenever we actually find a match for ";". It (post-)increments a and returns the empty string, so finding a match for ";" effectively increments the accumulator and deletes the semicolon. In the case of a match, we move to the next position in the string, otherwise we check the next rule in the list.

If we encounter the substring "#", we replace it with FromCharacterCode@Mod[a,a=0;127]. FromCharacterCode does what you expect, but Mod[a,a=0;127] can be tricky if you don't understand Mathematica's evaluation procedure. We evaluate Mod (no-op), then evaluate the first argument a to get the value of the accumulator. When we evaluate the second argument a=0;127, we reset the accumulator to 0 and return 127. Thus we end up with Mod[<a>,127], where <a> is whatever the value of a was before the expression started evaluating. The only way I can think of to get this to work by resetting the accumulator after calculating the modulus without introducing additional variables is something like "#":>Reap[Sow[FromCharacterCode[Mod[a,127]]];a=0][[2,1,1]]. Introducing another variable I could do something like "#":>(b=a;a=0;FromCharacterCode[b~Mod~127]).

If we reach the last replacement rule, we replace any single character _ with the empty string "".

\$\endgroup\$
1
1
\$\begingroup\$

Rip, 33 chars

0gDiW[D';EI[Pi0]'#EI[27pdMo0]gDi]

Got around to testing this, and this works with the given examples.

\$\endgroup\$
1
\$\begingroup\$

Proton, 66 bytes

a=>''.join(map(len+((%)&127)+chr,a.rstrip(';').split('\#'))[to-1])

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ This is the one of the first challenges you found to answer? :P \$\endgroup\$ Aug 17, 2017 at 19:45
  • \$\begingroup\$ @totallyhuman hey why not :P \$\endgroup\$
    – hyper-neutrino
    Aug 17, 2017 at 19:57
1
\$\begingroup\$

Bash + coreutils + dc, 93 bytes

tr ';#' '69'|tr -dc '69'|dc -e '?0sa[10~rdZ1<P]dsPx[6=Ala127%P0sa]sR[la1+saq]sA[lRxz0<M]dsMx'

Try it online!

First time submitting a multi-utility answer, so if I botched that let me know - but just treating it as bash seems the least generous in terms of byte count anyway.

I thought this would be fun to do in dc, but obviously dc does not really care for strings, so the first thing to do was use tr to translate it into a new dialect, 69 which is just ;# only... well, sixes and nines. After doing that, we also use tr to delete everything that isn't a 6 or a 9 so that we don't have to account for errant characters in dc.

With a giant number input into dc, we can make an interpreter. 0sa clears the accumulator, a. [10~rdZ1<P]dsPx uses the divide-returning-both-quotient-and-remainder command, ~ to break our big number into a stack full of individual commands. Jump to the end, the main macro [lRxz0<M]dsMx executes macro R until the stack is empty. Macro R is [6=Ala127%P0sa]sR, which tests if the top-of-stack is a 6 and runs macro A if so. Macro A has a quit command in it, which essentially makes the rest of R an else statement. This does the mod 127, Print ASCII, and reset accumulator a to zero operations. Meanwhile, macro A is just a simple increment operation for the accumulator: [la1+saq]sA.

You can try just the dc portion online!

\$\endgroup\$
1
\$\begingroup\$

Coconut, 59 bytes

t->''.join(map(x->chr(x.count(';')%127),t.split('#')[:-1]))

Try it online!

\$\endgroup\$
1
\$\begingroup\$

brainfuck, 201 bytes

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

Try it online! As a note, this will take 60 seconds to finish because the program will never halt, so it will stop after the Tio 60 second timeout.

How it works

This works differently than the given algorithm. Instead of using mod it just wraps to 0 after counting 127 semicolons.

+[                              'while true
    >>,                         'read input
    [>+>+<<-]                   
    >>>----[>+<----]>----       
    [-<<->>]+<<[>>-<<[-]]>>     'if(input == ';')
    [                       
        -<<<[-]<<+              'increment counter 
        [->>+>+<<<]
        >>>>>--[<+>--]<
        [-<->]  
        <[                      'if(counter == 127) counter = 0
            [-]<[->>+<<]>
        ]
        [-]<[-]                 
        >>[-<<<<+>>>>]>
    ]
    <<<[                        'if(input != 59)
        >--[>--<+++++++]>-      
        [<+>-]          
        <[-<->]     
        <[                      'if(input == '#')
            [-]-                
        ]
        +[                      
            [-]                 
            <<.[-]>>            'print counter as ascii and clear tape
        ]
    ]
    <<<         
]
\$\endgroup\$
1
\$\begingroup\$

Julia 1.0, 90 62 bytes

i(s)=print.(Char.(count.(==(';'),split(s,"#")[1:end-1]).%127))

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Powershell, 67 bytes

See also Joey's answer

-join($args-split'#|[^#]*$'|%{[char]((($_-split';').Count-1)%127)})

Test script:

$f = {

    -join($args-split'#|[^#]*$'|%{[char]((($_-split';').Count-1)%127)})

}

$c00 = [char]0x0000
$c23 = [char]0x0017

@(
    ,($c00,'#')
    ,('',';;;;;;')
    ,("2$($c23)d$c00$c00$c00$c00{$c00$c00$($c00)",';;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;hafh;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;f;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;###ffh#h#;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;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;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#')
    ,(';#', ';;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#')
    ,('!', ';;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#')
) | %{
    $e,$s = $_
    $r = &$f $s
    "$($e-eq$r): $r"
}

Output:

True:
True:
True: 2d    {
True: Hello, World!
True: ;#
True: !

Explanation:

-join(                                   #
    $args-split'#|[^#]*$'|%{             # extract strings tailed by # symbol from arguments
        [char](                          #
            (($_-split';').Count-1)%127  # count ; symbol in each extracted string, get remain by 127
        )                                # convert int to char
    }                                    #
)                                        # join all chars
\$\endgroup\$
1
\$\begingroup\$

C# 7572 + 18 = 9390 Bytes

using System.Linq;
s=>string.Join("",s.Split('#').Select(p=>(char)(p.Count(c=>c==59)%127)))

-3 Bytes thanks to @Charlie

Ungolfed full program with test cases

using System.Linq;

class P
{
    static void Main()
    {
        System.Func<string, string> f =
            s => string.Join("",                  //6. Combine results with empty string as separator
                s.Split('#')                      //1. Split the input by #
                    .Select(                      //2. For each part
                        p => (char)(              //5. Convert to character
                            p.Count(c => c == 59) //3. Count of all ;
                            % 127)                //4. Mod 127
                    ));

        System.Console.WriteLine(f(";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;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;;;;;;;;;;;;;;;;;;;;;aa;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#au###h;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;h;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;o"));
        System.Console.WriteLine(f(";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#"));
    }    
}
\$\endgroup\$
3
  • \$\begingroup\$ I've just come up with this same answer by myself, only to see that you posted it before. You can save 3 bytes using Count instead of Sum. Note that the last ; does not count. \$\endgroup\$
    – Charlie
    Sep 6, 2018 at 9:13
  • \$\begingroup\$ s.Split('#') gives an extra letter in the second example. Note that last substring has not # at end. \$\endgroup\$
    – mazzy
    Sep 6, 2018 at 13:12
  • \$\begingroup\$ I made the same solution and then I found yours. You can save 6 bytes if you won't join the string, but create an array of chars with ToArray(). \$\endgroup\$ Jan 23, 2020 at 10:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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