http://nabetani.sakura.ne.jp/hena/ordf01_twicel/ Répondre 1 heure et 30 minutes ... Il y a encore plus.
main.py
MAX_CEL_COUNT = 8
def get_bit(data, x, y):
if x<0 or y<0 or x>=MAX_CEL_COUNT or y>=MAX_CEL_COUNT:
return -1
return data[x][y]
def twicel_smell(data, cx, cy):
current_bit = get_bit(data, cx, cy)
hitcel = (-1, -1)
hitcount = 0
arounds = [[0, -1],[0, 1],[-1, 0],[1, 0]]
for around in arounds:
if current_bit == get_bit(data, cx + around[0], cy + around[1]):
hitcount +=1
hitcel = (cx + around[0], cy + around[1])
if hitcount != 1:
return None
return hitcel
def count_twicel(cels):
targets = []
data = [[]]
hitresult = [0,0]
for i in range(0, MAX_CEL_COUNT):
raw = format(cels[i], '08b')
data.append([])
for j in range(0, MAX_CEL_COUNT):
targets.append((i, j))
data[i].append(int(raw[j]))
for target in targets:
result1 = twicel_smell(data, target[0], target[1])
if result1 == None:
continue
result2 = twicel_smell(data, result1[0], result1[1])
if target == result2:
hitresult[get_bit(data, target[0], target[1])] += 1
targets.remove(result1)
return (hitresult[0], hitresult[1])
def test(result, actual, expect):
result[0 if actual == expect else 1] +=1
result= [0,0]
test(result, count_twicel([0xdc, 0xbc, 0xa7, 0x59, 0x03, 0xd5, 0xd4, 0xea]), (2,3))
test(result, count_twicel([0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]), (0,0))
test(result, count_twicel([0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]), (0,0))
test(result, count_twicel([0xcc, 0x33, 0xcc, 0x33, 0xcc, 0x33, 0xcc, 0x33]), (16,16))
test(result, count_twicel([0xaa, 0xaa, 0x55, 0x55, 0xaa, 0xaa, 0x55, 0x55]), (16,16))
test(result, count_twicel([0xac, 0xa3, 0x5c, 0x53, 0xca, 0x3a, 0xc5, 0x35]), (8,8))
test(result, count_twicel([0xdb, 0x00, 0xdb, 0x00, 0xdb, 0x00, 0xaa, 0xaa]), (0,13))
test(result, count_twicel([0x24, 0x24, 0xdb, 0x24, 0x24, 0xdb, 0x24, 0x24]), (0,12))
test(result, count_twicel([0xd7, 0xd7, 0xe9, 0xf1, 0xf7, 0xde, 0x60, 0x56]), (3,2))
test(result, count_twicel([0x17, 0x7d, 0x64, 0x9b, 0xa5, 0x39, 0x53, 0xa6]), (2,2))
test(result, count_twicel([0xbb, 0x8f, 0x18, 0xfb, 0x89, 0xc2, 0xc7, 0x35]), (1,2))
test(result, count_twicel([0x6d, 0x63, 0x20, 0x08, 0x54, 0xcd, 0x32, 0x4f]), (2,2))
test(result, count_twicel([0xa9, 0xca, 0xcd, 0x46, 0x99, 0xe6, 0xf0, 0x30]), (2,2))
test(result, count_twicel([0x5b, 0x70, 0xfd, 0x45, 0xe2, 0xa1, 0xab, 0x9a]), (1,2))
test(result, count_twicel([0x24, 0xe4, 0xa8, 0x12, 0xe1, 0xa6, 0x3f, 0xf3]), (2,1))
test(result, count_twicel([0x79, 0x32, 0x2e, 0x07, 0xd5, 0x10, 0xe7, 0x9d]), (2,2))
test(result, count_twicel([0x60, 0xbc, 0xab, 0xec, 0x1f, 0xeb, 0x63, 0x2c]), (4,2))
test(result, count_twicel([0xa5, 0xdd, 0x92, 0x4e, 0x67, 0xc6, 0xdc, 0x34]), (6,1))
test(result, count_twicel([0xaa, 0x96, 0x6d, 0x67, 0xd2, 0xa8, 0xac, 0x90]), (3,2))
test(result, count_twicel([0x95, 0x72, 0x7d, 0x5c, 0x47, 0xdc, 0xef, 0x99]), (4,0))
test(result, count_twicel([0x17, 0xd6, 0x6a, 0x27, 0x1f, 0x25, 0x26, 0xb8]), (2,1))
test(result, count_twicel([0xf0, 0xf3, 0x76, 0xc5, 0x31, 0xca, 0x6b, 0xae]), (1,2))
test(result, count_twicel([0x01, 0x59, 0x26, 0xfa, 0x8c, 0x70, 0x12, 0xcd]), (1,4))
test(result, count_twicel([0x1a, 0xc3, 0x1f, 0x0b, 0x83, 0xb6, 0x81, 0x0d]), (0,5))
test(result, count_twicel([0x4c, 0x49, 0x05, 0xcf, 0x54, 0xbb, 0x1f, 0xda]), (1,2))
test(result, count_twicel([0xeb, 0x7c, 0xd5, 0x09, 0x2a, 0xc2, 0x14, 0x6b]), (0,7))
test(result, count_twicel([0xb4, 0xd3, 0x4c, 0xc4, 0xed, 0x19, 0xe8, 0x63]), (1,3))
test(result, count_twicel([0xbd, 0xbc, 0x6d, 0x60, 0x9b, 0x00, 0x9a, 0x32]), (2,4))
test(result, count_twicel([0x94, 0x97, 0x3f, 0xe3, 0xc7, 0x06, 0x15, 0xc0]), (2,2))
test(result, count_twicel([0x5f, 0x1d, 0x67, 0x16, 0xb8, 0xf7, 0x0a, 0x2a]), (2,2))
test(result, count_twicel([0xdf, 0xe6, 0xf9, 0x4f, 0x59, 0xe9, 0x1f, 0xee]), (3,0))
test(result, count_twicel([0x5a, 0x53, 0x9a, 0x9a, 0x73, 0xb4, 0x37, 0x07]), (3,2))
test(result, count_twicel([0xbd, 0x87, 0x7c, 0xe7, 0xc0, 0x37, 0x82, 0xda]), (2,3))
test(result, count_twicel([0x3d, 0xc0, 0x13, 0xac, 0x57, 0x3d, 0x15, 0x78]), (2,2))
test(result, count_twicel([0x63, 0x64, 0x54, 0x3a, 0x40, 0x28, 0x4e, 0x4e]), (0,3))
test(result, count_twicel([0xf6, 0x81, 0xc9, 0x15, 0x00, 0x4c, 0xa0, 0xa8]), (1,4))
test(result, count_twicel([0x19, 0x41, 0xdf, 0xf8, 0xe3, 0x74, 0x6b, 0x9b]), (4,2))
test(result, count_twicel([0xd5, 0x0b, 0xdd, 0x35, 0x3b, 0xd2, 0x0b, 0x6b]), (1,5))
test(result, count_twicel([0x08, 0xb7, 0x91, 0xf3, 0x6e, 0x3c, 0x74, 0xa0]), (0,0))
test(result, count_twicel([0xb8, 0xa8, 0xb4, 0xa6, 0x93, 0x2c, 0x94, 0x3f]), (0,0))
test(result, count_twicel([0x88, 0x22, 0x21, 0xee, 0xdc, 0x19, 0x43, 0x01]), (0,0))
test(result, count_twicel([0xe1, 0xee, 0x35, 0xbc, 0xfc, 0x00, 0x8e, 0xfe]), (0,0))
test(result, count_twicel([0x3c, 0x42, 0x63, 0x5f, 0x27, 0x47, 0x07, 0x90]), (0,0))
print("Success:" + str(result[0]) + ", Fail:" + str(result[1]))
J'ai un peu raccourci le code.
Recommended Posts