5
\$\begingroup\$

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.

\$\endgroup\$
  • 6
    \$\begingroup\$ xkcd.com/1179 \$\endgroup\$ – Zaq Jul 31 '14 at 15:09
  • \$\begingroup\$ Are leading zeroes required in the output? \$\endgroup\$ – NinjaBearMonkey Jul 31 '14 at 15:16
  • \$\begingroup\$ @Zaq Awesome picture :) I will use it next time, I promise. \$\endgroup\$ – izlin Aug 1 '14 at 5:44
  • \$\begingroup\$ @hsl Yes they are. \$\endgroup\$ – izlin Aug 1 '14 at 5:44
3
\$\begingroup\$

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.

\$\endgroup\$
1
\$\begingroup\$

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))
\$\endgroup\$
1
\$\begingroup\$

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))
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.