Your task is to write the shortest computer program (measured as filesize, not lines or characters) in any language that can solve the following problem:
[quote]
[b]Input File:[/b] sitin.txt
[b]Output File:[/b] sitout.txt
[b]Time Limit:[/b] 1 second
A local musician is putting on a concert to raise money for charity. The concert will be held in the town hall, a spacious venue perfectly suited for such an event. There are r rows of seats, each containing exactly s seats. At most one person can sit on a single seat (that is, two people cannot share a seat).
There is a problem - the concert may have been overbooked! This means that if everybody who bought tickets comes to the concert, some of them might have to stand. Now the musician has aproached you, not for advice, but for the answer to the following question: if everybody who bought tickets arrives and tries to find a seat, how many people will end up sitting, and how many people will be standing?
[b]Input[/b]
The first line of the input file will consist of the two integers r and s: the number of rows and the number of seats per row in that order. It is guaranteed that 0 <= r, s <= 10,000. The second line will contain a single integer between 0 and 1,000,000,000: the number of tickets that have been bought.
[b]Output[/b]
Your output file should consist of two integers separated by a space: the number of people sitting and standing respectively.
Remember, everybody tries to sit if they can. If the concert has been overbooked, you will not be able to seat them all.
[b]Sample Input[/b]
7 12
100
[b]Sample Output[/b]
84 16
[b]Explanation[/b]
The first line of input says that there are 7 rows of 12 seats each (i.e. 7 x 12 = 84 seats total). The second line says that 100 people have booked tickets. Since there aren't enough seats for all of them, only 84 people may sit and the remaining 16 must stand.
[/quote]
Your program must output the correct answer for any input given to it, and it must run within the time requirements.
The competition will end at 10:00 GMT Saturday, 18th December, at which point the winner will be the person who has submitted the shortest source code in terms of bytes. The prize will be a cookie of some description.
Let the hacking begin!
Working on it right now
I gave it a go, some python wizard who loves all this crazy stuff and can hack the language to pieces will probably laugh at my pitiful attempt.
[code]with open("sitin.txt")as f:i,s=int,str;d=f.readlines();v=d[0].split();l=(i(v[0]),i(v[1]));t=i(d[1].split()[0]);open("sitout.txt","w").write(s(l[0]*l[1])+" "+s(t-l[0]*l[1]))[/code]
198 Bytes.
[lua]f=io.input"sitin.txt"v1,v2,v3=f:read"a*":match"(%d+) (%d+)\n(%d+)"io.output"sitout.txt":write(v1*v2," ",v3-v1*v2)[/lua]
115 bytes. I'll write a better one when I get home, no tools here at school.
Damn... I was down to 45 characters when I noticed I had to read the input from a file and not stdin...
[code]7 12
100[/code]8 bytes in SITCODE. My own custom language.
[cpp]#include<stdio.h>
int main(){int a,b,c,d;FILE*f=fopen("sitin.txt","r");FILE*g=fopen("sitout.txt","w");fscanf(f,"%d %d\n%d",&a,&b,&c);d=a*b;fprintf(g,"%d %d\n",d,(c<=d)?0:c%d);return 0;}[/cpp]
I'm certain that's about as small as you can get a C 'interpreter' for SITCODE. In ANSI C.
185 bytes.
[QUOTE=Jookia;26708863][code]7 12
100[/code]8 bytes in SITCODE. My own custom language.[/quote]
Give me a copy of the interpreter, and no problem!
[quote][cpp]#include<stdio.h>
int main(){int a,b,c,d,e;FILE*f=fopen("sitin.txt","r");FILE*g=fopen("sitout.txt","w");fscanf(f,"%d %d\n%d",&a,&b,&c);d=a*b;e=(c<=d)?0:c%d;fprintf(g,"%d %d\n",d,e);return 0;}[/cpp]
I'm certain that's about as small as you can get a C 'interpreter' for SITCODE. In ANSI C.
191 bytes.[/QUOTE]
You can get rid of the return 0 and make it smaller in other ways.
What's the expected output if there's enough seats? "#people 0"?
If that's the case are none of the currently submitted programs correct.
I reduced it to 185 bytes. That's the lowest (I'll eat my own words later) it can go in ANSI C (without warnings).
[QUOTE=itsbth;26708966]What's the expected output if there's enough seats? "#people 0"?[/QUOTE]
Yes
[editline]15th December 2010[/editline]
[QUOTE=Jookia;26708975]I reduced it to 185 bytes. That's the lowest (I'll eat my own words later) it can go in ANSI C (without warnings).[/QUOTE]
Warnings are fine.
[QUOTE=itsbth;26708966]What's the expected output if there's enough seats? "#people 0"?
If that's the case are none of the currently submitted programs correct.[/QUOTE]
My program returns the number of seats and 0. Because there's no people standings.
[editline]15th December 2010[/editline]
[QUOTE=Siemens;26708980]Warnings are fine.[/QUOTE]
It was more of a code tidyness thing, but I'll do warnings then..
This one
[code]with open('sitin.txt')as i, open('sitout.txt','w')as u:j=int;s=i.read().split();x=j(s[0])*j(s[1]);y=j(s[2])-x;u.write('%s %s'%(x,y))[/code]
and this one
[code]
o=open
with o('sitin.txt')as i,o('sitout.txt','w')as u:j=int;s=i.read().split();x=j(s[0])*j(s[1]);y=j(s[2])-x;u.write('%s %s'%(x,y))
[/code]
Are both 132 characters, 133 bytes for filesize (Accounts for EOF character, in both cases).
Kind of a fake edit:
Just as I go to post this I noticed Robert's solution. Almost dude! Combined your s=str and s(var) code results in larger code than if you were to just use the python 2.x string formatting style. (% operator)
Last edit, hopefully:
I also just noticed it needs to handle cases where there are more than enough seats. Fixed version below. final size 147 bytes
[code]
o=open
with o('sitin.txt')as i,o('sitout.txt','w')as u:j=int;s=i.read().split();x=j(s[0])*j(s[1]);y=j(s[2])-x;u.write('%s %s'%(x,y if y>0 else 0))
[/code]
[cpp]#include<stdio.h>
main(){int a,b,c,d;FILE*f=fopen("sitin.txt","r"),*g=fopen("sitout.txt","w");fscanf(f,"%d %d\n%d",&a,&b,&c);d=a*b;fprintf(g,"%d %d\n",d,c<=d?0:c%d);}[/cpp]
166. I really don't think I can get it any smaller.
[QUOTE=Jookia;26709134][cpp]#include<stdio.h>
main(){int a,b,c,d;FILE*f=fopen("sitin.txt","r"),*g=fopen("sitout.txt","w");fscanf(f,"%d %d\n%d",&a,&b,&c);d=a*b;fprintf(g,"%d %d\n",d,c<=d?0:c%d);}[/cpp]
166. I really don't think I can get it any smaller.[/QUOTE]
You can treat a FILE* as an int on 32 bit systems.
You could also use a long for your int values and retain compatibility with x64
I use 64bit, so I don't see that working.
[cpp]#include<stdio.h>
main(){int a,b,c,d;FILE*e=fopen("sitin.txt","r"),*f=fopen("sitout.txt","w");fscanf(e,"%d %d\n%d",&a,&b,&c);d=a*b;fprintf(f,"%d %d\n",d,c<=d?0:c-d);}[/cpp]
Fixed the code so it actually does what you ask.
[QUOTE=Siemens;26708099]Your task is to write the shortest computer program (measured as filesize, not lines or characters) in any language that can solve the following problem:[/QUOTE]
[QUOTE=Siemens;26708965]Give me a copy of the interpreter, and no problem![/QUOTE]
Wait, what? If I write my own (1) byte-code language interpreter with the whole problem part of this task built in the language, I can do this in 1 byte of source code for that language. Would that be allowed, because it would make this whole competition pointless?
[QUOTE=Siemens;26708099]the winner will be the person who has submitted the shortest source code in terms of bytes.[/QUOTE]
I was hoping this was about compiled programs. It would be neat and very good practice if we had to get our hands dirty trying to squeeze PE32 headers and stuff for smaller size.
[QUOTE=Spoco;26709879]I was hoping this was about compiled programs. It would be neat and very good practice if we had to get our hands dirty trying to squeeze PE32 headers and stuff for smaller size.[/QUOTE]
I use a COM file. What now?
[QUOTE=HubmaN;26710369]I use a COM file. What now?[/QUOTE]
Due to differences in executable file formats, in a competition where the only goal is small size, the file format should be limited to only one format.
[QUOTE=Spoco;26710524]Due to differences in executable file formats, in a competition where the only goal is small size, the file format should be limited to only one format.[/QUOTE]
Now that I think of it, there's still plenty of ground to be had at Real Man Assembling it. I take my previous statement back.
I think I'll take a shot at this using Piet :v:
I expected a competition where you had to make a game of golf, was disappointed :smith:
[code]main=do{f<-readFile"sitin.txt";let{[r,c,p]=map(read::String->Int)$words f;t=r*c} in writeFile"sitout.txt"$concat[show t," ",show$if p<t then 0 else p`mod`t]}[/code]
154 for a Haskell solution. Forced type separation and no access to the dangling fruits of the full Functor and friends makes this Imperative Haskell. And the fact that unsafePerformIO is long for a reason :( I would do so many bad things with it.
Holy crap, I'm the first one to get under 100 bytes!
[lua]n="*n"f=io.input"sitin.txt"a,b,c=f:read(n,n,n)io.output"sitout.txt":write(a*b," ",c-a*b)[/lua]
90 bytes :smug:
Now to try Perl...
[QUOTE=raBBish;26711161]Holy crap, I'm the first one to get under 100 bytes!
[lua]n="*n"f=io.input"sitin.txt"a,b,c=f:read(n,n,n)io.output"sitout.txt":write(a*b," ",c-a*b)[/lua]
90 bytes :smug:
Now to try Perl...[/QUOTE]
That's not a valid solution. It gives the wrong output if there's enough seats.
Are you allowed to produce a negative value for the second number? This issue's been raised above...
[QUOTE=itsbth;26711227]That's not a valid solution. It gives the wrong output if there's enough seats.[/QUOTE]
Damn, I should've read the instructions properly, sorry :smith:
ninajed!
Here's the fixed version, 112 bytes though...
[lua]n="*n"f=io.input"sitin.txt"a,b,c=f:read(n,n,n)io.output"sitout.txt":write(math.min(a*b,c)," ",math.max(0,c-a*b))[/lua]
Sorry, you need to Log In to post a reply to this thread.