python - The D+J programming challenge from Kattis -
the problem follows:
dick d=12 years old. when this, mean @ least twelve , not yet thirteen years since dick born.
dick , jane have 3 pets: spot dog, puff cat, , yertle turtle. spot s years old when puff born; puff p years old when yertle born; spot yy years old when yertle born. sum of spot’s age, puff’s age, , yertle’s age equals sum of dick’s age (d) , jane’s age (j). how old spot, puff, , yertle?
the input's given s,p,y,j , output required is: spot's age, puff's age , yertle's age.
my solution follows:
import sys import math d = 12 line in sys.stdin: line = [int(x) 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: # 0.66666 cases spot = puff = int(math.ceil(yertle+p)) yertle = int(math.floor(yertle)) else: # 0.33333 cases yertle = int(math.floor(yertle)) puff = yertle + p spot = d+j - puff - yertle print spot,puff,yertle
but incorrect on inputs such as: s=5, p=5, y=10, j=10. because specifications actual age's of dogs are: spot=12.333, puff=7.333, yertle=2.333 because doing integer division 12,7,2 instead. however, results don't satisfy $$spot + puff + yertle = dick + jane$$ rule. have other ideas on making mistake or how should've approached/solved this?
p.s. link problem source
don't use float arithmetics, work integers.
let's denote d+j = dj
, spot's age s
, puff's age p
, yertle's age y
let's spot birthday time zero, puff born in interval [s, s+1)
, yertle born in interval [y, y+1)
. current time in interval [s, s+1)
.
if onto time line, can see
s = y + y or s = y + y + 1 , 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) possible addendum
3 * s = dj + y + s + (0,1,2)
we can see right part must divisible 3, 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 should add 2 make sum 37 divisible 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 more complex case m = 2 (5 5 11 10): here should add 1 make sum 38 divisible 3 , solve - use 1 - p or y calculation? can determine evaluating s/p/y relation: if y = s + p + 1 use 1 puff's age else yertle (because puff's fraction larger yertle's fraction, born in later year period) here 11 = 5 + 5 + 1, s = (22 + 16 + 1) / 3 = 13 y = s - y = 13 - 11 = 2 p = s - s - 1 = 13 - 5 - 1 = 7