9 Holes of Code Golf - Kickoff

12

4

There has already been a 9 Hole Challenge (and another one here), but that was soooo last year. And besides, I have only been around for 2 months (although it seems like forever). And it is considerably different.

Leader Board: (note that weighting for holes are not established yet)

+---------------+------------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+
|Competitor     | Language   | Hole 1 | Hole 2 | Hole 3 | Hole 4 | Hole 5 | Hole 6 | Hole 7 | Hole 8 | Hole 9 | Total  |
+----------------------------------------------------------------------------------------------------------------------+
|Dennis         |CJam        |        | 31     |        |        |        |        |        |        |        | 31     |
+----------------------------------------------------------------------------------------------------------------------+
|Optimizer      |CJam        |        | 35     |        |        |        |        |        |        |        | 35     |
+----------------------------------------------------------------------------------------------------------------------+
|Martin Büttner |Mathematica |        | 222    |        |        |        |        |        |        |        | 222    |
+----------------------------------------------------------------------------------------------------------------------+
|Cameron        |Python      | 878    |        |        |        |        |        |        |        |        | 878    |
+----------------------------------------------------------------------------------------------------------------------+
|bubalou        |Processing 2| 717    |        |        |        |        |        |        |        |        | 717    |
+----------------------------------------------------------------------------------------------------------------------+
|Doorknob       |Python 2    |        |        |0.079711|        |        |        |        |        |        |0.079711|
+----------------------------------------------------------------------------------------------------------------------+
|Vulcan         |Java        |        |        |0.6949  |        |        |        |        |        |        |0.6949  |
+----------------------------------------------------------------------------------------------------------------------+
|Eli            |C++         |        |        |1.42042 |        |        |        |        |        |        |1.42042 |
+---------------+------------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+

Other Holes:

It's been slow around here lately, so I am here to kick off the next 9 hole golf course, involving a smattering of all that I have seen here in my (albeit limited) experience. This will entail:

  • Ascii Art
  • Graphical Output
  • Conway's Game of Life
  • King of the Hill
  • Kolmogorov complexity
  • Quines
  • Image processing
  • Math
  • Classic code golf.

I am really looking forward to seeing what you can do!

Competition Rules

  • You will choose 1 language for all of the holes (that you participate in...see point 4 for more info).
  • For all holes - standard loopholes are (still) not funny.
  • The next challenge will appear when I see that there are enough submissions given a reasonable amount of time. King of the hill, for example, will take longer.
  • You do not have to participate in all of the holes. If you find a hole particularly challenging, don't have the time to do it, etc, you will receive 2 times as many points as the lowest score. Please do not take advantage of this rule by leaving 1 golfscript answer with 12 characters and taking 24 as a score.

Scoring

  • Your score is based on the culmination of the scores from all of the holes
  • Lowest score wins (as per real golf)
  • The leaderboard will be placed at the top of this page

I will try my very best to make sure that no questions are duplicates, all questions have objective criteria, are well written, and that they all have (relatively) equal weight in the context of the whole contest.

However, Please be patient when I fail.

And, without further ado, the first challenge!

The Plasma Globe

A plasma globe is everyone's favorite toy:

enter image description here

Your job it to draw one.

You must draw a base:

enter image description here

a globe:

enter image description here

A tesla thingy (?)

enter image description here

And, of course, cool plasma shoots:

enter image description here

However, when you put your hand near a plasma globe (or a conductive object as Wikipedia tells me), it attracts the plasma.

Your globe should reflect that.

To put it quantitatively, your globe has a maximum of 16 shoots (see above picture). An "object" on the surface of the globe (given by an angle in radians) will contain "conductive power", that is, the amount of beams it attracts. So an object with a power of 5 will attract 5 beams (a single line with thickness 5) leaving 11 to spread evenly among the rest of the globe:

enter image description here

Note that
1. the black circle in the middle remains above the plasma
2. that in this example, the angle would be pi/2.

You may have more than one conductive object, and in that event, it is not required that the shoots are equally spaced apart. However, they be relatively spaced out. For example, this is ok for 2 objects, 1 at angle pi/4 power 5 and another at angle 5pi/3 power 3:

enter image description here

You should be able to see each of the end points of the shoots (of plasma).

However, given an object (or sum of objects) with power greater than 16, the globe will "break":

enter image description here

Notes

  • The globe's diameter is 1.5 times bigger than the length of the base, which is a square
  • When the globe breaks, the globe is on the right side. It is tangent to the right side of the base as well as the ground. There should be no plasma when the plasma globe is broken (why? safety features of course! No word on how it broke in the first place.)
  • The color of everything except the shoots must be black, and 1 pixel thick. The color of the plasma has hue 245 - 280, and saturation/value 100. Use this under "HSV" if you have no idea what I am talking about.

Input

Input can be through STDIN/command line args/whatever or through function arguments.

There should be 2 inputs - length of the base of the plasma globe (include an axis if using vector graphics), and an array of objects like this:

[[angle,power],[angle,power],[angle,power]]

So for no objects (see first picture with 16 lines) the input would be

100,[]

For the next (one object, power 5) it would be:

100,[[1.570796,5]]

For the last example:

100,[[0.785398,5],[5.23598,3]]

This is code-golf, so shortest code in bytes wins.

Stretch Maniac

Posted 2014-10-12T23:02:09.657

Reputation: 3 971

@MartinBüttner If the image does not get smaller when the base-length get smaller, include an axis – Stretch Maniac – 2014-10-12T23:29:20.790

In your second example. What if the second object was at -3/8π? In that case it would overlap exactly with one of the other beams. Do we have to rotate all the beams in such a case? If so, I think you need to specify how far away the individual beams need to be from the object-induced beams. – Martin Ender – 2014-10-12T23:43:27.390

You should rotate the rest of the beams in the event of an exact overlap, as you said. Each beam should be visually distinct from one another, so I should see the object induced beam as being separate from the beam it is close to. If you need more specification, you can use .1 as the minimum angle. However, I don't want to specify this too much, because a beam of power 15 will cover much more than a beam of power 3, therefore needing a much greater buffer distance. – Stretch Maniac – 2014-10-12T23:53:03.753

1er.... why the column "hole 2" of the board is already filled even if there are no answers? – xem – 2014-10-17T20:28:47.830

How big should the square become? Is it required to have a line (or anything else that visually distinguishes it from the background)? – user2428118 – 2014-10-17T22:44:03.013

Does the globe have to be exactly centered at the top of the square? – user2428118 – 2014-10-17T22:59:36.820

2

A little help to better visualize this: http://inear.se/plasmaball/

– CSᵠ – 2014-10-18T00:09:54.623

@user2428118 The globe should be centered above (and tangent to) the square... Within a pixel or 2 is fine. The square should be visible - so yes, it should be distinguishable from the background. – Stretch Maniac – 2014-10-18T00:32:21.543

1@xem um, there are answers to hole 2, so why not post them? This doesn't necessarily have to go in order... – Stretch Maniac – 2014-10-18T00:34:15.083

If only I could think of a reliable way to ensure that object-induced beams and leftover beams never overlap... – Martin Ender – 2014-10-18T01:41:11.770

ah ok each hole has its own thread. nevermind ten ;) – xem – 2014-10-18T06:14:14.813

@StretchManiac Acc. to your rules. If answers in first and second hole remain like what they are now, Cameron gets over 800 point advantage over the rest of the 3 participants. (Re: if you do not participate in a hole, you get double of worst score) – Optimizer – 2014-10-19T17:32:06.930

@Optimizer I was going to do update weights and no answers only periodically (rather than every time someone answers). Otherwise, it would require substantial changes whenever anyone answers. Think of it as a raw score table, not as the actual winning table (though I will throw one up eventually). – Stretch Maniac – 2014-10-19T21:24:27.070

@StretchManiac You did not get my point. Your that rule is putting the other three contenders to a great disadvantage. – Optimizer – 2014-10-20T05:45:47.453

@Optimizer All of the scores will eventually become the same weight - so the hole 2 scores might be multiplied by 30 (as an example) to even the scores out. Then the double rule will take effect. – Stretch Maniac – 2014-10-21T00:05:33.640

Answers

4

Processing 2 - 717 characters

As processing is a language made for artists, and I am a very novice programmer, I do not expect to do very well on many of these challenges. That being said, I really like how simple it is to draw things in processing, and I have been meaning to play around with it some more, so these challenges should be interesting.

int j,m,b,k,d,l;float w,c,h,x,y,z;float v[],p[],g[];void setup(){j=m=d=0;c=z=0;String i[]= loadStrings("f.txt");i[0]=i[0].replace("[","").replace("]","");String o[]=split(i[0],',');v=new float[o.length];p=new float[o.length-1];for(String s:o){if(!s.equals("")){v[j]=Float.parseFloat(s);}j++;}w=v[0];size((int)w*3,(int)w*3);h=w*.75;l=v.length;noLoop();}void draw(){translate(w/2,height);scale(1,-1);rect(0,0,w,w);if(l>2){while(m<j-1){m+=2;c+=v[m];}}if(c>16){ellipse(w+h,h,2*h,2*h);rect(w/2,w,1,h);}else{ellipse(w/2,w+h,2*h,2*h);rect(w/2,w,1,h);b=16;m=1;stroke(#1500ff);if(l>2){while(m<j){p[m-1]=cos(v[m])*h;p[m]=sin(v[m])*h;strokeWeight(v[m+1]);line(w/2,w+h,p[m-1]+w/2,p[m]+w+h);b-=v[m+1];m+=2;}}strokeWeight(1);c=(PI*2)/b;k=b;g=new float[b+b];while(b>0){g[d]=cos(z+c*b)*h;g[d+1]=sin(z+c*b)*h;m=0;if(l>2){while(m<j-1){if(abs(g[d]-p[m])<.1 && abs(g[d+1]-p[m+1])<.1){b=k+1;z=z+.1;d=-2;break;}m+=2;}}b--;d+=2;}d--;while(d>0){line(w/2,w+h,g[d]+w/2,g[d-1]+w+h);d-=2;}}stroke(#000000);fill(#000000);ellipse(w/2,w+h,w/9,w/9);}

I'm sure this can be golfed down considerably and I may try to do that when I have more time, but for now I am happy with it.

reads in a file (f.txt) located in the data folder of the sketch to get its input. I used Camerons idea of shifting the free beams until they don't overlap.

int j,m,b,k,d,l;
float w,c,h,x,y,z;
float v[],p[],g[];
void setup(){
j=m=d=0;
c=z=0;
String i[]= loadStrings("f.txt");
i[0]=i[0].replace("[","").replace("]","");
String o[]=split(i[0],',');
v=new float[o.length];
p=new float[o.length-1];
for(String s:o){if(!s.equals("")){v[j]=Float.parseFloat(s);}j++;}
w=v[0];
size((int)w*3,(int)w*3);
h=w*.75;
l=v.length;
noLoop();
}

void draw()
{
  translate(w/2,height);
  scale(1,-1);
  rect(0,0,w,w);
  if(l>2) 
  {
    while(m<j-1)
    {
      m+=2;
      c+=v[m];
    }
  }
  if(c>16)
  {
    ellipse(w+h,h,2*h,2*h);
    rect(w/2,w,1,h);
  }
  else
  {
    ellipse(w/2,w+h,2*h,2*h);
    rect(w/2,w,1,h);
    b=16;m=1;
    stroke(#1500ff);
    if(l>2)
    { 
      while(m<j)
      {
        p[m-1] = cos(v[m]) * h;
        p[m] = sin(v[m]) * h;
        strokeWeight(v[m+1]);
        line(w/2,w+h,p[m-1]+w/2,p[m]+w+h);
        b-=v[m+1];
        m+=2;
      }
    }
    strokeWeight(1);
    c=(PI*2)/b;
    k=b;
    g=new float[b+b];
    while(b>0)
    {
      g[d] = cos(z+c*b) * h;
      g[d+1] = sin(z+c*b) * h;
      m=0;
      if(l>2)
      {
        while(m<j-1)
        {
          if(abs(g[d]-p[m])<.1 && abs(g[d+1]-p[m+1])<.1)
          {
            b=k+1;
            z=z+.1;
            d=-2;
            break;
          }
          m+=2;
        }
      }
      b--;
      d+=2;
    }
    d--;
    while(d>0)
    {
      line(w/2,w+h,g[d]+w/2,g[d-1]+w+h);
      d-=2;
    }
  }
    stroke(#000000);
    fill(#000000);
    ellipse(w/2,w+h,w/9,w/9);
}

Examples:

100,[[0.785398,3],[5.23598,5]]

plasma orb 1

100,[]

plasma orb 2

100,[[1.72398,12],[5.23598,5]]

plasma orb 3

get processing here

bubalou

Posted 2014-10-12T23:02:09.657

Reputation: 305

3

Python, 878 chars

By no means is this golfed well, but I wanted to see an answer for this hole.

import matplotlib.pyplot as P
from math import *
L=len
M=min
Y=P.plot
K=P.Circle
Z=P.gcf().gca().add_artist
f=sin
g=cos
k={'color':(0,0,0)}
j={'color':(.16,0,1)}
def Q(S,C):
    P.axis([-S,S*2.5,0,S*3.5],**k)
    Y([0,S,S,0,0],[0,0,S,S,0],**k)
    Y([S/2,S/2],[S,7*S/4],**k)
    Z(K([S/2,7*S/4],S/20,**k))

    k['fill']=False

    A,B=zip(*C)

    N=16-sum(B)
    if N<0:
        Z(K([7*S/4,3*S/4],3*S/4,**k))

    else:
        Z(K([S/2,7*S/4],3*S/4,**k))
        if L(C)==0:
            D(16,0,S)
        elif L(C)==1:
            D(N,A[0],S)
            Y([S/2,S/2+3*S*g(A[0])/4],[7*S/4,7*S/4+3*S*f(A[0])/4],linewidth=B[0],**j)
        else:
            for c in C:
                Y([S/2,S/2+3*S*g(c[0])/4],[7*S/4,7*S/4+3*S*f(c[0])/4],linewidth=c[1],**j)
            D(N,J(N,A),S)
    P.show()


def J(N,A):
    T=d=0
    t=2*pi/N
    while d<0.1:
        T+=0.1
        d=M(M(a-T-floor((a-T)/t)*t for a in A),\
            M(T+ceil((a-T)/t)*t-a for a in A))
    return T


def D(N,I,S):
    a=I
    for i in range(N):
        Y([S/2,S/2+3*S*g(a)/4],[7*S/4,7*S/4+3*S*f(a)/4],**j)
        a+=2*pi/N

And some sample outputs

Q(100,[[pi/4,6],[-4.2*pi/8,1]])

enter image description here

Q(100,[[0.785398,10],[5.23598,7]])

enter image description here

Q(100,[[pi/4,3],[pi/2,3],[3*pi/2,2],[5*pi/4,2]])

enter image description here

Cameron

Posted 2014-10-12T23:02:09.657

Reputation: 997

How does this ensure that object-induced and free beams never overlap? – Martin Ender – 2014-10-19T20:36:09.523

I rotate the equally spaced beams by a small amount until I find an orientation that ensures the beams are at least 0.1 radians spaced apart – Cameron – 2014-10-19T20:56:16.467

Ah, right, I expected something like that. Thanks for clarifying! – Martin Ender – 2014-10-19T20:58:19.780

Np, I've been trying to solve how to maximize the minimal distance between any object-induced beam and any free beam, but its seems to be a difficult question – Cameron – 2014-10-19T20:59:14.477

I think that's the inverse problem to this http://codegolf.stackexchange.com/q/37269/8478

– Martin Ender – 2014-10-19T21:00:27.130

1A,B=zip(*C) should save a few bytes – gnibbler – 2014-10-21T02:08:32.613

1

Python 2.7, 378 375

from turtle import *;import sys
(s,P),A,B=eval(sys.argv[1]),90,180
n,S=sum([b for(a,b) in P]),.75*s;l=16-n
for i in 'abcd':fd(s);lt(A)
pu()
if l<0:goto(s+S,0)
else:goto(s/2,s)
pd();circle(S);pu();goto(s/2,s);lt(A);pd();fd(S)
def C():fd(S);fd(-S)
if n<16:
 color('blue')
 for i in range(l):rt(360/l);C()
 for a,p in P:pensize(p);rt(a*57.3);C()
color('black')
shape('circle')

It reads its parameters from commandline argument.

Sample images :

(parameters=100,[[0.785398,5],[5.23598,3]])

tesla

(parameters=100,[])

tesla2

(parameters=100,[[1.72398,12],[5.23598,5]])

tesla3

dieter

Posted 2014-10-12T23:02:09.657

Reputation: 2 010

I wasn't able to test this, but it's golfed a bit here: https://repl.it/E52i

– 0WJYxW9FMN – 2016-11-22T17:35:57.813