-2

Is there a closed form solution for the cookie cutter problem? For reference this is the: google page

*Updated to include problem statement

Problem

In this problem, you start with 0 cookies. You gain cookies at a rate of 2 cookies per second, by clicking on a giant cookie. Any time you have at least C cookies, you can buy a cookie farm. Every time you buy a cookie farm, it costs you C cookies and gives you an extra F cookies per second.

Once you have X cookies that you haven't spent on farms, you win! Figure out how long it will take you to win if you use the best possible strategy.

Example

Suppose C=500.0, F=4.0 and X=2000.0. Here's how the best possible strategy plays out:

You start with 0 cookies, but producing 2 cookies per second. After 250 seconds, you will have C=500 cookies and can buy a farm that produces F=4 cookies per second. After buying the farm, you have 0 cookies, and your total cookie production is 6 cookies per second. The next farm will cost 500 cookies, which you can buy after about 83.3333333 seconds. After buying your second farm, you have 0 cookies, and your total cookie production is 10 cookies per second. Another farm will cost 500 cookies, which you can buy after 50 seconds. After buying your third farm, you have 0 cookies, and your total cookie production is 14 cookies per second. Another farm would cost 500 cookies, but it actually makes sense not to buy it: instead you can just wait until you have X=2000 cookies, which takes about 142.8571429 seconds.

Total time: 250 + 83.3333333 + 50 + 142.8571429 = 526.1904762 seconds.

Notice that you get cookies continuously: so 0.1 seconds after the game starts you'll have 0.2 cookies, and π seconds after the game starts you'll have 2π cookies.

Salomon Zhang
  • 1,553
  • 3
  • 23
  • 41
GeneralBecos
  • 2,476
  • 2
  • 22
  • 32

2 Answers2

0

No. It won't have any closed form.

The algorithm goes like this,

Wait for collecting C many cookies. If you have C many cookies, buy a new farm if

 (X-C)/R >= X/(R+F) --- (i)

else DON'T buy any farm and continue collecting cookies till you have X many cookies.

eqn (i)
LHS is the time for the collecting (X-C) many cookies [collected C many cookies already which I did not spend on buying a farm] with current collecting rate.

RHS is the time for collecting X many cookies with the increased collecting rate.

From the equation we have, R <= F(X-C)/C

So the answer would be,

C/2 + C/2+F + C/2+2F + C/2+3F + ... + C/2+NF + X/2+NF [2 + NF <= F(X-C)/C]
= C(1/2 + 1/2+F + 1/2+2F + ... + 1/2+NF) + X/2+NF = A

suppose we have a closed form to calculate A

then for F = 1, C = 1, X = K

we have A = (1/2 + 1/3 + .. + 1/2+N) + X/2+N where 2+N <= (K-1)

=> (1/2 + 1/3 + .. + 1/2+N) = A - X/2+N which would have a closed form too.

But, the finite sum of series {1/N} does not have any closed form. So neither this has.

rnbguy
  • 1,369
  • 1
  • 10
  • 28
  • I see where you are going with this... though, you cannot generate a closed form for A (which I agree is impossible), you could build one for A + X/(k+n) (where k is 2 like in this case) – GeneralBecos Apr 17 '14 at 15:47
  • *I meant, could you build one for A + X/(k+n) .. :) – GeneralBecos Apr 17 '14 at 21:03
0

I guess there is a closed form:

Note that you have to compare (since it is optimal either to buy another farm as long as you can or never to buy another one)

(X-C)/(2+F(n-1)) with X/(2+Fn), where n is the number of farms.

Therefore, you have just to find n such that solves

(X-C)/(2+F(n-1)) = X/(2+Fn).

n=(FX-2C)/CF

If n is positive, it means that your solution is Floor(n). Otherwise, n=0 is your solution.

PS: "2" above can be replaced by initial production rate.

DanielTheRocketMan
  • 3,199
  • 5
  • 36
  • 65