Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 The 20line or less weekly contest!
Index -> Programming, Turing -> Turing Submissions
Goto page 1, 2, 3, 4, 5, 6  Next
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
MihaiG




PostPosted: Thu Feb 26, 2009 1:54 am   Post subject: The 20line or less weekly contest!

Update:
Since this started thursday, the submitted code will be reviewed wednesday night(or thursday morning) based on submission time(server time), Users and mods can both participate(cept mods dont get bits) winners will recieve 100bits and a "virtual" trophy/badge for each week.


Hey guys,

ive really gotten into having fun in optimizing code so i want to start a weekly event, where users submit programs written in 20 lines or less.
Users will be encouraged to be creative and improve on the code and ideas of others. Some basic rules i want to lay out are:

1.Submissions must have a total length(20 lines or less, commenting does not count).
2.Code must be commented(where not obvious, assume we have only programmed for 3 weeks)
3.Ideas and code may be used from previous submissions in the case where the resulting code is at least 3 lines shorter than the previous(and or similar) version.
4. Reasonable explanations must be included with each submission.
5. Prizes of 100bit will be awarded to weekly winners Smile

And to start it off, i will warp your minds with the Sierpinski Triangle.

code:

View.Set ("graphics:512;512")
%i need a temporary value to store the AND operation to since turing doesnt like using it directly
var z : int
for x : 1 .. maxx%one iteration for each pixel
    for y : 1 .. maxy
        z := x & y
        if z = 0 then
            Draw.Dot (x, y, 7)
        end if
    end for
end for

What this essentialy does it if the AND operation between two coordinates results in zero then a black pixel is drawn,(you can also switch that z = 0 to a z != 0)
Posted Image, might have been reduced in size. Click Image to view fullscreen.

So thats my first submission i am waiting for your submissions!

*edit note you can use a max screen for my program, but it works best with square displays where the window size is a 2^n :]*[/img]
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Thu Feb 26, 2009 3:20 am   Post subject: Re: The 20line or less weekly contest!

MihaiG @ Thu Feb 26, 2009 1:54 am wrote:
i need a temporary value to store the AND operation to since turing doesnt like using it directly

It might not like it, but it works.
Turing:

View.Set ("graphics:512;512")
for i : 0 .. maxx*maxy - 1
    Draw.Dot ((i mod maxx)+1, (i div maxx) + 1, ceil((((i mod maxx)+1) & ((i div maxx)+1))/maxint)*black)
end for

For comments, refer to MihaiG's program above. This does the same thing.

Though calling this "optimized" would be misleading, as it makes the solution worse, in just about every way imaginable.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
The_Bean




PostPosted: Thu Feb 26, 2009 11:00 pm   Post subject: Re: The 20line or less weekly contest!

I propose a line length limit, not because of multiple commands on one line, but to prevent me from doing this...

This is to all those people that were trying to make those projectile games a while back.
Turing:

View.Set ("graphics:1024,400;nobuttonbar;title:Target Practise with Physics;position:center,center")
var velocity : real
var angle : real
var box:int:=Rand.Int(50,maxx)
Draw.FillBox(box-50,0,box+50,20,3)
Draw.ThickLine(0,0,20,10,5,28)
put "Enter power in m/s where 1 pixel = 1 meter (0..100): " ..
get velocity
put "Enter Angle in degrees (1..89): " ..
get angle
for x : 0 .. floor ((velocity ** 2 * sind (2 * angle)) / 9.8)
    Draw.ThickLine(x,round(((-((velocity*sind(angle))*(2*velocity*sind(angle))/9.8*.5-4.9*(.5*(2*velocity*sind(angle))/9.8)**2))/((velocity**2*sind(2*angle))/9.8*.5)**2)*(x-.5*(velocity**2*sind(2*angle))/9.8)**2+((velocity*sind(angle))*(2*velocity*sind(angle))/9.8*.5-4.9*(.5*(2*velocity*sind(angle))/9.8)**2)),x+1,round(((-((velocity*sind(angle))*(2*velocity*sind(angle))/9.8*.5-4.9*(.5*(2*velocity*sind(angle))/9.8)**2))/((velocity**2*sind(2*angle))/9.8*.5)** 2)*((x+1)-.5*(velocity**2*sind(2*angle))/9.8)**2+((velocity*sind(angle))*(2*velocity*sind(angle))/9.8*.5-4.9*(.5*(2*velocity*sind(angle))/9.8)**2)),3,7)
    delay(2)
end for
if (velocity ** 2 * sind (2 * angle)) / 9.8>=box-50 and (velocity ** 2 * sind (2 * angle)) / 9.8<=box+50 then
    put "You Hit It!"..
else
    put "You Missed!"..
end if

To anyone saying that its more than 20 lines, the Draw.ThickLine() is only actually on 1 line, it looks like more because of text wrapping.

And heres a nicer version for the commenting part.
Turing:

View.Set ("graphics:1024,400;nobuttonbar")
var velocity : real
var angle : real
put "Enter power(0..100): " ..
get velocity
put "Enter Angle(0..89): " ..
get angle
var tTime : real := (2 * velocity * sind (angle)) / 9.8  %calculate the time the projectile will be in the air
var maxDistance : real := (velocity ** 2 * sind (2 * angle)) / 9.8 %find the horizantal displacement of the projectile
var maxHeight : real := (velocity * sind (angle)) * tTime * .5 - 4.9 * (.5 * tTime) ** 2 % find the maximum height of the projectile
var porabolaA : real := (-maxHeight) / (maxDistance * .5) ** 2 %find the 'a' value in the vertex form of a probola y=a(x-d)**2+c where y=0 x=maxDistance d=half the max distance c=max height
for x : 0 .. floor (maxDistance)%go through all x values in the horizantal displacement
    Draw.Line (x, round (porabolaA * (x - .5 * maxDistance) ** 2 + maxHeight),x+1,round (porabolaA * ((x+1) - .5 * maxDistance) ** 2 + maxHeight), 7) %draw a line connecting one point to the next
    %%%In the other program i replaced all of the variables with what they equaled according to velocity and angle which was really long a repedative
    delay (2)
end for
MihaiG




PostPosted: Fri Feb 27, 2009 11:16 am   Post subject: RE:The 20line or less weekly contest!

Heres my take on it, line length should not be an issue if the line wraps around(i know ive written some nested stuff that is huge), just as long as you only have like "one" main fcn on that line
Clayton




PostPosted: Fri Feb 27, 2009 11:32 am   Post subject: RE:The 20line or less weekly contest!

What about curly braces? Wink Is:

Java:
public class Test
{
    public static void main (String[] args)
    {
        System.out.println("How many lines is this?");
    }
}


3 lines, or 7?
copthesaint




PostPosted: Fri Feb 27, 2009 11:59 am   Post subject: Re: The 20line or less weekly contest!

What counts as a line? ie does for statement, if statements ect.

Does this count as 2 lines?
code:
Loop
end loop
----------
for
end for
----------
if
end if
----------
Select case
end case
----------

Does This count as 1 line?
code:
else
----------
copthesaint




PostPosted: Fri Feb 27, 2009 12:55 pm   Post subject: Re: The 20line or less weekly contest!

This is my add on (15 lines) that I made, It's like The Draw.Fill AND it doesn't delete other colors.
The problem with this is though if you the color is in the are ANYWHERE it will change it. It has It's flaws and benefits.
Turing:

%This find Dots Equal to The 2nd color and replaces them with the 1st color
Draw.FillOval (25,25,25,25,black)%This is just to show you that it works
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%The program%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
var Xvalue,Yvalue : int := 0
procedure ChangeDotColorArea (X1value : real, Y1value : real, X2value : real, Y2value : real, Color1 : int, Color2 : int)
Xvalue:= round (X1value)
Yvalue:= round (Y1value)
    for z : round (X1value * Y1value) .. round (X2value * Y2value)
    Xvalue+=1
    if Xvalue > X2value then
    Xvalue:= round (X1value)
    Yvalue+= 1
    end if
        if View.WhatDotColor (Xvalue,Yvalue) = Color2 then
            Draw.Dot (Xvalue,Yvalue,Color1)
        end if
    end for
end ChangeDotColorArea
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%End of the program%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
ChangeDotColorArea (0,0,25,25,blue,black) %This is also just to show you that it works
ChangeDotColorArea (25,25,50,50,blue,black)
ChangeDotColorArea (25,0,50,25,red,black)
ChangeDotColorArea (0,25,25,50,red,black)
MihaiG




PostPosted: Fri Feb 27, 2009 3:10 pm   Post subject: RE:The 20line or less weekly contest!

Clayton: only turing, hence why it was posted in the turing section, any part of the code that is interpreted/excecuted (non commenting or non space) counts to the line count
Sponsor
Sponsor
Sponsor
sponsor
SNIPERDUDE




PostPosted: Mon Mar 02, 2009 9:40 pm   Post subject: RE:The 20line or less weekly contest!

I'm guessing using semi-colons to shorten it is not allowed?
zylum




PostPosted: Mon Mar 02, 2009 11:49 pm   Post subject: RE:The 20line or less weekly contest!

Another Sierpinski triangle generator. Generates a random triangle.. This could be made a lot shorter with some trickery but I thought I'd keep it in this somewhat clean state Wink

code:
View.Set ("graphics:max;max")

var d : array 1 .. 8 of real

for i : 0 .. 3
    d (i * 2 + 1) := Rand.Int (1, maxx)
    d (i * 2 + 2) := Rand.Int (1, maxy)
end for

for : 0 .. 100000
    var r := Rand.Int (0, 2)
    d (7) := (d (7) + d (r * 2 + 1)) / 2
    d (8) := (d (8) + d (r * 2 + 2)) / 2
    Draw.Dot (floor (d (7)), floor (d (8)), black)
end for
syntax_error




PostPosted: Mon Mar 02, 2009 11:51 pm   Post subject: Re: RE:The 20line or less weekly contest!

SNIPERDUDE @ Mon Mar 02, 2009 9:40 pm wrote:
I'm guessing using semi-colons to shorten it is not allowed?



MihaiG wrote:

Clayton only turing, hence why it was posted in the turing section, any part of the code that is interpreted/excecuted (non commenting or non space) counts to the line count


Does turing have semi colons? I think not.

Edit: Fixed tags.
Tony




PostPosted: Mon Mar 02, 2009 11:54 pm   Post subject: RE:The 20line or less weekly contest!

Turing supports semi-colon separation of statements
Turing:

put "one"; put "two"
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
BigBear




PostPosted: Tue Mar 03, 2009 4:14 pm   Post subject: Re: RE:The 20line or less weekly contest!

Tony @ Mon Mar 02, 2009 11:54 pm wrote:
Turing supports semi-colon separation of statements
Turing:

put "one"; put "two"

I am pretty sure that 20 lines is after F2 formatting
MihaiG




PostPosted: Tue Mar 03, 2009 5:55 pm   Post subject: Re: The 20line or less weekly contest!

excellent zylum Very Happy love it id love to see the trickery Smile

i think the point is to strive in creating nifty pieces of code, not how to cheat the system,
also i think to be fair, in all respects, only post one submission a week,(unless requested, ie how i requested zylum's "alternate")

but listen it doesnt have to be graphical, i just did that as an example

also, i think it would be good to also post explanations of the code posted , ie zylum Very Happy
MihaiG




PostPosted: Thu Mar 05, 2009 4:42 pm   Post subject: Re: The 20line or less weekly contest!

Ok so first week is over, and the award goes to Zylum.
Posted Image, might have been reduced in size. Click Image to view fullscreen.


+100bits to zylum!

to start of week2 i give you fun Very Happy i give you 15(or 16) lines of code with trickery
CODE:

View.Set ("offscreenonly")
%points of a cube assuming a side length of 2 with the center at [0,0,0]
var xp : array 1 .. 8 of real := init (-1, -1, 1, 1, -1, 1, 1, -1)
var yp : array 1 .. 8 of real := init (-1, 1, 1, -1, -1, -1, 1, 1)
var zp : array 1 .. 8 of real := init (1, 1, 1, 1, -1, -1, -1, -1)
loop
    cls
    for i : 1 .. upper (xp)
    %rotates on the z axis and then the x axis, soem trickery was used, such as precomputed values of sin/cos (for 1/2 of a degree)
    %and to eliminate the use of temporary variables i used previously computed values and solved for their old values
   
        xp (i) := xp (i) * 0.999961923 + yp (i) * 0.0087265355
        yp (i) := yp (i) * 0.999961923 - ((xp (i) - yp (i) * 0.0087265355) / 0.999961923) * 0.0087265355
        zp (i) := zp (i) * 0.999961923 + yp (i) * 0.0087265355
        yp (i) := yp (i) * 0.999961923 - ((zp (i) - yp (i) * 0.0087265355) / 0.999961923) * 0.0087265355
        %enlarges the size by 10 times and reduces the "perspective" effect to a very minimal feel, aos shifts to the middel of the user's screen
        %the size is determined by the distance to the center, so the closer it is, the smaller, it is, (you can do the inverse and multiply by a larger constant to do the reverse
        Draw.FillOval (round (xp (i) * 10 / (zp (i) * 0.001 - 0.25) + maxx / 2), round (yp (i) * 10 / (zp (i) * 0.001 - 0.25) + maxy / 2), ceil (sqrt (xp (i) ** 2 + yp (i) ** 2 + zp (i) ** 2)) * 2,
            ceil (sqrt (xp (i) ** 2 + yp (i) ** 2 + zp (i) ** 2)) * 2, 7)
            delay(1)
    end for
    View.Update
end loop

you can remove the delay if it is going to slow
Display posts from previous:   
   Index -> Programming, Turing -> Turing Submissions
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 6  [ 84 Posts ]
Goto page 1, 2, 3, 4, 5, 6  Next
Jump to:   


Style:  
Search: