The Doughnut Shop™

19

2

Being short of cash, you have signed up to build donuts for The Doughnut Shop™, the biggest digital doughnut company in the world, mostly because they sell every size of doughnut imaginable.

Now, given that trading standards nowadays is very tough, you need to write a piece of code as short as possible to create these doughnuts so that the source code that created them can be put on the outside of the packet.

Challenge

Given 4 inputs, radius of the outer ring, radius of the inner ring, the possible sprinkles and the chance of a cell having a sprinkle, output a doughnut covered in those sprinkles which has the correct inner and outer radii.

  • The input may be taken how you wish (arguments to a function, stdin, program arguments) and in any order.
    • The sprinkles will be given in the form of 1 character per sprinkle type
    • ^+*- as sprinkle input would be a list of 4 sprinkles, ^, +, *, -
    • The chance of a sprinkle will be entered as a floating point value between 0 and 1. eg: 0.1, 0.23
  • You must print out the output to stdout or equivalent.
  • Sprinkles can't be on the edges of the doughnut.
  • Each type of sprinkle must have an equally likely chance of being on each cell.
  • The radii are given in 1-cell units.
  • If the inner radius equals either 0 OR the outer radius, the doughnut is said to have no ring.
  • Both radii will be non-negative integers.
  • The inner and outer edges of the doughnut must be represented using hashes (#)
  • A test to see if a point is in a circle, given a radius and the center of the circle is:

    (x-center)**2+(y-center)**2 < radius**2

Example input with output

(outer radius, inner radius, sprinkles, chance of sprinkle)

  • 10, 4, "^+*-", 0.1

         #########
        #         #
      ##  ++   *  *##
      #             #
     #       ^^ - *  #
    #      #####   ^  #
    #+    #     #     #
    #    #       #-   #
    #    #       #  * #
    #    #       #+   #
    #    #       #    #
    #^  +#       #    #
    #     #     #     #
    # *    #####      #
     #       +  -    #
      #        ^    #
      ##  ^  +     ##
        #       ^ #
         #########
    
  • 5, 2, ":^+*", 0.9

      #####
     #^^+ ^#
    #**###  #
    #:#   #^#
    #^#   #*#
    #:#   #*#
    #:+###* #
     # *:^:#
      #####
    

This is code golf, the shortest answer in bytes wins

Blue

Posted 2015-09-25T10:47:33.820

Reputation: 26 661

Should there be an equal distribution of sparkles, or non uniform distribution will also do. – Kishan Kumar – 2015-09-25T16:04:28.710

There should be an equal distribution of sprinkles. – Blue – 2015-09-25T16:52:00.990

It's not clear to me from the spec which positions correspond to the borders of the circles. – Dennis – 2016-06-25T04:58:08.973

@Dennis I'd rather not change it and disqualify the only answer (that's a nice answer too) but I meant for a border to be where circle met non-circle (point is in circle but not all neighbors are) – Blue – 2016-06-25T19:21:00.690

Your example output pretty much invalidates it already, since the shapes for 10, 4 and 5, 2 are pretty different. I was going to leave a comment on the answer, but I realized that I didn't really understand how the output should look like for any dimensions but those in the examples. If you want to change your original idea match the output from the answer, that's up to you, but the challenge should clearly define how to draw the borders either way. – Dennis – 2016-06-25T20:30:36.843

Answers

2

MATLAB, 231 bytes

Here a matlab solution:

function g=z(r,q,s,p);[x,y]=meshgrid(1:2*r,1:2*r);d=(x-r).^2+(y-r).^2;h=size(d);e=zeros(h);e(d<r^2 & d>=q^2)=1;f=bwperim(e,4);k=rand(h);j=numel(s);l=changem(randi(j,h),s,1:j);g=char(e);g(:,:)=' ';g(k<=p)=l(k<=p);g(f)='#';g(~e)=' ';

Some examples:

>> z(10, 4, '^+*-', 0.1)

ans =

     #########      
    #         #     
  ##           ##   
  #    -       -#   
 #               #  
#   -  #####    ^ # 
#     #     #     # 
#   -#       #    # 
# *  #       #+   # 
#**  #       #    # 
#  * #       # -  # 
#+  *#       #    # 
#     #     #     # 
#      #####      # 
 #           ^   #  
  #     *       #   
  ##+          ##   
    #         #     
     #########      

>> z(5, 2, ':^+*', 0.9)

ans =

  #####   
 #++::*#  
#^^###++# 
# #   #+# 
#^#   #^# 
#*#   #*# 
#+:###^*# 
 #*:^+^#  
  #####   

>> z(20,6, 'erthhjjjjkjkk', 0.4)

ans =

             #############              
           ##jh  k  k  k  ##            
         ##  jjj    j khh   ##          
        #r kj h   k tjhj j    #         
      ##jk    t k  jh j       h##       
     #k       rre            k j #      
    # j   j j  j  khtkt jr     kj #     
    #  k   rk je    j      h   j  #     
   # j   k   k  jth e k j   j    j #    
  #h   h h e     t e ej  j  r k r e #   
  #    j   r  jh  jk     j  kk   j  #   
 #      k     k    h k  jk     k j   #  
 #  jjk   hh k hj  r  j  je rjj k j  #  
#  ek  j j jj  h#######          hke  # 
#hj      k j j #       #ke jhkt  jee  # 
#        jk  k#         # k    j   t  # 
#k        j  #           #khk  r     j# 
#   tj  j te #           # j  r j j   # 
#e   je   jhk#           #        t j # 
#jj    j  h  #           #     k jj e # 
# j j   hj j #           # jkt kjjjr e# 
#j k    e    #           #       r   k# 
#jj  k    ek #           # hj  j rtj  # 
#   k j   hk h#         #     j  h j  # 
#   h trt  jrht#       #   et        k# 
#j  ehjj      j #######ett  kh kjj k  # 
 #   r  jj    ekk jk    th k   kkk h #  
 #hj       khe kj hr  jj   kk  r j   #  
  #r t    k j  k r  j  jk k hh    jj#   
  #  kjj  h k j       j rrr j  r j  #   
   #j kej  jj    t       h  j   hh #    
    #  he   e  tje j  tjhkjk kj   #     
    #j kt rjk    j j  ee    rkj   #     
     #   jjr e  j jkt j   e  j  j#      
      ##k  thhjj je   kj  kh   ##       
        # hje  j     jj kk t j#         
         ## k       h     e ##          
           ## e jje   kkhj##            
             #############              

PieCot

Posted 2015-09-25T10:47:33.820

Reputation: 1 039

7

Python, 263 bytes

So I saw a challenge with no answers that looked relatively easy but also interesting and thought to myself:

Hmm... If I'm the only one with an answer, I'll be winning until a better answer inevitably shows up.

So I sat down with Python for a few minutes and came up with a rough draft which, with the help of the community's suggestions, I have been tweaking to reduce its size.

from random import*
def D(O,I,S,P):
 a=range(-O,O+1);C=lambda x,y,z,n:(n-.5)**2<x*x+y*y<(z+.5)**2
 if I>=O:I=0
 for y in a:
  R=''
  for x in a:
   if C(x,y,O,O)+(C(x,y,I,I)&(I!=0)):R+='#'
   elif C(x,y,O,I)&(uniform(0,1)<P):R+=choice(s)
   else:R+=' '
  print(R)

For the examples above, this creates

>>> D(10, 4, "^+*-", 0.1)
       #######       
     ##       ##     
    #         * #    
   #             #   
  #          + ^  #  
 # +               # 
 #   + +#####   -  # 
#      ##   ##    ^ #
#     ##     ##  *  #
#-    #       #     #
#     #       #  +  #
# +   #       #     #
#     ##     ##     #
#      ##   ##  *   #
 #+-    #####      # 
 #             - - # 
  #   -    -     +#  
   #      ^      #   
    # -    +    #    
     ## *     ##     
       #######       
>>> 

and

>>> D(5, 2, ":^+*", 0.9)
   #####   
  #*^:* #  
 #^::*:^*# 
#* :###+*:#
#:*#   #+:#
#::#   #+ #
#+:#   #*:#
#^^:###::^#
 # + :*^ # 
  # *:+*#  
   #####   
>>> 

I highly doubt that this is the shortest possible solution, but I think it worked out pretty well for a self-taught teenager's attempt to kill time. Since this was designed to be as small as possible, I have not included comments and have taken shortcuts on every variable name and as such, this program is more for usability than readability.

Should you want to use this code for some reason unbeknownst to me, just run it in IDLE and type the command

D(Outer Radius, Inner Radius, Sprinkles, Chance of Sprinkle)

in the format described above.

Anonymous No Lifer

Posted 2015-09-25T10:47:33.820

Reputation: 311

2

Welcome to PPCG! This is a good first answer, but there's a lot of room for improvement. For starters, removing unnecessary whitespace and shortening all variables to single letters will help, as well as removing the abs call, since the radii are guaranteed to be non-negative. I also recommend checking out Tips for golfing in Python for additional pointers. Again, welcome!

– AdmBorkBork – 2016-06-24T15:35:54.153

2This is a nice first answer! – cat – 2016-06-24T15:36:44.753

Additionally, folks around here tend to use https://mothereff.in/byte-counter for UTF-8 byte counts, just to have a consistent bases. Also, the recommended format for answers starts with ## Language, xx Bytes such as ##Python 2, 57 bytes -- I see that cat just edited that in for you. Please enjoy your stay!

– AdmBorkBork – 2016-06-24T15:38:21.357

1Tip: Call the function D and not Donut, this saves 4 chars, N=False if I==0 or I>=O else True could be not (I==0 or I>=O) and the function C could be a lambda. But it's a really good first entry! – Mega Man – 2016-06-24T17:02:03.873

1you can save on indentation by moving multiple statements onto the same line with semicolons. – Maltysen – 2016-06-24T18:51:48.660

1additionally, i don't think you use P more than once, so there is no point is saving the *100 in a variable. – Maltysen – 2016-06-24T18:53:16.567

You don't seem to be using the math module anywhere, too. Also, from random import*;r=randint is shorter – shooqie – 2016-06-25T15:06:27.717

@AnonymousNoLifer Hi currently this answer is invalid - could you explain to me how your answer chooses where to plot the borders so I can change the challenge so it is? Thanks – Blue – 2016-06-25T20:52:56.233

Neither (n - 0.5)^2 nor (z + 0.5)^2 requires importing math. Also if you use from random import*, you can do uniform(0,1)<P (without multiplying by 100) and choice(S). – Dennis – 2016-06-25T23:24:32.250

@muddyfish I initially tried to make it so the border was exactly on the line of the circle, but this left a lot of gaps so I smoothed it a bit to find all the areas between two circles of radius r-0.5 and r+0.5 to remove gaps. – Anonymous No Lifer – 2016-06-26T11:49:30.460

@Dennis Thanks, I always thought that the n**p thing was part of the math module for some reason. – Anonymous No Lifer – 2016-06-26T11:50:34.177