19
\$\begingroup\$

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

\$\endgroup\$
  • \$\begingroup\$ Should there be an equal distribution of sparkles, or non uniform distribution will also do. \$\endgroup\$ – Kishan Kumar Sep 25 '15 at 16:04
  • \$\begingroup\$ There should be an equal distribution of sprinkles. \$\endgroup\$ – Blue Sep 25 '15 at 16:52
  • \$\begingroup\$ It's not clear to me from the spec which positions correspond to the borders of the circles. \$\endgroup\$ – Dennis Jun 25 '16 at 4:58
  • \$\begingroup\$ @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) \$\endgroup\$ – Blue Jun 25 '16 at 19:21
  • \$\begingroup\$ 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. \$\endgroup\$ – Dennis Jun 25 '16 at 20:30
2
\$\begingroup\$

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##            
             #############              
\$\endgroup\$
7
\$\begingroup\$

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.

\$\endgroup\$
  • 2
    \$\begingroup\$ 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! \$\endgroup\$ – AdmBorkBork Jun 24 '16 at 15:35
  • 2
    \$\begingroup\$ This is a nice first answer! \$\endgroup\$ – cat Jun 24 '16 at 15:36
  • 1
    \$\begingroup\$ Tip: 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! \$\endgroup\$ – Mega Man Jun 24 '16 at 17:02
  • 1
    \$\begingroup\$ you can save on indentation by moving multiple statements onto the same line with semicolons. \$\endgroup\$ – Maltysen Jun 24 '16 at 18:51
  • 1
    \$\begingroup\$ additionally, i don't think you use P more than once, so there is no point is saving the *100 in a variable. \$\endgroup\$ – Maltysen Jun 24 '16 at 18:53

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.