Answer to "Offline Real-time How to Write F01 Problem"

http://nabetani.sakura.ne.jp/hena/ordf01_twicel/ Answer 1 hour and 30 minutes.... There is still more.

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]))

2017/01/26 Changed

I shortened the code a little.

Recommended Posts

Answer to "Offline real-time how to write F02 problem"
Answer to "Offline Real-time How to Write F01 Problem"
Answer to "Offline Real-time How to Write E13 Problem"
Answer to "Offline real-time writing F04 problem"
Answer to "Offline real-time writing F05 problem"
Answer to "Offline Real-Time Writing E12 Problem"
The 15th offline real-time how to write reference problem in Python
Offline real-time how to write Python implementation example of E15 problem
The 14th offline real-time how to write reference problem in python
The 18th offline real-time how to write reference problem in Python
The 16th offline real-time how to write problem was solved with Python
The 17th offline real-time how to write reference problem implemented in Python
The 19th offline real-time how to write reference problem to solve with Python
The 15th offline real-time how to write problem was solved with python
20th Offline Real-time How to Write Problems in Python
How to write offline real-time Solving E05 problems with Python
The 15th offline real-time I tried to solve the problem of how to write with python
Offline real-time how to write E11 ruby and python implementation example
How to write offline real time Solve F01 problems with Python
How to write offline real time I tried to solve the problem of F02 with Python
Part 1 I wrote the answer to the reference problem of how to write offline in real time in Python
XPath Basics (2) -How to write XPath
Part 1 I wrote an example of the answer to the reference problem of how to write offline in real time in Python
13th Offline Real-time How to Solve Writing Problems in Python
How to write a Python class
How to write soberly in pandas
Flask reuse How to write html
How to write Docker base image
How to write Django1.9 environment-independent wsgi.py
Notes on how to write requirements.txt
The 17th Offline Real-time How to Solve Writing Problems in Python
How to write offline real time Solve E04 problems in Python
Answer to Splash memory overuse problem (draft)
Qiita (1) How to write a code name
How to set optuna (how to write search space)
How to write Python document comments (Docstrings)
How to write offline real time I tried to solve E11 with python
How to write this process in Perl?
How to read problem data with paiza
How to write Ruby to_s in Python
Summary of how to write AWS Lambda
How to write pydoc and multi-line comments
How to write offline real time I tried to solve E12 with python
How to write urlfetch unittest on GAE / P
How to write regular expression patterns in Linux
The 18th offline real-time writing problem in Python
How to write async and await in Vue.js
How to write a named tuple document in 2020
[Go] How to write or call a function
The 19th offline real-time writing problem in Python
How to write a ShellScript bash case statement
How to write a GUI using the maya command
[Python] Try to read the cool answer to the FizzBuzz problem
How to write string concatenation in multiple lines in Python
How to write a list / dictionary type of Python3
[Python] How to write a docstring that conforms to PEP8
The 14th offline real-time writing reference problem with Python
How to write faster when using numpy like deque
Compare how to write processing for lists by language
[Introduction to Python] How to write repetitive statements using for statements