19
\$\begingroup\$

Make me a s'more! I tell you the width, the amount of graham cracker, the amount of chocolate, and the amount of marshmallow. An example:

Input:

Width: 10 Graham: 3 Chocolate: 2 Marshmallow: 1.

Output:

GGGGGGGGGG
GGGGGGGGGG
GGGGGGGGGG
CCCCCCCCCC
CCCCCCCCCC
MMMMMMMMMM
GGGGGGGGGG
GGGGGGGGGG
GGGGGGGGGG

Is it that easy? Um... yes.

Note that the input should be a list of arguments to a function or a program, not a string. You might choose the first being Width, then Graham, but any order is fine.

Full test cases if you are interested.

Stack snippet (for testing, etc.)

This is to test the output.

var smore = function(width, graham, chocolate, marshmallow){
	return ("G".repeat(width) + "\n").repeat(graham) + 
	("C".repeat(width) + "\n").repeat(chocolate) + 
	("M".repeat(width) + "\n").repeat(marshmallow) + 
	("G".repeat(width) + "\n").repeat(graham);
};
Snippetify(smore);
<script src="https://programmer5000.com/snippetify.min.js"></script>
Width: <input type = "number">
Graham: <input type = "number">
Chocolate: <input type = "number">
Marshmallow: <input type = "number">
<button>Try it out!</button>
<pre data-output></pre>

Notes:

  • You may include a trailing newline on the end of the last line. You may also use a \ instead of a newline.
  • This is .
  • Any questions? Comment below:
\$\endgroup\$
10
  • 21
    \$\begingroup\$ I edited out your Let Me Google That For You link. It really wasn't funny. \$\endgroup\$ Commented Apr 17, 2017 at 13:47
  • 1
    \$\begingroup\$ @FelipeNardiBatista yes. \$\endgroup\$
    – user58826
    Commented Apr 17, 2017 at 14:30
  • 1
    \$\begingroup\$ Some answers are assuming flexible input order and format (as usual in PPCG), but the challenge seems to require a specific order and rule out strings (not sure what that means). Can you clarify? \$\endgroup\$
    – Luis Mendo
    Commented Apr 17, 2017 at 14:34
  • 2
    \$\begingroup\$ Thanks for clarifying. You should then reword the sentence the input should be a list of arguments to a function or a program, not a string, with the first being Width, then Graham, etc. Personally I would say something like "Input format is flexible as usual" \$\endgroup\$
    – Luis Mendo
    Commented Apr 17, 2017 at 14:36
  • 4
    \$\begingroup\$ @programmer5000 but why? If they downvoted, it's 90% likely it's because they think it's a boring and trivial challenge. Furthermore, it's quite rude to tell people to explain or retract. They have the right to downvote without comment. \$\endgroup\$
    – Riker
    Commented Apr 17, 2017 at 16:12

43 Answers 43

1
2
1
\$\begingroup\$

R, 69 bytes

function(n,u)cat(rep(strrep(c("G","C","M","G"),n),c(u,u[1])),sep="
")

Try it online!

Trying this strrep function I just heard about... Probably a intToUtf8 approach could do better than that. Thanks @Shaggy for pointing an issue...fixing it saved 6 bytes.

\$\endgroup\$
4
  • 1
    \$\begingroup\$ There's only one layer of chocolate. \$\endgroup\$
    – Shaggy
    Commented Jul 11, 2018 at 14:14
  • \$\begingroup\$ @Shaggy you just saved me 6 bytes, ty! \$\endgroup\$
    – JayCe
    Commented Jul 11, 2018 at 14:22
  • \$\begingroup\$ @dzaima it takes a width n and a vector of heights u which you can input as c(g,c,m), like this \$\endgroup\$
    – Giuseppe
    Commented Jul 11, 2018 at 14:44
  • \$\begingroup\$ @Giuseppe oh. Really shouldn't ask questions about languages I don't know :p \$\endgroup\$
    – dzaima
    Commented Jul 11, 2018 at 14:48
1
\$\begingroup\$

Java 11, 226 171 132 bytes

(w,g,c,m)->{x(w,"G",g);x(w,"C",c);x(w,"M",m);x(w,"G",g);};void x(int w,String c,int x){for(;x-->0;)System.out.println(c.repeat(w));}

-55 bytes thanks to @ceilingcat.

Try it online.

Explanation:

(w,g,c,m)->{            // Main method with four integer parameters and String return-type
  x(w,"G",g);           //  Print all Graham rows
  x(w,"C",c);           //  Print all Chocolate rows
  x(w,"M",m);           //  Print all Marshmallon rows
  x(w,"G",g);}          //  Print all Graham rows again

void x(int w,String c,int x){
                        // Separated method with two integer & character parameters and
                        // String return-type
  for(;x-->0;)          //  Loop `x` amount of times:
    System.out.println( //   Print with trailing newline:
      c                 //    The given character `c`
       .repeat(w));}    //    repeated `w` amount of times
\$\endgroup\$
3
  • 1
    \$\begingroup\$ Not bad...for java! \$\endgroup\$
    – user58826
    Commented Apr 18, 2017 at 14:27
  • 1
    \$\begingroup\$ @programmer5000 Hehe, thanks! I like golfing in Java 7 (and sometimes 8), although I don't think it will ever even compete with other answers.. The only time 'somewhat competed' with a Java answer was with this 8 byte answer and this 19 byte answer, actually outgolfing Python for the first time. ;p Although those golfing languages with their 1 or 2 byte submissions still leave Java in the dust of course. \$\endgroup\$ Commented Apr 18, 2017 at 14:32
  • \$\begingroup\$ @ceilingcat Thanks! This is a pretty old answer, so going from Java 7 to Java 11 saves a bunch more bytes. ;) \$\endgroup\$ Commented Jan 29, 2020 at 9:09
1
\$\begingroup\$

Vyxal, 11 bytes

`GCM`f*εǏf⁋

Try it Online!

`GCM`f      # "GCM" as a char list
      *     # repeat each by width
       ε    # repeat each by input size amounts
        Ǐ   # Append the first item
         f  # Flatten
          ⁋ # Join by newlines
\$\endgroup\$
1
\$\begingroup\$

J, 14 bytes

#&>'GCMG'#~4&$

Attempt This Online!

Takes input as 10 f 3 2 1 for the example test case.

#&>'GCMG'#~4&$
           4&$    Cycle the right side to length 4; [3 2 1 3]
   'GCMG'#~       Repeat each char of GCMG that many times; 'GGGCCMGGG'
#&>               Repeat each char `left side` times and stack them into a matrix
\$\endgroup\$
0
\$\begingroup\$

Mathematica 102 Bytes (100 Characters)

Heard the s'mores built-in isn't coming out til V12.

s=StringRepeat;StringReplace[s@@@({Characters@"GCMG",#/.#[[4]]->#[[1]]})<>"",x_:>x~s~#[[4]]<>"\n"]&

Pretty straightforward using the idea of building up a column first. Long function names waste 35 bytes. The one box-looking symbol is actually a transpose character and will paste into Mathematica just fine.

Usage: %@{Graham, Chocolate, Marshmallows, Width} e.g. %@{3, 2, 1, 11}

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

Haskell, 91 bytes

import Data.List
(#)=replicate
f w g c m=intercalate"\n"$map(w#)$g#'G'++c#'C'++m#'M'++g#'G'

Should be pretty self-explanatory. Since it was noted in a comment that character matrices are allowed, here's a 58 byte version that returns a list of strings (one for each layer):

(#)=replicate
f w g c m=map(w#)$g#'G'++c#'C'++m#'M'++g#'G'
\$\endgroup\$
0
\$\begingroup\$

R, 117 114 bytes

f=function(w,g,h,m){s=strrep;f=function(a,b)s(paste0(s(a,w),'\n'),b);G=f("G",g);cat(G,f("C",h),f("M",m),G,sep='')}

Explanation:

function(w,g,h,m){              #width, graham, chocolate, marshmallow
  s=strrep                      #repeats strings (for each layer)
  f=function(a,b){              #for a given layer
    s(paste0(s(a,w),'\n'),b)    #repeat the corresponding letter w times, then repeat that + a newline the corresponding number of times
  }
  G=f("G",g)
  cat(                        #print to console:
      G,                 #so repeat "G" w times, over g lines
      f("C",h),                 #etc.
      f("M",m),
      G,sep='')        #with no separation
}

Saved 3 bytes thanks to @Giuseppe!

\$\endgroup\$
3
  • \$\begingroup\$ This can be shortened to 97 bytes with a different approach: function(w,g,s,m)matrix(c(rep("G",w*g),rep("S",w*s),rep("M",w*m),rep("G",w*g)),g*2+s+m,w,byrow=T) \$\endgroup\$
    – count
    Commented Apr 19, 2017 at 7:25
  • \$\begingroup\$ That seems to output a matrix, which doesn't quite look like what the OP specified. \$\endgroup\$
    – BLT
    Commented Apr 19, 2017 at 12:52
  • \$\begingroup\$ Removing the brackets throws an error, but great catch on the 3 bytes! \$\endgroup\$
    – BLT
    Commented Apr 19, 2017 at 20:59
0
\$\begingroup\$

Japt, 30 26 bytes

"GCMG"¬mpU £XÄ pVgY)q1Ãm·q

Try it online!

30-byte solution:

Y=VÆ'GpU÷[YWÆ'CpU÷XÆ'MpUÃY]·

Try it online!

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

T-SQL, 259 bytes

CREATE PROC S @ INT,@g INT,@c INT,@m INT,@b INT=0 AS SET @B=@G G:PRINT REPLICATE('G',@)SET @G-=1IF @G>0 GOTO G C:PRINT REPLICATE('C',@)SET @C-=1IF @C>0GOTO C M:PRINT REPLICATE('M',@)SET @M-=1IF @M>0GOTO M Q:PRINT REPLICATE('G',@)SET @B-=1IF @B>0 GOTO Q RETURN

This stored procedure loops for the first layer or graham, chocolate, and Marshmallow with a GOTO in order to print up to the established amount. For the last layer of graham the fourth optional parameter (previously setted to the @g value) is used to do the same as the first layer of graham. The width of the layer is created with the REPLICATE function.

Usage:

EXECUTE S @ = 5, --Width
    @g = 7, --Graham
    @c = 8, --Chocolate
    @m = 4, --Marshmallow
    @b = NULL --Control variable, can be ignored
\$\endgroup\$
0
\$\begingroup\$

Charcoal, 23 bytes

↷²Nγ×⟦⁺⁺⁺×Gγ×CN×MN×Gγ⟧N

Try it online!

Input is in the form graham, chocolate, marshmallow, width

Explanation

↷²                          Pivot right by 2 * 45 degrees
  Nγ                       Input number to g
      ⟦⁺⁺⁺×Gγ×CN×MN×Gγ⟧   "G" * g + "C" * input number + "M" * input number + "G" * g
     ×                   N Multiplied by N

Lists are implicitly printed joined by newlines.

\$\endgroup\$
1
  • \$\begingroup\$ I think you can save a byte by inputting ×GN to γ and another byte by using instead of ↷². (Note that the existing code no longer works because × now vectorises but you can concatenate a newline for the same byte count.) \$\endgroup\$
    – Neil
    Commented Dec 27, 2017 at 21:08
0
\$\begingroup\$

SOGL V0.12, 11 bytes

GCM”{e*.∙Q≥

Try it Here!

Explanation:

GCM”         push "GCM"
    {        for each
     e*        multiply horizontally by e, which, when first called, gets the numerical value of the next input
       .∙      multiply vertically by the nest input
         Q     output without popping and without disabling implicit output
          ≥    put the top item of the stack at the bottom as to keep the first item on the top
\$\endgroup\$
2
  • \$\begingroup\$ wasn' sogl v0.12 released after this challange? \$\endgroup\$ Commented Oct 9, 2017 at 13:44
  • 1
    \$\begingroup\$ @FelipeNardiBatista Yes, but that's now allowed. \$\endgroup\$
    – dzaima
    Commented Oct 9, 2017 at 13:56
0
\$\begingroup\$

Japt -R, 16 bytes

"GCMG"ËpU*VgEÃòU

Try it


Explanation

                     :Implicit input of integer U=Width & array V=[Graham,Chocolate,Marshmallow]
"GCMG"               :String literal
      Ë              :Map each character at index E
       p             :  Repeat
        U*           :   U multiplied by
          VgE        :   Get the integer at index E in V (Yay, index wrapping!)
             Ã       :End mapping
              òU     :Split to an array of strings of length U
                     :Implicitly join with newlines and output
\$\endgroup\$
0
\$\begingroup\$

05AB1E, 14 12 bytes

…GCMS×ĆIĆÅΓ»

Try it online.

Explanation:

…GCM          # Push string "GCM"
    S         # Convert it to a list of characters: ["G","C","M"]
     ×        # Repeat each the (implicit) input-integer `w` amount of times as string
              #  i.e. w=4 → ["GGGG","CCCC","MMMM"]
      Ć       # Enclose this list; appending its own head as trailing item
              #  → ["GGGG","CCCC","MMMM","GGGG"]
       IĆ     # Push the second input-list, and enclose it as well
              #  i.e. [3,2,1] → [3,2,1,3]
         ÅΓ   # Run-length decode, repeating the strings the integers amount of times
              #  → ["GGGG","GGGG","GGGG","CCCC","CCCC","MMMM","GGGG","GGGG","GGGG"]
           »  # And join this list by newlines
              #  → "GGGG\nGGGG\nGGGG\nCCCC\nCCCC\nMMMM\nGGGG\nGGGG\nGGGG"
              # (after which the result is output implicitly)
\$\endgroup\$
1
2

Your Answer

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