Problem:


マヤの大予言

真也君はテレビで「マヤの大予言！2012年で世界が終る？」という番組を見ました。結局、世界が終るかどうかはよくわかりませんでしたが、番組で紹介されていたマヤの「長期暦」という暦に興味を持ちました。その番組では以下のような説明をしていました。

マヤ長期暦は、右の表のような単位からなる、全部で13バクトゥン(187万2000日)で構成される非常に長い暦である。ある計算法では、この暦は紀元前3114年8月11日に始まり2012年12月21日に終わると考えられていて、このため今年の12月21日で世界が終るという説が唱えられている。しかし、13バクトゥンで１サイクルとなり、今の暦が終わったら新しいサイクルに入るだけという考えもある。

１キン＝１日

１ウィナル＝２０キン

１トゥン＝１８ウィナル

１カトゥン＝２０トゥン

１バクトゥン＝２０カトゥン

 

「ぼくの二十歳の誕生日はマヤ暦だと何日になるのかな？」真也君はいろいろな日をマヤ長期暦で表してみたくなりました。

では、真也君の代わりに、西暦とマヤ長期暦とを相互変換するプログラムを作成してください。

入力

入力は複数のデータセットからなる。入力の終わりは#１つの行で示される。各データセットは以下の形式で与えられる。

b.ka.t.w.ki

または

y.m.d

データセットは１つの文字列を含む１行からなる。b.ka.t.w.ki はマヤ長期暦の日付、y.m.d は西暦の日付である。与えられる単位は以下の通りである。

マヤ長期暦

b
バクトゥン
(0 ≤ b< 13)

ka
カトゥン
(0 ≤ ka < 20)

t
トゥン
(0 ≤ t< 20)

w
ウィナル
(0 ≤ w < 18)

ki
キン
(0 ≤ ki < 20)

西暦

y
年
(2012 ≤ y ≤ 10,000,000)

m
月
(1 ≤ m ≤ 12)

d
日
(1 ≤ d ≤ 31)

西暦の日の最大値は、大の月、小の月、うるう年かどうかで変わる(うるう年は 4 の倍数の年のうち、100 で割り切れない年か、400 で割り切れる年である)。マヤ長期暦の日付の範囲は 0.0.0.0.0 から 12.19.19.17.19 までとする。ただし、マヤ長期暦の 0.0.0.0.0 は西暦の 2012.12.21 に対応する。また、西暦の日付の範囲は 2012.12.21から 10000000.12.31 までとする。

データセットの数は 500 を超えない。

出力

入力が西暦のときはマヤ長期暦を、マヤ長期暦のときは西暦を、それぞれ入力と同じ形式で出力する。
入力された西暦を換算した結果、マヤ長期暦の次のサイクルに入った場合でも b.ka.t.w.ki の形式で出力してよい。

入力例

2012.12.31
2.12.16.14.14
7138.5.13
10.5.2.1.5
10000000.12.31
#

出力例

0.0.0.0.10
3054.8.15
0.0.0.0.10
6056.2.29
8.19.3.13.2



Sample input:
2012.12.31
2.12.16.14.14
7138.5.13
10.5.2.1.5
10000000.12.31
#


Sample output:
0.0.0.0.10
3054.8.15
0.0.0.0.10
6056.2.29
8.19.3.13.2

Submission status: Wrong Answer

Solution code:
KI = 1
W = 20 * KI
T = 18 * W
KA = 20 * T
B = 20 * KA
MAX_MAYA = 12 * B + 19*KA + 19*T + 17*W + 19

month = (31,28,31,30,31,30,31,31,30,31,30,31)


def is_uruu(y):
    return y % 4 == 0 and (y % 100 != 0 or y % 400 == 0)
def y_d(y):
    return 365 if not is_uruu(y) else 366
def m_d(y,m):
    return 29 if (m == 2 and is_uruu(y)) else month[m-1]

while 1:
    inp = input().split('.')
    if inp[0] == '#':break

    inp = list(map(int,inp))
    if len(inp) == 5:#maya
        val = inp[0] * B + inp[1] * KA + inp[2] * T + inp[3] * W + inp[4] * KI
        y,m,d = 2012,12,21
        if val <= 10:
            print('%d.%d.%d' % (y,m,d+10))
        else:
            val -=11
            y,m,d = 2013,1,1

            while val >= y_d(y): 
                val -= y_d(y)
                y += 1
            while val >= m_d(y,m):
                val -= m_d(y,m)
                m += 1

            d += val
            print('%d.%d.%d' % (y,m,d))
    else:
        if inp[0] == 2012:
            print('0.0.0.0.%d' % (inp[2] - 21))
        else:
            val = 11
            for y in range(2013,inp[0]):
                val += y_d(y)
            for m in range(1,inp[1]):
                val += m_d(inp[0],m)
            val += inp[2]-1
            if val > MAX_MAYA:
                val = val % (MAX_MAYA+1)
            val = val % (MAX_MAYA+1)
            b=val // B
            val %= B
            ka = val // KA
            val %= KA
            t = val //T
            val %= T
            w = val //W
            val %= W
            print('%d.%d.%d.%d.%d'%(b,ka,t,w,val))

Submission status: Wrong Answer

Solution code:
KI = 1
W = 20 * KI
T = 18 * W
KA = 20 * T
B = 20 * KA
#MAX_MAYA = 12 * B + 19*KA + 19*T + 17*W + 19
MAX_MAYA = 1872000 
month = (31,28,31,30,31,30,31,31,30,31,30,31)

def get_days(y,m,d):
    if m <= 2:
        y -= 1
        m += 12
    dy = 365 * (y - 1)
    c = y/100
    dl = (y >> 2) - c + (c /4)
    dm = (m * 979 -1033) >> 5
    return dy + dl + dm + d -1

def is_uruu(y):
    return y % 4 == 0 and (y % 100 != 0 or y % 400 == 0)
def y_d(y):
    return 365 if not is_uruu(y) else 366
def m_d(y,m):
    return 29 if (m == 2 and is_uruu(y)) else month[m-1]

base = get_days(2012,12,21)
while 1:
    inp = input().split('.')
    if inp[0] == '#':break

    inp = list(map(int,inp))
    if len(inp) == 5:#maya
        val = inp[0] * B + inp[1] * KA + inp[2] * T + inp[3] * W + inp[4] * KI
        y,m,d = 2012,12,21
        if val <= 10:
            print('%d.%d.%d' % (y,m,d+val))
        else:
            val -=11
            y,m,d = 2013,1,1

            while val >= y_d(y): 
                val -= y_d(y)
                y += 1
            while val >= m_d(y,m):
                val -= m_d(y,m)
                m += 1

            d += val
            print('%d.%d.%d' % (y,m,d))
    else:
        if inp[0] == 2012:
            print('0.0.0.0.%d' % (inp[2] - 21))
        else:
            val = get_days(inp[0],inp[1],inp[2]) - base
            val = val % (MAX_MAYA)
            b=val // B
            val %= B
            ka = val // KA
            val %= KA
            t = val //T
            val %= T
            w = val //W
            val %= W
            print('%d.%d.%d.%d.%d'%(b,ka,t,w,val))

Submission status: Wrong Answer

Solution code:
KI = 1
W = 20 * KI
T = 18 * W
KA = 20 * T
B = 20 * KA
#MAX_MAYA = 12 * B + 19*KA + 19*T + 17*W + 19
MAX_MAYA = 1872000 
month = (31,28,31,30,31,30,31,31,30,31,30,31)

def get_days(y,m,d):
    if m <= 2:
        y -= 1
        m += 12
    dy = 365 * (y - 1)
    c = y/100
    dl = (y >> 2) - c + (c /4)
    dm = (m * 979 -1033) >> 5
    return dy + dl + dm + d -1

def is_uruu(y):
    return y % 4 == 0 and (y % 100 != 0 or y % 400 == 0)
def y_d(y):
    return 365 if not is_uruu(y) else 366
def m_d(y,m):
    return 29 if (m == 2 and is_uruu(y)) else month[m-1]

base = get_days(2012,12,21)
from datetime import date,timedelta
st = date(2012,12,21)
while 1:
    inp = input().split('.')
    if inp[0] == '#':break

    inp = list(map(int,inp))
    if len(inp) == 5:#maya
        d = inp
        r = d[0]
        r *= 20
        r += d[1]
        r *= 20
        r += d[2]
        r *= 18
        r += d[3]
        r *= 20
        r += d[4]
        ed = st+timedelta(days=r)
        print(str(ed.year)+'.'+str(ed.month)+'.'+str(ed.day))
#        val = inp[0] * B + inp[1] * KA + inp[2] * T + inp[3] * W + inp[4] * KI
#        y,m,d = 2012,12,21
#        if val <= 10:
#            print('%d.%d.%d' % (y,m,d+val))
#        else:
#            val -=11
#            y,m,d = 2013,1,1
#
#            while val >= y_d(y): 
#                val -= y_d(y)
#                y += 1
#            while val >= m_d(y,m):
#                val -= m_d(y,m)
#                m += 1
#
#            d += val
#            print('%d.%d.%d' % (y,m,d))
    else:
        if inp[0] == 2012:
            print('0.0.0.0.%d' % (inp[2] - 21))
        else:
            val = get_days(inp[0],inp[1],inp[2]) - base
            val = val % (MAX_MAYA)
            b=val // B
            val %= B
            ka = val // KA
            val %= KA
            t = val //T
            val %= T
            w = val //W
            val %= W
            print('%d.%d.%d.%d.%d'%(b,ka,t,w,val))

Submission status: Wrong Answer

Solution code:
import datetime

def is_leap_year(y):
    return y % 4 == 0 and (y % 100 != 0 or y % 400 == 0)

def leap_year_count(target_y,benchmark_y):
    target = target_y // 4 - target_y // 100 + target_y // 400
    benchmark = benchmark_y // 4 - benchmark_y // 100 + benchmark_y // 400
    return target - benchmark

def ad2mayan(y, m, d):
    diff = 0
    if datetime.MAXYEAR < y:
        day_of_2012 = (datetime.date(2013, 1, 1) - datetime.date(2012, 12, 21)).days
        day_of_the_year = (datetime.date(1, m, d) - datetime.date(1, 1, 1)).days if is_leap_year(y) else (datetime.date(4, m, d) - datetime.date(4, 1, 1)).days
        
        days = (y - 2013) * 365 + leap_year_count(y, 2013)

        diff = day_of_2012 + days + day_of_the_year
    else:
        benchmark = datetime.date(2012, 12, 21)
        diff = (datetime.date(y, m, d) - benchmark).days
    
    ki, diff = diff % 20, diff // 20
    w, diff = diff % 18, diff // 18
    t, diff = diff % 20, diff // 20
    ka, diff = diff % 20, diff // 20
    b = diff % 13
    
    return b,ka,t,w,ki

def mayan2ad(b, ka, t, w, ki):
    ka += b * 20
    t += ka * 20
    w += t * 18
    ki += w * 20
    time = datetime.date(2012, 12, 21) + datetime.timedelta(ki)
    return time.year, time.month, time.day

import sys
f = sys.stdin

for line in f:
    if line[0] == '#':
        break

    t = list(map(int, line.split('.')))
    if len(t) == 3:
        print('.'.join(map(str, ad2mayan(*t))))
    elif len(t) == 5:
        print('.'.join(map(str, mayan2ad(*t))))

Submission status: Wrong Answer

Solution code:
import datetime

def is_leap_year(y):
    return y % 4 == 0 and (y % 100 != 0 or y % 400 == 0)

def leap_year_count(target_y,benchmark_y):
    target = target_y // 4 - target_y // 100 + target_y // 400
    benchmark = benchmark_y // 4 - benchmark_y // 100 + benchmark_y // 400
    return target - benchmark

def ad2mayan(y, m, d):
    diff = 0
    if datetime.MAXYEAR < y:
        day_of_2012 = (datetime.date(2013, 1, 1) - datetime.date(2012, 12, 21)).days
        day_of_the_year = 0
        if is_leap_year(y):
            day_of_the_year = (datetime.date(2, m, d) - datetime.date(1, 12, 31)).days
        else:
            day_of_the_year = (datetime.date(4, m, d) - datetime.date(3, 12, 31)).days
        
        days = (y - 2013) * 365 + leap_year_count(y - 1, 2013)
        print(day_of_2012,days,day_of_the_year)

        diff = day_of_2012 + days + day_of_the_year
    else:
        benchmark = datetime.date(2012, 12, 21)
        diff = (datetime.date(y, m, d) - benchmark).days
    
    ki, diff = diff % 20, diff // 20
    w, diff = diff % 18, diff // 18
    t, diff = diff % 20, diff // 20
    ka, diff = diff % 20, diff // 20
    b = diff % 13
    
    return b,ka,t,w,ki

def mayan2ad(b, ka, t, w, ki):
    ka += b * 20
    t += ka * 20
    w += t * 18
    ki += w * 20
    time = datetime.date(2012, 12, 21) + datetime.timedelta(ki)
    return time.year, time.month, time.day

import sys
f = sys.stdin

for line in f:
    if line[0] == '#':
        break
    t = list(map(int, line.split('.')))
    if len(t) == 3:
        print('.'.join(map(str, ad2mayan(*t))))
    elif len(t) == 5:
        print('.'.join(map(str, mayan2ad(*t))))

Submission status: Wrong Answer

Solution code:
import datetime

def is_leap_year(y):
    return y % 4 == 0 and (y % 100 != 0 or y % 400 == 0)

def leap_year_count(target_y,benchmark_y):
    target = target_y // 4 - target_y // 100 + target_y // 400
    benchmark = benchmark_y // 4 - benchmark_y // 100 + benchmark_y // 400
    return target - benchmark

def ad2mayan(y, m, d):
    diff = 0
    if datetime.MAXYEAR < y:
        day_of_2012 = (datetime.date(2013, 1, 1) - datetime.date(2012, 12, 21)).days
        day_of_the_year = 0
        if is_leap_year(y):
            day_of_the_year = (datetime.date(2, m, d) - datetime.date(1, 12, 31)).days
        else:
            day_of_the_year = (datetime.date(4, m, d) - datetime.date(3, 12, 31)).days
        
        days = (y - 2013) * 365 + leap_year_count(y - 1, 2013)

        diff = day_of_2012 + days + day_of_the_year
    else:
        benchmark = datetime.date(2012, 12, 21)
        diff = (datetime.date(y, m, d) - benchmark).days
    
    ki, diff = diff % 20, diff // 20
    w, diff = diff % 18, diff // 18
    t, diff = diff % 20, diff // 20
    ka, diff = diff % 20, diff // 20
    b = diff % 13
    
    return b,ka,t,w,ki

def mayan2ad(b, ka, t, w, ki):
    ka += b * 20
    t += ka * 20
    w += t * 18
    ki += w * 20
    time = datetime.date(2012, 12, 21) + datetime.timedelta(ki)
    return time.year, time.month, time.day

import sys
f = sys.stdin

for line in f:
    if line[0] == '#':
        break
    t = list(map(int, line.split('.')))
    if len(t) == 3:
        print('.'.join(map(str, ad2mayan(*t))))
    elif len(t) == 5:
        print('.'.join(map(str, mayan2ad(*t))))

Submission status: Wrong Answer

Solution code:
# Aizu Problem 0261: Mayan Crucial Prediction

import sys, math, os, datetime

# read input:
PYDEV = os.environ.get('PYDEV')
if PYDEV=="True":
    sys.stdin = open("sample-input.txt", "rt")

dpm = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] # day per month

 
def get_day(year, month, day):
    if month <= 2:
        year -= 1
        month += 12
    dy = 365 * (year - 1)
    c = year // 100
    dl = (year >> 2) - c + (c >> 2)
    dm = (month * 979 - 1033) >> 5
    return dy + dl + dm + day - 1

def from_day(n):
    a = n + 32044;
    b = (4 * n + 3) // 146097
    c = a - (146097 * b) // 4
    d = (4 * c + 3) // 1461
    e = c - (1461 * d) // 4
    m = (5 * e + 2) // 153
    m1 = m // 10
    day = e - (153 * m + 2) // 5 + 1
    month = m + 3 - 12 * m1
    year = 100 * b + d - 4800 + m1
    if year <= 0:
        year = -year + 1
    return year, month, day


offset = get_day(2012, 12, 21)
maya= [144000, 7200, 360, 20, 1]
    
while True:
    inp = input().strip()
    if inp == "#":
        break
    inp = [int(_) for _ in inp.split('.')]
    if len(inp) == 3:
        # christian to mayan:
        n = (get_day(inp[0], inp[1], inp[2]) - offset) % 1872000
        out = ""
        for k in range(4):
            out += str(n // maya[k]) + '.'
            n %= maya[k]
        out += str(n)
        print(out)
    else:
        # mayan to christian:
        n = sum([maya[k] * inp[k] for k in range(5)])
        #date = datetime.date.fromordinal(offset + n + 1)
        year, month, day = from_day(n + 2456283 + 1)
        print(str(year) + "." + str(month) + "." + str(day))

Submission status: Wrong Answer

Solution code:
m1 = [31,28,31,30,31,30,31,31,30,31,30,31]
m2 = [31,29,31,30,31,30,31,31,30,31,30,31]
while 1:
    try:d = list(map(int, input().split(".")))
    except:break
    if len(d) == 3:
        days = -734858
        days += (d[0] - 1) * 365 + (d[0] - 1) // 4 - (d[0] - 1) // 100 + (d[0] - 1) // 400
        if d[0] % 4 == 0 and (d[0] // 100 != 0 or d[0] // 400 == 0):days += sum(m2[:d[1] - 1])
        else:days += sum(m1[:d[1] - 1])
        days += d[2]
        maya = [0,0,0,0,0]
        maya[0] = days // 144000 % 13
        maya[1] = days // 7200 % 20
        maya[2] = days // 360 % 20
        maya[3] = days // 20 % 18
        maya[4] = days % 20
        print(".".join(map(str, maya)))
    else:
        days = d[0] * 144000 + d[1] * 7200 + d[2] * 360 + d[3] * 20 + d[4] + 734858
        sei = [1,0,0]
        sei[0] += days // 146097 * 400
        days %= 146097
        sei[0] += days // 36524 * 100
        days %= 36524
        sei[0] += days // 1461 * 4
        days %= 1461
        sei[0] += days // 365
        days %= 365
        if sei[0] % 4 == 0 and (sei[0] // 100 != 0 or sei[0] // 400 == 0):
            for i in range(12):
                if sum(m2[:i + 1]) >= days:
                    sei[1] = i + 1
                    days -= sum(m2[:i])
                    break
        else:
            for i in range(12):
                if sum(m1[:i + 1]) >= days:
                    sei[1] = i + 1
                    days -= sum(m1[:i])
                    break
        sei[2] = days
        print(".".join(map(str, sei)))

Submission status: Wrong Answer

Solution code:
m1 = [31,28,31,30,31,30,31,31,30,31,30,31]
m2 = [31,29,31,30,31,30,31,31,30,31,30,31]
while 1:
    try:d = list(map(int, input().split(".")))
    except:break
    if len(d) == 3:
        days = -734858
        days += (d[0] - 1) * 365 + (d[0] - 1) // 4 - (d[0] - 1) // 100 + (d[0] - 1) // 400
        if d[0] % 4 == 0 and (d[0] // 100 != 0 or d[0] // 400 == 0):days += sum(m2[:d[1] - 1])
        else:days += sum(m1[:d[1] - 1])
        days += d[2]
        maya = [0,0,0,0,0]
        maya[0] = days // 144000 % 13
        maya[1] = days // 7200 % 20
        maya[2] = days // 360 % 20
        maya[3] = days // 20 % 18
        maya[4] = days % 20
        print(".".join(map(str, maya)))
    else:
        days = d[0] * 144000 + d[1] * 7200 + d[2] * 360 + d[3] * 20 + d[4] + 734858
        sei = [1,0,0]
        sei[0] += days // 146097 * 400
        days %= 146097
        sei[0] += days // 36524 * 100
        days %= 36524
        sei[0] += days // 1461 * 4
        days %= 1461
        sei[0] += days // 365
        days %= 365
        if days == 0:
            sei[0] -= 1
            if sei[0] % 4 == 0 and (sei[0] // 100 != 0 or sei[0] // 400 == 0):days += 366
            else:days += 365
        if sei[0] % 4 == 0 and (sei[0] // 100 != 0 or sei[0] // 400 == 0):
            for i in range(12):
                if sum(m2[:i + 1]) >= days:
                    sei[1] = i + 1
                    days -= sum(m2[:i])
                    break
        else:
            for i in range(12):
                if sum(m1[:i + 1]) >= days:
                    sei[1] = i + 1
                    days -= sum(m1[:i])
                    break
        sei[2] = days
        print(".".join(map(str, sei)))

Submission status: Wrong Answer

Solution code:
m1 = [31,28,31,30,31,30,31,31,30,31,30,31]
m2 = [31,29,31,30,31,30,31,31,30,31,30,31]
while 1:
    try:d = list(map(int, input().split(".")))
    except:break
    if len(d) == 3:
        days = -734858
        days += (d[0] - 1) * 365 + (d[0] - 1) // 4 - (d[0] - 1) // 100 + (d[0] - 1) // 400
        if d[0] % 4 == 0 and (d[0] % 100 != 0 or d[0] % 400 == 0):days += sum(m2[:d[1] - 1])
        else:days += sum(m1[:d[1] - 1])
        days += d[2]
        maya = [0,0,0,0,0]
        maya[0] = days // 144000 % 13
        maya[1] = days // 7200 % 20
        maya[2] = days // 360 % 20
        maya[3] = days // 20 % 18
        maya[4] = days % 20
        print(".".join(map(str, maya)))
    else:
        days = d[0] * 144000 + d[1] * 7200 + d[2] * 360 + d[3] * 20 + d[4] + 734858
        sei = [1,0,0]
        sei[0] += days // 146097 * 400
        days %= 146097
        sei[0] += days // 36524 * 100
        days %= 36524
        sei[0] += days // 1461 * 4
        days %= 1461
        sei[0] += days // 365
        days %= 365
        if days == 0:
            sei[0] -= 1
            if sei[0] % 4 == 0 and (sei[0] // 100 != 0 or sei[0] // 400 == 0):days += 366
            else:days += 365
        if sei[0] % 4 == 0 and (sei[0] // 100 != 0 or sei[0] // 400 == 0):
            for i in range(12):
                if sum(m2[:i + 1]) >= days:
                    sei[1] = i + 1
                    days -= sum(m2[:i])
                    break
        else:
            for i in range(12):
                if sum(m1[:i + 1]) >= days:
                    sei[1] = i + 1
                    days -= sum(m1[:i])
                    break
        sei[2] = days
        print(".".join(map(str, sei)))

Submission status: Wrong Answer

Solution code:
m1 = [31,28,31,30,31,30,31,31,30,31,30,31]
m2 = [31,29,31,30,31,30,31,31,30,31,30,31]
while 1:
    try:d = list(map(int, input().split(".")))
    except:break
    if len(d) == 3:
        days = -734858
        days += (d[0] - 1) * 365 + (d[0] - 1) // 4 - (d[0] - 1) // 100 + (d[0] - 1) // 400
        if d[0] % 4 == 0 and (d[0] % 100 != 0 or d[0] % 400 == 0):days += sum(m2[:d[1] - 1])
        else:days += sum(m1[:d[1] - 1])
        days += d[2]
        maya = [0,0,0,0,0]
        maya[0] = days // 144000 % 13
        maya[1] = days // 7200 % 20
        maya[2] = days // 360 % 20
        maya[3] = days // 20 % 18
        maya[4] = days % 20
        print(".".join(map(str, maya)))
    else:
        days = d[0] * 144000 + d[1] * 7200 + d[2] * 360 + d[3] * 20 + d[4] + 734858
        sei = [1,0,0]
        sei[0] += days // 146097 * 400
        days %= 146097
        sei[0] += days // 36524 * 100
        days %= 36524
        sei[0] += days // 1461 * 4
        days %= 1461
        sei[0] += days // 365
        days %= 365
        if days == 0:
            sei[0] -= 1
            if sei[0] % 4 == 0 and (sei[0] % 100 != 0 or sei[0] % 400 == 0):days += 366
            else:days += 365
        if sei[0] % 4 == 0 and (sei[0] % 100 != 0 or sei[0] % 400 == 0):
            for i in range(12):
                if sum(m2[:i + 1]) >= days:
                    sei[1] = i + 1
                    days -= sum(m2[:i])
                    break
        else:
            for i in range(12):
                if sum(m1[:i + 1]) >= days:
                    sei[1] = i + 1
                    days -= sum(m1[:i])
                    break
        sei[2] = days
        print(".".join(map(str, sei)))

Submission status: Wrong Answer

Solution code:
from datetime import date, timedelta

st = date(2012, 12, 21)
while True:
    s = input()
    if s == '#':
        break
    d = list(map(int, s.split('.')))
    if len(d) == 3:
        r = 0
        while d[0] > 3000:
            d[0] -= 400
            r += 365*400+97

        ed = date(d[0], d[1], d[2])
        r += (ed-st).days
        ki = r%20
        r//=20
        w = r%18
        r//=18
        t = r%20
        r//=20
        ka = r%20
        r//=20
        b = r%13
        print(str(b)+'.'+str(ka)+'.'+str(t)+'.'+str(w)+'.'+str(ki))
    else:
        r = d[0]
        r *= 20
        r += d[1]
        r *= 20
        r += d[2]
        r *= 18
        r += d[3]
        r *= 20
        r += d[4]
        print((st+timedelta(days=r)).strftime("%Y.%m.%d"))

Submission status: Accepted

Solution code:
import datetime
from datetime import timedelta
from datetime import date

def ad2mayan(y, m, d):
    days = 0
    while datetime.MAXYEAR < y:
        days += 365 * 400 + 97
        y -= 400

    days += (date(y, m, d) - date(2012, 12, 21)).days
    
    ki, days = days % 20, days // 20
    w, days = days % 18, days // 18
    t, days = days % 20, days // 20
    ka, days = days % 20, days // 20
    b = days % 13
    
    return b,ka,t,w,ki

def mayan2ad(b, ka, t, w, ki):
    ka += b * 20
    t += ka * 20
    w += t * 18
    ki += w * 20
    dt = datetime.date(2012, 12, 21) + timedelta(ki)
    return dt.year, dt.month, dt.day

import sys
f = sys.stdin


for line in f:
    if line[0] == '#':
        break
    t = list(map(int, line.split('.')))
    
    if len(t) == 3:
        print('.'.join(map(str, ad2mayan(*t))))
    elif len(t) == 5:
        print('.'.join(map(str, mayan2ad(*t))))

Submission status: Accepted

Solution code:
from datetime import date,timedelta
a=date(2012,12,21)
while 1:
    try:s=list(map(int,input().split('.')))
    except:break
    d=0
    if len(s)<4:    
        while s[0]>3000:d+=365*400+97;s[0]-=400
        d+=(date(s[0],s[1],s[2])-a).days
        d,k=divmod(d,20)
        d,w=divmod(d,18)
        d,t=divmod(d,20)
        d,_k=divmod(d,20)
        b=d%13
        print('.'.join(map(str,[b,_k,t,w,k])))
    else:
        for i in range(len(s)-1):
            d+=s[i]
            d*=18 if i==2 else 20
        d+=s[4]
        d=a+timedelta(days=d)
        print('.'.join(map(str,[d.year,d.month,d.day])))

Submission status: Accepted

Solution code:
from datetime import date,timedelta
a=date(2012,12,21)
while 1:
    try:s=list(map(int,input().split('.')))
    except:break
    d=0
    if len(s)<4:    
        while s[0]>3000:d+=365*400+97;s[0]-=400
        d+=(date(s[0],s[1],s[2])-a).days
        d,k=divmod(d,20)
        d,w=divmod(d,18)
        d,t=divmod(d,20)
        d,_k=divmod(d,20)
        d%=13
        print('.'.join(map(str,[d,_k,t,w,k])))
    else:
        for i in range(len(s)-1):
            d+=s[i]
            d*=18 if i==2 else 20
        d+=s[4]
        d=a+timedelta(days=d)
        print('.'.join(map(str,[d.year,d.month,d.day])))

Submission status: Accepted

Solution code:
#-*- coding: utf-8 _*_
import datetime
 
def is_leap(y):
    if y%400==0:   return True
    if y%100==0:   return False
    if y%4==0:     return True
    return False
 
def get_day(y,m,d):
    if m<=2:
        y-=1
        m+=12
    dy = 365*(y-1)
    c = y/100
    dl=(y>>2)-c+(c>>2)
    dm=(m*979-1033)>>5
    return dy+dl+dm+d-1
 
def solve(line):
    ipt = map(int, line.split("."))
#     print ipt
    if len(ipt) >3:
        n=0
        for i in range(5):  n+=ipt[i]*mm[i]
        date = datetime.date.fromordinal(offset+n+1)
        print str(date.year)+"."+str(date.month)+"."+str(date.day)
    else:
        n=get_day(*ipt)-offset
#         print n
        n%=1872000
        ret=""
        for i in range(4):
            ret+=str(n/mm[i])+"."
            n%=mm[i]
        ret+=str(n)
        print ret
 
if __name__=='__main__':
    # const
    mm=(144000,7200,360,20,1)
    w1=(0,0,31,59,90,120,151,181,212,243,273,304,334,365)
    w2=(0,0,31,60,91,121,152,182,213,244,274,305,335,366)
    offset = get_day(2012,12,21)
#     print datetime.MAXYEAR
     
    while True:
        line=raw_input()
        if line=="#":   exit()
        solve(line)

Submission status: Accepted

Solution code:
m1 = [31,28,31,30,31,30,31,31,30,31,30,31]
m2 = [31,29,31,30,31,30,31,31,30,31,30,31]
while 1:
    try:d = list(map(int, input().split(".")))
    except:break
    if len(d) == 3:
        days = -734858
        days += (d[0] - 1) * 365 + (d[0] - 1) // 4 - (d[0] - 1) // 100 + (d[0] - 1) // 400
        if d[0] % 4 == 0 and (d[0] % 100 != 0 or d[0] % 400 == 0):days += sum(m2[:d[1] - 1])
        else:days += sum(m1[:d[1] - 1])
        days += d[2]
        maya = [0,0,0,0,0]
        maya[0] = days // 144000 % 13
        maya[1] = days // 7200 % 20
        maya[2] = days // 360 % 20
        maya[3] = days // 20 % 18
        maya[4] = days % 20
        print(".".join(map(str, maya)))
    else:
        days = d[0] * 144000 + d[1] * 7200 + d[2] * 360 + d[3] * 20 + d[4] + 734858
        sei = [1,0,0]
        sei[0] += (days  - 1) // 146097 * 400
        days -= (days  - 1) // 146097 * 146097
        sei[0] += min(3, (days  - 1)// 36524) * 100
        days -=  min(3, (days  - 1)// 36524) * 36524
        sei[0] += min(24, (days  - 1) // 1461) * 4
        days -= min(24, (days  - 1) // 1461) * 1461
        sei[0] += min(3, (days  - 1) // 365)
        days -= min(3, (days  - 1) // 365) * 365
        if sei[0] % 4 == 0 and (sei[0] % 100 != 0 or sei[0] % 400 == 0):
            for i in range(12):
                if sum(m2[:i + 1]) >= days:
                    sei[1] = i + 1
                    days -= sum(m2[:i])
                    break
        else:
            for i in range(12):
                if sum(m1[:i + 1]) >= days:
                    sei[1] = i + 1
                    days -= sum(m1[:i])
                    break
        sei[2] = days
        print(".".join(map(str, sei)))

Submission status: Accepted

Solution code:
#-*- coding: utf-8 _*_
import datetime

def is_leap(y):
    if y%400==0:   return True
    if y%100==0:   return False
    if y%4==0:     return True
    return False

def get_day(y,m,d):
    if m<=2:
        y-=1
        m+=12
    dy = 365*(y-1)
    c = y/100
    dl=(y>>2)-c+(c>>2)
    dm=(m*979-1033)>>5
    return dy+dl+dm+d-1

def solve(line):
    ipt = map(int, line.split("."))
#     print ipt
    if len(ipt) >3:
        n=0
        for i in range(5):  n+=ipt[i]*mm[i]
        date = datetime.date.fromordinal(offset+n+1)
        print str(date.year)+"."+str(date.month)+"."+str(date.day)
    else:
        n=get_day(*ipt)-offset
#         print n
        n%=1872000
        ret=""
        for i in range(4):
            ret+=str(n/mm[i])+"."
            n%=mm[i]
        ret+=str(n)
        print ret

if __name__=='__main__':
    # const
    mm=(144000,7200,360,20,1)
    w1=(0,0,31,59,90,120,151,181,212,243,273,304,334,365)
    w2=(0,0,31,60,91,121,152,182,213,244,274,305,335,366)
    offset = get_day(2012,12,21)
#     print datetime.MAXYEAR
    
    while True:
        line=raw_input()
        if line=="#":   exit()
        solve(line)

Submission status: Accepted

Solution code:
from datetime import date, timedelta

st = date(2012, 12, 21)
while True:
    s = input()
    if s == '#':
        break
    d = list(map(int, s.split('.')))
    if len(d) == 3:
        r = 0
        while d[0] > 3000:
            d[0] -= 400
            r += 365*400+97

        ed = date(d[0], d[1], d[2])
        r += (ed-st).days
        ki = r%20
        r//=20
        w = r%18
        r//=18
        t = r%20
        r//=20
        ka = r%20
        r//=20
        b = r%13
        print(str(b)+'.'+str(ka)+'.'+str(t)+'.'+str(w)+'.'+str(ki))
    else:
        r = d[0]
        r *= 20
        r += d[1]
        r *= 20
        r += d[2]
        r *= 18
        r += d[3]
        r *= 20
        r += d[4]
        ed = st+timedelta(days=r)
        print(str(ed.year)+'.'+str(ed.month)+'.'+str(ed.day))

Submission status: Accepted

Solution code:
def Fliegel(y, m, d):
    if m <= 2:
        m += 12
        y -= 1
    return int(365.25*y) + int(30.59*(m-2)) + (y//400) - (y//100) + d - 678912

def InvFliegel(mjd):
    mjd += 678881
    a = 4*mjd + 3 + 4*(3*(1 + 4*(mjd+1)//146097)//4)
    b = 2 + 5*((a%1461)//4)
    y = a//1461
    m = 3 + b//153
    d = 1 + (b%153)//5
    if m > 12:
        m -= 12
        y += 1
    return y, m, d

mjd_base = Fliegel(2012, 12, 21)
maya_unit = [13, 20, 20, 18, 20]
maya_cycle = 13*20*20*18*20

while 1:
    s = input()
    if s == '#':
        break
    v = list(map(int, s.split(".")))
    if len(v) == 3:
        mjd = (Fliegel(v[0], v[1], v[2]) - mjd_base)%maya_cycle
        m = []
        for u in reversed(maya_unit) :
            m.append( mjd%u )
            mjd /= u
        print("%d.%d.%d.%d.%d" % (m[4], m[3], m[2], m[1], m[0]))
    else:
        mjd = v[0]
        for i in range(1,5):
            mjd *= maya_unit[i]
            mjd += v[i]
        y, m, d = InvFliegel(mjd + mjd_base)
        print("%d.%d.%d" % (y, m, d))

Submission status: Accepted

Solution code:
def convert1(y, m, d):
    if m <= 2:
        m += 12
        y -= 1
    mjd = int(365.25*y) + (y//400) - (y//100) + int(30.59*(m-2)) + d - 678912
    return mjd
def convert2(mjd):
    n = mjd + 678881
    a = 4*n + 3 + 4*(3*(4*(n+1)//146097+1)//4)
    b = 5*((a % 1461)//4) + 2
    y, m, d = a//1461, b//153 + 3, (b % 153)//5 + 1
    if m >= 13:
        y += 1
        m -= 12
    return y, m, d

base = convert1(2012, 12, 21)
while 1:
    s = input()
    if s == '#':
        break
    *vs, = map(int, s.split("."))
    if len(vs) == 5:
        b, ka, t, w, ki = vs
        day = ki + w*20 + t*18*20 + ka*20*18*20 + b*20*20*18*20
        y, m, d = convert2(base + day)
        print("%d.%d.%d" % (y, m, d))
    else:
        y, m, d = vs
        d = convert1(y, m, d) - base
        ki = d % 20; d //= 20
        w = d % 18; d //= 18
        t = d % 20; d //= 20
        ka = d % 20; d //= 20
        b = d % 13
        print("%d.%d.%d.%d.%d" % (b, ka, t, w, ki))

Submission status: Accepted

Solution code:
import datetime
time_std=datetime.date(2012, 12, 21)
while 1:
    n=input()
    if n=="#":break
    n_len=list(map(int,n.split(".")))
    if len(n_len)==3:
        year_keep=0
        while n_len[0]>9999:
            year_keep+=1
            n_len[0]-=400
        ans=[0]*5
        cal_date=datetime.date(n_len[0], n_len[1], n_len[2])
        date_dif=(cal_date-time_std).days+year_keep*146097
        ans[0]=str(date_dif//(20*20*18*20)%13)
        ans[1]=str(date_dif//(20*18*20)%20)
        ans[2]=str(date_dif//(18*20)%20)
        ans[3]=str(date_dif//(20)%18)
        ans[4]=str(date_dif%(20))
        print(".".join(ans))
    if len(n_len)==5:
        ans=[0]*3
        past_date=(((n_len[0]*20+n_len[1])*20+n_len[2])*18+n_len[3])*20+n_len[4]
        year_keep=past_date//146097
        past_date=past_date%146097
        ans=list(map(str,str(datetime.date(2012, 12, 21)+datetime.timedelta(days=past_date)).split("-")))
        ans[0]=str(int(ans[0])+year_keep*400)
        ans[1]=str(int(ans[1]))
        ans[2]=str(int(ans[2]))
        print(".".join(ans))