9
\$\begingroup\$

;# is a very simple language. It has 2 commands:

  • ; will increment the accumulator
  • # outputs the accumulator modulo 127, and then resets the accumulator

;$ is also very simple and is very similar (and also does not yet exist). It has 2 commands:

  • ; will increment the accumulator
  • $ outputs the accumulator modulo 127. However, it does not reset the accumulator.

Challenge

Create a program that will convert ;# code to ;$ code. That is, given input a using standard methods, output he shortest b such that a in ;# outputs the same thing as b in ;$. The input will only contain the characters ';' and '#'. The input will not have trailing semicolons. That is, it will match the regex (;*#)+. This also means input will not be blank. Note that ;;;;## is possible (equivalent to ord(4) ord(0)).

Examples

;# code -> ;$ code
;;;;;;;;;;#;;;;;;;;;;;;# -> ;;;;;;;;;;$;;$
;;;;;;;;;;;;;;#;;;# -> ;;;;;;;;;;;;;;$;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;$
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#;;;;;;;;;;;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;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;$;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;$;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;$ (Hello, World!)
\$\endgroup\$
3
  • 1
    \$\begingroup\$ I'm downvoting this, because there are too many ;# questions going around. They're not very interesting. \$\endgroup\$
    – isaacg
    May 25, 2017 at 3:06
  • \$\begingroup\$ @isaacg Ah. Thanks for your input. I'll try to keep my challenges more original next time though do note that this was one of the earlier ;# challenges; it was the first one after the compiler and the transpiler IIRC. \$\endgroup\$
    – hyper-neutrino
    May 25, 2017 at 3:08
  • \$\begingroup\$ Is the ;# input shortest one? \$\endgroup\$
    – l4m2
    Apr 13, 2018 at 19:46

7 Answers 7

3
\$\begingroup\$

Jelly, 18 bytes

ṣ”#L€ṙ-I%127”;ẋp”$

Try it online!

How it works

ṣ”#L€ṙ-I%127”;ẋp”$  Main link. Argument: s (string)

ṣ”#L€               Split s at hashes. Take the length of each resulting chunk.
     ṙ-             Rotate the result -1 units to the left / 1 unit to the right.
                    Since s won't have trailing semicola, the length of the last
                    chunk will be 0. Rotating moves this 0 to the beginning.
       I            Increments; take all forward differences. Because of the 0 we
                    removed to the beginning, the first forward difference will be
                    the length of the first chunk.
        %127        Take the differences modulo 127. In Python, `n % d' is either 0
                    or has the same sign as d, so this reports how many semicola
                    are needed to get from one code point to the next one.
            ”;ẋ     Repeat ';' k times, for each modulus k.
               p”$  Take the Cartesian product with "$", appending '$' to each run
                    of semicola.
\$\endgroup\$
2
\$\begingroup\$

JavaScript (ES6), 68 bytes

s=>s.replace(/;*#/g,s=>';'.repe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;;;;;;;;;;;;;;;;;;;;;#;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#"))

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

PHP, 99 bytes

while(~$c=a^$argn[$p++])j^$c?r^$c?:$a=-$b*$b=print str_repeat(";",(127+$a%127)%127)."$":$b+=!!++$a;
\$\endgroup\$
1
\$\begingroup\$

J, 37 bytes

[:;';$'<@#~1,.~127|2-~/\0,=&'#'#;._2]

Try it online!

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

Python, 101 100 97 85 bytes

1 bytes saved thanks to @WheatWizard

def t(c,s=0):
 for x in map(len,c.split('#')[:-1]):print(end=';'*((x-s)%127)+'$');s=x
\$\endgroup\$
2
  • \$\begingroup\$ You can drop the space between ) and for \$\endgroup\$
    – Wheat Wizard
    May 23, 2017 at 22:19
  • \$\begingroup\$ @WheatWizard thanks \$\endgroup\$
    – Uriel
    May 23, 2017 at 22:22
0
\$\begingroup\$

Retina, 52 bytes

^
##
#
127$*;$
^;+\$;+\$|(;+)(?<=\$\1(;{127})?\$\1)

Try it online! Includes test suite. Explanation: 127 is added to each # to make the subtraction modulo 127 easier; the #s are changed to $ at the same time. The subtraction itself is handled by the lookbehind (?<=\$\1(;{127})?\$\1) which ensures that we subtract either the number of ;s between the previous two #s (which handles the case when there are more ;s) or 127 fewer than that number (which handles the case when there are fewer ;s and they need to wrap past 127). A lookbehind is used so that all the replacements can be calculated in one pass. In order for there always to be two previous #s, two extra #s are temporarily prefixed.

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

Perl 5 -p, 65 bytes

s/(;*)#/$b=($q+$b)%127;';'x($q+=(($q=length$1)<$b)*127-$b).'$'/ge

Try it online!

\$\endgroup\$

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.