Answer to "Offline real-time writing F05 problem"

Answer to "Offline real-time writing F04 problem-turning blocks" http://nabetani.sakura.ne.jp/hena/ordf05rotblo/

I couldn't make it at the venue. About 2 hours in total. I found a Python iPhone app on the train. Debug with it.

The reasons why it did not work at the site are as follows.

--The rounding logic was wrong. It was 0.5 plus without using round. Negative numbers will result in unintended results. --The rotation angle was wrong. It should be -90 degrees, but it was 90 degrees. --The usage of i and j of the multiple array row was reversed.

main.py



## yhpg F05

import math

W = H = 5

def rotate(x, y):
    d = - math.pi / 2
    xd = x * math.cos(d) - y * math.sin(d)
    yd = x * math.sin(d) + y * math.cos(d)
    return (int(round(xd)), int(round(yd)))

def make_key(x, y):
   return str(x) + "," +str(y)

def resolve(data):
    center, rows = data.split(":")
    row = rows.split("/")

    ax = [-1, 0, 1, 2, 3]
    ay = [2, 1, 0, -1, -2]
    bx = [-2, -1, 1, 2, 3]
    by = [2, 1, -1, -2, -3]

    field = {}

    locx, locy = None, None
    if center == 'a':
        locx, locy = ax, ay
    else:
        locx, locy = bx, by

    for i in range(W):
        for j in range(H):
            field[make_key(locx[i], locy[j])] = '0'

    for i in range(W):
        for j in range(H):
            if row[j][i] == '1':
                key = make_key(*rotate(locx[i], locy[j]))
                if field.has_key(key):
                    field[key] = row[j][i]
                else:
                    return '-'        

    result = ['']*H
    for i in range(W):
        for j in range(H):
            result[j] += field[make_key(locx[i], locy[j])]

    return '/'.join(result)


## test logic
class Result:
    def __init__(self):
        self.success = 0
        self.fail = 0

RESULT = Result()
RESULT.success = RESULT.fail = 0

def test(data, expect):
    print("actual:" + resolve(data) + " expected:" + expect)
    if resolve(data) == expect:
        RESULT.success += 1
    else:
        RESULT.fail += 1

## ---- tests ----

test( "a:00000/00110/00100/00100/00000", "00000/00000/00000/11100/00100" )   
test( "b:00000/00000/00000/00011/00011", "-" )
test( "a:00000/00000/00000/00011/00011", "-" )
test( "b:00000/00000/00100/00000/00000", "00000/00000/01000/00000/00000" )
test( "a:00000/00000/00100/00000/00000", "00000/00000/00000/01000/00000" )
test( "b:00000/00110/00100/00100/00000", "00000/00000/11100/00100/00000" )
test( "b:00000/00000/00011/00011/00000", "00000/00000/00000/11000/11000" )
test( "a:00000/00000/00011/00011/00000", "-" )
test( "a:01110/00100/00000/00000/00000", "00000/00000/00010/00110/00010" )
test( "b:01110/00100/00000/00000/00000", "00000/00010/00110/00010/00000" )
test( "a:00000/11110/00000/00000/00000", "00000/00100/00100/00100/00100" )
test( "b:00000/11110/00000/00000/00000", "00100/00100/00100/00100/00000" )
test( "a:00000/00011/00110/00000/00000", "-" )
test( "b:00000/00011/00110/00000/00000", "00000/00000/01000/01100/00100" )
test( "a:00000/11100/11100/11100/00000", "00000/11100/11100/11100/00000" )
test( "b:00000/11100/11100/11100/00000", "11100/11100/11100/00000/00000" )
test( "a:01000/00000/00101/10010/10001", "-" )
test( "b:01000/00000/00101/10010/10001", "-" )
test( "b:10000/00000/10010/00000/00000", "01010/00000/00000/01000/00000" )
test( "a:10000/00000/10010/00000/00000", "00000/01010/00000/00000/01000" )
test( "a:00000/10101/11010/11010/01000", "-" )
test( "b:00000/10101/11010/11010/01000", "-" )
test( "b:01101/00011/01101/00000/00000", "00000/01010/01010/00100/01110" )
test( "a:01101/00011/01101/00000/00000", "-" )
test( "a:00001/00000/00000/00100/00010", "-" )
test( "b:00001/00000/00000/00100/00010", "-" )
test( "b:00100/00000/00100/01000/00000", "00000/10000/01010/00000/00000" )
test( "a:00100/00000/00100/01000/00000", "00000/00000/10000/01010/00000" )
test( "a:00010/00100/00000/10000/00000", "00000/10000/00000/00100/00010" )
test( "b:00010/00100/00000/10000/00000", "10000/00000/00100/00010/00000" )
test( "b:11010/00011/10101/00001/00001", "-" )
test( "a:11010/00011/10101/00001/00001", "-" )
test( "a:00100/00010/00000/11000/00000", "00000/10000/10000/00010/00100" )
test( "b:00100/00010/00000/11000/00000", "10000/10000/00010/00100/00000" )
test( "b:01010/00000/00000/01000/00000", "00000/10010/00000/00010/00000" )
test( "a:01010/00000/00000/01000/00000", "00000/00000/10010/00000/00010" )
test( "a:00000/00000/00100/10100/00000", "00000/10000/00000/11000/00000" )
test( "b:00000/00000/00100/10100/00000", "10000/00000/11000/00000/00000" )
test( "b:10000/01101/01000/01100/10011", "-" )
test( "a:10000/01101/01000/01100/10011", "-" )
test( "a:00010/00000/00110/01000/10001", "-" )
test( "b:00010/00000/00110/01000/10001", "-" )
test( "b:00000/01000/01100/00000/00000", "00000/01100/01000/00000/00000" )
test( "a:00000/01000/01100/00000/00000", "00000/00000/01100/01000/00000" )
test( "a:01000/00000/00000/10000/00000", "00000/10000/00010/00000/00000" )
test( "b:01000/00000/00000/10000/00000", "10000/00010/00000/00000/00000" )
test( "b:00000/01101/00000/01010/11010", "-" )
test( "a:00000/01101/00000/01010/11010", "-" )
test( "a:00110/00101/00000/10100/00100", "-" )
test( "b:00110/00101/00000/10100/00100", "-" )
test( "b:11000/10110/00000/00110/00000", "00110/00010/10100/10100/00000" )
test( "a:11000/10110/00000/00110/00000", "00000/00110/00010/10100/10100" )
test( "a:00000/00000/00000/00001/00110", "-" )
test( "b:00000/00000/00000/00001/00110", "-" )
test( "b:01011/10001/00000/00000/00000", "00100/00010/00000/00010/00110" )
test( "a:01011/10001/00000/00000/00000", "-" )
## --------------

print("Success: {0.success}, Fail: {0.fail}".format(RESULT))

Recommended Posts

Answer to "Offline real-time writing F04 problem"
Answer to "Offline real-time writing F05 problem"
Answer to "Offline Real-Time Writing E12 Problem"
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"
The 18th offline real-time writing problem in Python
The 19th offline real-time writing problem in Python
The 14th offline real-time writing reference problem with Python
13th Offline Real-time How to Solve Writing Problems in Python
The twelfth offline real-time writing reference problem. Implementation by python
The 15th offline real-time how to write reference problem in Python
The 17th Offline Real-time How to Solve Writing Problems in Python
The 10th offline real-time writing reference problem. Implementation example by Python.
Offline real-time how to write Python implementation example of E15 problem
The 11th offline real-time writing reference problem. Implementation example by python.
The 14th offline real-time how to write reference problem in python
The 18th offline real-time how to write reference problem in Python
Answer to "Offline real-time writing F04 problem"
Answer to "Offline real-time writing F05 problem"
Answer to "Offline Real-Time Writing E12 Problem"
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"
[Python] Try to read the cool answer to the FizzBuzz problem
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 16th offline real-time how to write reference problem to solve with 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
The 15th offline real-time I tried to solve the problem of how to write with python
20th Offline Real-time How to Write Problems in Python
[Python] Try to read the cool answer to the FizzBuzz problem
How to write offline real time I tried to solve the problem of F02 with Python
Offline real-time how to write Python implementation example of E14
How to write offline real-time Solving E05 problems with Python
Part 1 I wrote the answer to the reference problem of how to write offline in real time in Python