60
\$\begingroup\$

Oreoorererereoo

Given an input string that is similar to the word "oreo", give an ASCII representation of the cookie that is as wide as the input string (to ensure cookie stability).

Rules

  • The input is lowercase, a non-empty string with no whitespace containing any combination of the strings "o" and "re", and containing only those strings.
  • The string "o" represents the solid cookie, while the string "re" represents the filling.
  • The output must be a stacked cookie that is as wide as the input string.
  • The output may not be an array of strings
  • The cookie must overlap the filling by one character on each side
  • The characters used for the output don't have to match the output below (█ and ░), they just have to be different non-whitespace characters for the two parts of the cookie
  • The whitespace padding on the left side of the filling is required, and any trailing whitespace is optional

Examples

Input: oreo
Output:
████
 ░░ 
████

Input: o
Output:
█

Input: re
Output: (two spaces)


Input: rere
Output:
 ░░ 
 ░░ 

Input: oreoorererereoo
Output:
███████████████
 ░░░░░░░░░░░░░ 
███████████████
███████████████
 ░░░░░░░░░░░░░ 
 ░░░░░░░░░░░░░ 
 ░░░░░░░░░░░░░ 
 ░░░░░░░░░░░░░ 
███████████████
███████████████

Since this is code golf the shortest answer wins, good luck :)

\$\endgroup\$
  • 3
    \$\begingroup\$ "The whitespace padding on each side of the filling is required". Does this actually mean that there must be a space character at the end of each line of filling? If so why? As long as it works visually then what does this requirement add to the challenge? \$\endgroup\$ – ElPedro Jan 6 at 0:23
  • \$\begingroup\$ @ ElPedro Good point, I modified the rules and @Dennis I edited the rules so the comments should be okay to clean up \$\endgroup\$ – GammaGames Jan 7 at 3:22
  • \$\begingroup\$ @JonathanAllan Since it's printing "ascii-art" I removed that rule, it looks like I forgot to update the question. Should be updated now. \$\endgroup\$ – GammaGames Jan 7 at 16:05
  • \$\begingroup\$ Awesome, thanks! \$\endgroup\$ – Jonathan Allan Jan 7 at 16:18
  • \$\begingroup\$ @GammaGames, if whitespace on the right is not required anymore, I assume the output for test case re should be now acceptable as 1 or 2 spaces, not necessarily 2? \$\endgroup\$ – Kirill L. Jan 10 at 8:24

44 Answers 44

2
\$\begingroup\$

C (gcc), -D$=putchar(33 + 88 = 102 bytes

O(char*r){for(char*e,*o=r,x;*r;$-x),$-23))for(x=*r++>111,e=x?$-1),r++,o+1:o;*++e;$+x));}

Try it online!

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

Sed, 89 characters

(86 characters code + 3 characters command line option)

h;s/./E/g;H;g;s/\n//;:b;s/(o|re)([ore]+)(E*)/\1\3\n\2\3/;tb;s/o//g;s/reE(E*)E/ \L\1 /g

Commented with example for input “oreo”:

h        # copy to hold space
s/./E/g  # change into cookie: EEEE
H        # append to hold space
g        # copy back to pattern space: oreo␤EEEE
s/\n//   # remove the newline: oreoEEEE

:b                                 # split the os and res in separate lines
  s/(o|re)([ore]+)(E*)/\1\3\n\2\3/ # repeating the EEEE for each:
tb                                 # oEEEE␤reEEEE␤oEEEE

s/o//g                # remove the os: EEEE␤reEEEE␤EEEE
s/reE(E*)E/ \L\1 /g   # remove the res, cut edge, change fill: EEEE␤ ee ␤EEEE

Sample run:

bash-4.4$ sed -r 'h;s/./E/g;H;g;s/\n//;:b;s/(o|re)([ore]+)(E*)/\1\3\n\2\3/;tb;s/o//g;s/reE(E*)E/ \L\1 /g' <<< oreoorererereoo
EEEEEEEEEEEEEEE
 eeeeeeeeeeeee 
EEEEEEEEEEEEEEE
EEEEEEEEEEEEEEE
 eeeeeeeeeeeee 
 eeeeeeeeeeeee 
 eeeeeeeeeeeee 
 eeeeeeeeeeeee 
EEEEEEEEEEEEEEE
EEEEEEEEEEEEEEE

Try it online!

\$\endgroup\$
  • \$\begingroup\$ I didn't expect an entry like that, creative! \$\endgroup\$ – GammaGames Jan 11 at 15:04
  • \$\begingroup\$ Same byte count but with newlines - Try it online! \$\endgroup\$ – Logern Jan 11 at 18:52
  • \$\begingroup\$ @Logern, thanks for playing with it. This brought to my attention that the TIO code was still the first version, which didn't handled input “re” correctly. \$\endgroup\$ – manatwork Jan 11 at 19:02
2
\$\begingroup\$

APL (Dyalog Extended), 22 bytesSBCS

Anonymous tacit prefix function. Cookies are o, cream is r.

'^rr'⎕R' '⍤1≢⊢⍤/⍪~'e'⍨

Try it online!

 swap the input for:

'e' this letter

~ remove all the "e"s from:

 the columnised argument

⊢⍤/ replicate that single column to be as many columns as:

 the tally of characters in the input

⍤1 on each row (lit. on each 1D sub-array):

'^rr'⎕R' ' PCRE Replace leading "rr" with a single space

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

Brainfuck, 276 268 bytes

Technicaly as the brainfuck only has 8 instructions, that means it needs only 3 bits per character instead of 8 (that also means we could encode brainfuck in base64 with exactly 2 instructions per character), but I'll play fair and say it's 268 bytes long.

I was afraid of using more cells to set up the values to be displayed, so it can probably be shortened, at least I used one to get a shorter solution.

It was a fun challenge, thanks!

Edit: now featuring the 3rd rule!

Edit: moving the line feed from the datapool to the display part of the code allowed me to get down to 268 bytes.

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

Here's a more readable version:

The datapool is made like so: [first character, placeholder value used to move the input character count, repeated character, last character]

>>+<,[//start of the loop
<++++++++++[>-----------<-]>-//"o"
[>-<,>>+<<[-]//if it's not a "o", skip one character and:

>>>>[>]//go to the data pool and search for the end
>++++[<++++++++>-]//put " " in memory
+>//put 1 in memory
>++++++++++[<+++++++++++>-]<+>//put a "o" in memory
>++++[<++++++++>-]<//put another " " in memory
[<]<<<//get back to the beginning of the data pool and get back to the program

]>[[-]//else (so if it's a "o"):

>>>[>]//go to the data pool and search for the end
>+++++++[<+++++>-]//put a "#" in memory
+>//put 1 in memory
>+++++++[<+++++>-]//put a "#" in memory
>+++++++[<+++++>-]<//put a "#" in memory
[<]<<//get back to the beginning of the data pool and get back to the program

]+<,>>+<<]//loop until the end of the input line and count the number of characters

>>//go to the number of characters variable
--//substract 2 to it because the first and last characters take one space each
[>>>+<<<-]//copy the value to the next place
>>[.>//start of the loop and display of the first character
-[>>>>+<<<<->.<]//display of the repeated character while copying the character count to the next line
>>.//display of the last character
[-]++++++++++.//display a line feed
>]//loop until the end of the data pool

it works on copy.sh/brainfuck with default settings (link)

\$\endgroup\$
  • \$\begingroup\$ Fun language, nice entry! :) \$\endgroup\$ – GammaGames Jul 22 at 3:59
  • 1
    \$\begingroup\$ Oops, it looks like it breaks the third rule! The output isn't as wide as the input :s \$\endgroup\$ – GammaGames Jul 22 at 14:50
  • \$\begingroup\$ ah sorry, I didn't read that one, thanks I'm gonna work on that \$\endgroup\$ – Xaetral Jul 22 at 20:54
  • \$\begingroup\$ alright the only case where the result is not the expected one is "o" "re" outputs 2 spaces and "" outputs nothing but "o" is not doable with the method I chose because the repeated character should be negative here \$\endgroup\$ – Xaetral Jul 23 at 10:19
1
\$\begingroup\$

SNOBOL4 (CSNOBOL4), 136 bytes

	S =INPUT
	N =SIZE(S)
S	S ('o' | 're') . X REM . S	:F(END)
	OUTPUT =IDENT('o',X) DUPL(X,N)	:S(S)
	OUTPUT =' ' DUPL(0,N - 2) ' '	:(S)
END

Try it online!

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

CJam, 27 bytes

q:T'e-{i2%T,'9*_:)2>S\+?N}/

Try it online!

Explanation:

q                            read input:                    | "oreoo"
 :T                          store in variable T            | "oreoo", T="oreoo"
   'e-                       remove all 'e' characters:     | "oroo"
      {                  }/  for each character:            | 'o
       i                       get Unicode value:           | 79
        2%                     modulo 2:                    | 1
          T                    push original input:         | 1 "oreoo"
           ,                   get length:                  | 1 5
            '9*                repeat '9' that many times:  | 1 "99999"
               _               duplicate:                   | 1 "99999" "99999"
                :)             increment every character:   | 1 "99999" ":::::"
                  2>           remove first two characters: | 1 "99999" ":::"
                    S\+        prepend a space:             | 1 "99999" " :::"
                       ?       conditional:                 | "99999"
                        N      add a newline:               | "99999" N
                             (implicit output)              | "99999" N " :::" N "99999" N "99999" N
\$\endgroup\$
  • \$\begingroup\$ This breaks the rules: "The whitespace padding on each side of the filling is required" \$\endgroup\$ – NieDzejkob Jan 6 at 15:19
  • \$\begingroup\$ Sorry, I allowed the optional trailing whitespaces. Nice entry! \$\endgroup\$ – GammaGames Jan 7 at 3:27
1
\$\begingroup\$

Lua, 112 bytes

s=io.read()for t in s:gmatch"."do io.write(t=="o"and t:rep(#s).."\n"or(t=="r"and" "..t:rep(#s-2).." \n"or""))end

Explanation

s = io.read() -- Reads the input
for t in s:gmatch(".") do -- For each character in the input, call it 't' and enter the loop
 io.write(t=="o" and t:rep(#s).."\n" or (t=="r" and " "..t:rep(#s-2).." \n" or ""))

          t=="o" and  -- if 't' is equal to "o" then
                     t:rep(#s).."\n"  -- you have to write 't' repeated N times, where N is
                                      -- is the size of the input s, appended by a new line
                                     or (t=="r" and  -- else, if 't' is equal to "r" then
                                                    " "..t:rep(#s-2).." \n"  -- write a space, followed by 't' repeated N-2 times,
                                                                             -- where N is the size of the input.
                                                                             -- followed by a space and a new line.
                                                                            or "" -- else, just don't write anything.

end

Try it online!

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

JavaScript, 70 bytes

x=>x.replace(/()()o|re/g,(_,o=1,s=' ')=>s.padEnd(x.length-o,+o)+s+`
`)

Try it online!

Or... If we're allowed to use other characters for whitespace, we can get it down to 65 bytes:

x=>x.replace(/()()o|re/g,(_,o=1,s=' ')=>s.padEnd(x.length-o,+o)+s+`
`)

Try it online!

...but I don't think that counts.

\$\endgroup\$
  • \$\begingroup\$ You're right, the padding has to be whitespace. Nice entry! \$\endgroup\$ – GammaGames Jan 7 at 3:24
1
\$\begingroup\$

JavaScript (Node.js), 65 bytes

s=>s[R='replace'](/.e?/g,x=>s[R](/./g,x[0])[R](/^r|r$/g,' ')+`
`)

Try it online!

Thanks to Yair Rand, saves 2 bytes.

\$\endgroup\$
  • \$\begingroup\$ You can save a byte by moving the last replace a bit earlier and removing the m flag: s=>s[R='replace'](/re|o/g,x=>s[R](/./g,x[0])[R](/^r|r$/g,' ')+ ) \$\endgroup\$ – Yair Rand Jan 7 at 4:09
  • \$\begingroup\$ And another byte by using /.e?/g instead of /re|o/g . \$\endgroup\$ – Yair Rand Jan 7 at 4:50
  • \$\begingroup\$ @YairRand Thanks. \$\endgroup\$ – tsh Jan 7 at 5:40
1
\$\begingroup\$

Python 2, 64 bytes

s=input()
for c in s:
	if'n'<c:j='o'<c;print' '*j+c*(len(s)-2*j)

A full program which prints the result.

Try it online! Or see the test-suite.

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

Batch, 133 bytes

@set/ps=
@set o=%s:r=o%
@set o=%o:e=o%
@set r=%o:o=-%
@set r= %r:~2%
@set s=%s:e= %
@for %%c in (%s:o=o %)do @call echo %%%%c%%

Takes input on STDIN. Explanation:

@set/ps=

Input the string.

@set o=%s:r=o%
@set o=%o:e=o%

Make a copy of the string with all of the rs and es replaced with os.

@set r=%o:o=-%
@set r= %r:~2%

Make a copy of the string of os, change them to -s and replace the first two with a space.

@set s=%s:e= %
@for %%c in (%s:o=o %)do @call echo %%%%c%%

Change the es into spaces and also add a space after each o, allowing the os and rs to be iterated over, printing the appropriate string each time.

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

Python 2, 70 75 bytes

lambda s:"\n".join((c*(len(s)-2*(c<'f'))).center(len(s))for c in s if'r'>c)

Try it online!

Uses 'o' for cookie, 'e' for filling. Returns a string.

EDIT: Saved 4 bytes thanks to Jo King.

Explanation:

# anonymous function, returns a string seperated by line breaks
lambda s:"\n".join(.......................................................)
                                                        # iterate through string, return each non-'r' character
                                                        for c in s if'r'>c
                    # duplicate the character until it is the length of the original string,
                    # minus two characters if the current letter is 'e'
                    c*(len(s)-2*(c<'f'))
                   # pad the string with spaces out to the length of the original string
                   (....................).center(len(s))
\$\endgroup\$
  • \$\begingroup\$ Sorry, the output should be a single string with line-breaks that can be printed or the program should print the output as it is processed. Arrays of strings aren't allowed, because of the "ascii" part. \$\endgroup\$ – GammaGames Jan 7 at 19:13
  • \$\begingroup\$ @GammaGames Bluh, I should really read the rules more closely. Thank you! \$\endgroup\$ – Triggernometry Jan 7 at 20:22
  • \$\begingroup\$ 66 bytes \$\endgroup\$ – Jo King Jan 8 at 3:32
1
\$\begingroup\$

Tcl, 216 bytes

set s [gets stdin]
set l [string map {o { o } re { re }} $s]
set n [string length $s]
foreach i $l {
if {$i eq "o"} {
puts [string repeat █ $n]
} elseif {$i eq "re"} {
puts " [string repeat ░ [expr {$n-2}]] "
}
}

Try it online!

\$\endgroup\$
  • 1
    \$\begingroup\$ Assume valid input and handle “o” and “r” and ignore “e”. For this split $s "" is shorter than string map. You only need $l once, so better skip the assignment to variable and use the expression directly in foreach. If you reverse the conditions, you can remove the spaces: $i eq "o""o"eq$i. Use if instead of elseif. no need the braces around expr's argument. Try it online! \$\endgroup\$ – manatwork Jan 9 at 19:26
1
\$\begingroup\$

Stax, 13 bytes

äNëY┬2-]‼Cv6╤

Run and debug it

Uses "o" and "r" characters for the layers.

\$\endgroup\$
  • \$\begingroup\$ Oh man, you tied the current lowest! Really cool language too, I appreciate stack-based languages. \$\endgroup\$ – GammaGames Jul 24 at 14:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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