yhpg06 - 続柄 をPythonで解いてみる

http://nabetani.sakura.ne.jp/hena/ord6kinship/
で出題されている続柄をPythonで解いてみる。

必ず3人ずつ子供がいるというあたりで、家系図をlistで持っていなくても解けるんじゃないかと思いましたが。


```py3:yhog06.py
#!/usr/bin/env python3
# -*- coding:utf-8 -*-

# https://yhpg.doorkeeper.jp/
# http://nabetani.sakura.ne.jp/hena/ord6kinship/

familyTree = {
 1:[2,3,4],
 2:[5,6,7],
 3:[8,9,10],
 4:[11,12,13],
 5:[14,15,16],
 6:[17,18,19],
 7:[20,21,22],
 8:[23,24,25],
 9:[26,27,28],
 10:[29,30,31],
 11:[32,33,34],
 12:[35,36,37],
 13:[38,39,40]
}

def solve(data):
    data = data.split("->")

    if data[0] == data[1]:
        return "me"
    elif isMo(data[0], data[1]):
        return "mo"
    elif isDa(data[0], data[1]):
        return "da"
    elif isSi(data[0],data[1]):
        return "si"
    elif isAu(data[0], data[1]):
        return "au"
    elif isNi(data[0], data[1]):
        return "ni"
    elif isCo(data[0], data[1]):
        return "co"
    else:
        return "-"

def isMo(left, right):
    try:
        return (int(left) in familyTree[int(right)])
    except:
        return False

def isDa(left, right):
    try:
        return (int(right) in familyTree[int(left)])
    except:
        return False

def isSi(left, right):
    for data in range(1,14):
        if ( int(right) in familyTree[int(data)] and int(left) in familyTree[int(data)]):
            return True
    return False

def isAu(left, right):
    for data in range(1,14):
        if isMo(left, data):
            if isSi(data, right):
                return True
    return False

def isNi(left, right):
    for data in range(1,14):
        if isSi(left, data):
            if isDa(data, right):
                return True
    return False

def isCo(left, right):
    for data in range(1,14):
        if isAu(left, data):
            if isDa(data, right):
                return True
    return False



def test(data, expect):
    result = solve(data)
    if result == expect:
        print("成功: " + result + "/" + expect )
    else:
        print("失敗: " + result + "/" + expect )

test( "5->2", "mo" );
test( "28->10", "au" );
test( "1->1", "me" );
test( "40->40", "me" );
test( "27->27", "me" );
test( "7->2", "mo" );
test( "40->13", "mo" );
test( "9->3", "mo" );
test( "4->1", "mo" );
test( "1->3", "da" );
test( "12->35", "da" );
test( "3->8", "da" );
test( "6->19", "da" );
test( "38->40", "si" );
test( "9->8", "si" );
test( "4->2", "si" );
test( "15->16", "si" );
test( "40->12", "au" );
test( "10->4", "au" );
test( "21->5", "au" );
test( "8->2", "au" );
test( "3->5", "ni" );
test( "11->39", "ni" );
test( "2->13", "ni" );
test( "13->32", "ni" );
test( "14->22", "co" );
test( "40->34", "co" );
test( "5->8", "co" );
test( "12->10", "co" );
test( "1->27", "-" );
test( "8->1", "-" );
test( "12->22", "-" );
test( "2->40", "-" );
test( "32->31", "-" );
test( "13->14", "-" );
```



追記：
整理して、書き直しました。


```py3:yhog06.py
#!/usr/bin/env python3
# -*- coding:utf-8 -*-

# https://yhpg.doorkeeper.jp/
# http://nabetani.sakura.ne.jp/hena/ord6kinship/



def solve(data):
    data = data.split("->")

    left = int(data[0])
    right = int(data[1])

    if left == right:
        return "me"
    elif isMo(left, right):
        return "mo"
    elif isDa(left, right):
        return "da"
    elif isSi(left, right):
        return "si"
    elif isAu(left, right):
        return "au"
    elif isNi(left, right):
        return "ni"
    elif isCo(left, right):
        return "co"
    else:
        return "-"

def mother(target):
    return int((target-2)/3+1)

def isMo(left, right):
    return right == mother(left)

def isDa(left, right):
    return ( mother(right) == left )

def isSi(left, right):
    return ( mother(left) == mother(right) )

def isAu(left, right):
    return isSi(mother(left), right)

def isCo(left, right):
    return isSi(mother(left),mother(right))

def isNi(left, right):
    return isSi(left, mother(right))
'''