2

The problem is as follows:

Dick is d=12 years old. When we say this, we mean that it is at least twelve and not yet thirteen years since Dick was born.

Dick and Jane have three pets: Spot the dog, Puff the Cat, and Yertle the Turtle. Spot was s years old when Puff was born; Puff was p years old when Yertle was born; Spot was yy years old when Yertle was born. The sum of Spot’s age, Puff’s age, and Yertle’s age equals the sum of Dick’s age (d) and Jane’s age (j). How old are Spot, Puff, and Yertle?

The input's given are s,p,y,j and the output required is: spot's age, puff's age and yertle's age.

My solution was as follows:

import sys
import math

d = 12
for line in sys.stdin:
    line = [int(x) for x in line.strip("\n").split()]

    s = line[0]
    p = line[1]
    y = line[2]
    j = line[3]

    yertle = (d+j-y-p)/3.0
    difference = yertle - math.floor(yertle)
    if difference > 0.5:
        # for the 0.66666 cases
        spot = puff = int(math.ceil(yertle+p))
        yertle = int(math.floor(yertle))
    else:
        # for the 0.33333 cases
        yertle = int(math.floor(yertle))
        puff = yertle + p
        spot = d+j - puff - yertle

    print spot,puff,yertle

but it is incorrect on certain inputs such as: s=5, p=5, y=10, j=10. Because for those specifications the actual age's of the dogs are: spot=12.333, puff=7.333, yertle=2.333 but because we are doing integer division we get 12,7,2 instead. However, those results don't satisfy the $$spot + puff + yertle = dick + jane$$ rule. Does anyone have other ideas on where I am making a mistake or how i should've approached/solved this?

P.S. link for problem source

Giggs
  • 51
  • 5
  • spot=13.000, puff=7.333, yertle=2.333 satisfy inputs s=5, p=5, y=10, j=10. The actual age difference between Spot and Yertle is y + [0,1) – hk6279 Jun 02 '16 at 08:20
  • @hk6279 But isn't the difference between Puff and Yertle similarly, p + [0,1). In which case, we could say spot=12.333, puff = 8, yertle=2.333, right? How do we know to add the 0.6666 to spot and not puff or yertle? – Giggs Jun 02 '16 at 11:38
  • The same rule apply to (Puff and Yertle) and (Puff and Spot). But spot=12.333, puff = 8, yertle=2.333 is incorrect because 12.333-8 = 4.333. – hk6279 Jun 03 '16 at 01:23
  • @hk6279 Ah, I see what you mean. I tried to account for that in the newer version of my code (now pasted in the question). It, for example, correctly returns 8 8 6 on the inputs 0 1 1 10 (as it should right?) But I'm still getting a wrong answer verdict. Any thoughts? – Giggs Jun 03 '16 at 04:04

1 Answers1

2

Don't use float arithmetics, work with integers.

Let's denote D+J = DJ, Spot's age S, Puff's age P, Yertle's age Y

Let's Spot birthday time is zero, so Puff was born in interval [s, s+1), Yertle was born in interval [y, y+1). Current time is in interval [S, S+1).

enter image description here

If we look onto time line, we can see that

   S = y + Y
   or
   S = y + Y + 1
and 
   S = s + P
   or
   S = s + P + 1

age sum is

 DJ = S + Y + P = S + S - y + S - s - (0, 1, 2)

where (0,1,2) is possible addendum

 3 * S = DJ + y + s + (0,1,2)

We can see that right part must be divisible by 3, so next calulations depends on value

 M =  (DJ + y + s) modulo 3

case M = 0: (5 5 10 9)
     S = (DJ + y + s) / 3 = (21 + 15) / 3 = 12
     P = S - s = 12 - 5 = 7
     Y = S - y = 12 - 10 = 2

case M = 1: (5 5 10 10)
     here we should add 2 to make sum 37 divisible by 3
     S = (DJ + y + s + 2) / 3 = (22 + 15 + 2) / 3 = 13
     P = S - s  - 1 = 13 - 5 = 1 =  7
     Y = S - y  - 1 = 13 - 10 - 1 = 2

now more complex case M = 2 (5 5 11 10):
    here we should add 1 to make sum 38 divisible by 3 
    and solve - where use 1 - for P or for Y calculation?
    We can determine this evaluating s/p/y relation:
    if y = s + p + 1 then use 1 for Puff's age else for Yertle
    (because Puff's fraction is larger then Yertle's fraction, 
    she was born in the later year period)
    here 11 = 5 + 5 + 1, so
    S = (22 + 16 + 1) / 3 = 13
    Y = S - y = 13 - 11 = 2
    P = S - s - 1 = 13 - 5 - 1 = 7
MBo
  • 77,366
  • 5
  • 53
  • 86
  • Thank you for your comprehensive answer! Could you please elaborate on the last case, the one titled 'now more complex case...'; like where did the y = s + p + 1 identity come from, and I noticed you compute S first before Y, whereas I've been computing Y first, does this make a difference? – Giggs Jun 03 '16 at 15:34
  • Imagine that Spot was born in Jan 2000, Puff in October 2005, and Yertle in Feb 2011. s=5, y=11 and p=5! In May 2013 we have Puff age 7 (while 2013-2005=8). Added some comment in answer. – MBo Jun 03 '16 at 15:44
  • I tried implementing your algorithm and it worked! But please answer this for me: does it matter that you are solving Spot's age first and solving Y and P in relation to it? Because I was solving for Yertle first and solving S and P in relation to Y. – Giggs Jun 04 '16 at 15:39
  • I've composed equation for Spot because it seemed simpler for me. If you can make simple and correct approach for Y first - use it. – MBo Jun 04 '16 at 16:04