Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   NPC Scripting (https://forums.graalonline.com/forums/forumdisplay.php?f=8)
-   -   Programming Exercise #8: Parking (https://forums.graalonline.com/forums/showthread.php?t=134265002)

xAndrewx 11-12-2011 09:51 AM

Quote:

Originally Posted by Tolnaftate2004 (Post 1673960)
The example I gave was wrong, unfortunately. I can't edit that post. it should be 1.666...
The number of cars that can fit in a lot of size 4 is 2. No matter where the first car parks, there is always room for the second car.

kewl

Tolnaftate2004 11-21-2011 09:26 PM

Alright, this thread has been dead for long enough...

Like I said before, the fact that the lot is circular is important. However, let us consider the first car. With probability 1 it parks somewhere in the lot (since it's empty). Then we can henceforth consider the lot to be linear (and 1 car-length shorter) with one end being the front of the first car and the other the end of the first car. And we have 1 car in our lot. So,

f(N; circular) = 1 + f(N-2; linear).

So now we consider the second car and a lot of N-2. Somewhere in this lot, the car will park and split our lot into 2 even further smaller lots. If we number the units 0 ... N-3, let us call the smaller unit number that the second car occupies i. After parking, there are lots of size i and N-4-i. So,

f(N-2; linear | car 2 parks in unit i) = 1 + f(i; linear) + f(N-4-i; linear).

For those of you who know your probability, you'll know that

P(Y) = P(Y | X = x1) * P(X = x1) + ... + P(Y | X = xn) * P(X = xn).

And for those of you who don't, P is the probability and that | means "given".

So, we apply this rule to the above:

f(N-2; linear) = 1 + (f(0; linear) + f(N-4; linear)) * P(i = 0) + ... + (f(N-4; linear) + f(0; linear)) * P(i = N-3).

Of course, P(i = x) = 1/(N-3), so we can boil this down to

f(N; linear) = 1 + 2/(N-1)*(f(0) + ... f(N-2)).

When applied, f(100; circular) = 43.233235838...

However, the recursion grows with N!! (repeat !s as many times as a car-length... :)), so to speed up this calculation, we use memoization.

PHP Code:

function f(n) {
  if (
2) return 0;
  
temp.f.("lot_" n);
  if (
r) return r;
  
temp.0temp.0;
  for (; 
1;)
    
+= f(i++);
  
/ (1) * v;
  
f.("lot_" ) = v;
  return 
v;


I've looked at the case where cars can park at non-integer positions within the lot. The answer would be smaller than the discrete case, so I have a sneaking suspicion that Mark Sir Link got it right in that case (but it's a pretty hard problem to solve exactly). Brownie points for you.

Tricxta 11-21-2011 10:20 PM

Looks more like a maths lesson rather then a programming solution. Pretty cool though since I see what you did there, I'll rep you for that.

xAndrewx 11-21-2011 10:40 PM

gj on the four people that entered.

oo_jazz_oo 11-21-2011 11:26 PM

I'm bad at conceptualizing maths. :(

Can't we have a programming excercise thats like....useful. :3

Tolnaftate2004 11-22-2011 04:26 AM

Quote:

Originally Posted by oo_jazz_oo (Post 1674967)
... useful

Explain.

xAndrewx 11-22-2011 08:55 PM

Quote:

Originally Posted by Tolnaftate2004 (Post 1675008)
Explain.

I really love you and your knowledge, it's great. (No sarcasm)

but will you ever use this on Graal?

Tolnaftate2004 11-22-2011 10:24 PM

Quote:

Originally Posted by xAndrewx (Post 1675065)
I really love you and your knowledge, it's great. (No sarcasm)

but will you ever use this on Graal?

Probably not to figure out how many cars are parked in my lot, but for something which uses a similar algorithm (e.g. baddy decision making).

DustyPorViva 11-22-2011 11:02 PM

I can understand this is to challenge peoples problem-solving skills, but it doesn't seem like many scripters are interested in it because they end up seeming like something you'd see in a math class finals paper.

I think what Jazz was saying was to have the challenges that would relate more to what a scripter would have to do while scripting a server or something. For example scripting a physics movement system for a sidescrolling main character, or perhaps scripting a physics collision system.

Tolnaftate2004 11-22-2011 11:32 PM

Quote:

Originally Posted by DustyPorViva (Post 1675072)
I think what Jazz was saying was to have the challenges that would relate more to what a scripter would have to do while scripting a server or something. For example scripting a physics movement system for a sidescrolling main character, or perhaps scripting a physics collision system.

HR tried that, and it was the least-active exercise to date.

DustyPorViva 11-22-2011 11:55 PM

Quote:

Originally Posted by Tolnaftate2004 (Post 1675078)
HR tried that, and it was the least-active exercise to date.

Honestly all the exercises seem fairly inactive, it seems wrong to criticize that challenge for it.

Emera 11-23-2011 12:15 AM

How the hell are all of you people this good at maths. I wish I was this good. I question the answer when I make RC echo 4 + 4
You make me feel dumb lol. Do any of you have an A level in further maths?

Tricxta 11-23-2011 01:09 AM

Quote:

Originally Posted by Emera (Post 1675085)
Do any of you have an A level in further maths?

what do you mean? I know mathematical induction :rolleyes: does that count?


All times are GMT +2. The time now is 06:48 AM.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.