When is my piversary?

5

Since I want to celebrate this definitely not nerdy event, your job is to calculate the piversary (pi-anniversary) of a given date.

Input

Your code has to have a possibilty to input a date in the format DD/MM/YYYY. You can expect, that the input is valid and the year is 0000<YYYY<9996.

Output

You have to release two dates, the date in Pi months and the date in Pi years.

Rules

  • If your language hasn't a build in Pi, you have to make a variable which will be exact enough to calculate the dates, but it is not allowed to calculate the days separately and use fix numbers.
  • You have to take care, that the months have different amount of days, but you don't have to care about leap years (only if you want).
  • If you calculate the date, you have to add the three years/months first and then the left part in rounded days.
  • Your code has to accept any valid date and should be able to handle edge cases like 30/12/2000


Example:

Input: 12/04/2010

Calculate monthly piversary:
date +  3 months = 12/07/2010
date + (31*(Pi-3)) = date + 4.39 = date + 4 = 16/07/2010 //31 because July has 31 days

Calculate yearly piversary:
date + 3 years = 12/04/2013
date + (365*(Pi-3)) = date + 52 = 03/06/2013

Output:
16/07/2010,03/06/2013

May the shortest code win.

izlin

Posted 2014-07-31T09:35:09.277

Reputation: 1 497

7xkcd.com/1179 – Zaq – 2014-07-31T15:09:56.130

Are leading zeroes required in the output? – NinjaBearMonkey – 2014-07-31T15:16:51.930

@Zaq Awesome picture :) I will use it next time, I promise. – izlin – 2014-08-01T05:44:20.487

@hsl Yes they are. – izlin – 2014-08-01T05:44:41.303

Answers

3

Mathematica, 152 134 bytes

f[i_]:=DateList@{i,{d="Day",m="Month",y="Year"}}~DatePlus~{{3,#},{Round[#2*(Pi-3.)],d}}~DateString~{d,"/",m,"/",y}&@@@{{m,31},{y,365}}

Ungolfed

f[i_] :=
 DateString[
    DatePlus[
     DateList@{i,{d="Day",m="Month",y="Year"}}, 
     {{3, #}, {Round[#2*(Pi - 3.)], d}}
    ],
    {d, "/", m, "/", y}
 ] & @@@ {{m, 31}, {y, 365}}

This does handle leap years. It's also horribly long.

Martin Ender

Posted 2014-07-31T09:35:09.277

Reputation: 184 808

1

MATLAB: 141 157, 169

o='DD/mm/YY'
d=datenum(x,o) 
f=@(s,v) datestr(addtodate(addtodate(d,3,s),(pi-3)*v,'day'),o)
f('year',365)
f('month',eomday(year(d),month(d)))

Quite long and straightforward. It assumes the input to be in x.


Here is a version that is a few chars longer for those without the financial toolbox:

o='DD/mm/YY'
d=datenum(x,o) 
[y,m]=datevec(d)
f=@(s,v) datestr(addtodate(addtodate(d,3,s),(pi-3)*v,'day'),o)
f('year',365)
f('month',eomday(y,m))

Dennis Jaheruddin

Posted 2014-07-31T09:35:09.277

Reputation: 1 848

1

Groovy - 297 chars

Uses Joda Time. Does not handle leap years.

Golfed:

@Grab(group='joda-time', module='joda-time', version='2.3')
f=org.joda.time.format.DateTimeFormat.forPattern("dd/MM/yyyy")
t=f.parseDateTime(args[0])
p="plusDays"
m="Month"
println f.print(t."plus${m}s"(3)."$p"((t."dayOf$m"().maximumValue*(Math.PI-3)) as int))+","+f.print(t.plusYears(3)."$p"(52))

Sample run:

$ groovy Pi.groovy 12/04/2010
16/07/2010,03/06/2013

$ groovy Pi.groovy 30/12/2000
03/04/2001,20/02/2004

Ungolfed:

@Grab(group='joda-time', module='joda-time', version='2.3')

f = org.joda.time.format.DateTimeFormat.forPattern("dd/MM/yyyy")

t = f.parseDateTime(args[0])

println f.print(t.plusMonths(3).plusDays((t.dayOfMonth().maximumValue*(Math.PI-3)) as int)) 
        + "," +
        f.print(t.plusYears(3).plusDays(52))

Michael Easter

Posted 2014-07-31T09:35:09.277

Reputation: 585