Offline real-time how to write Python implementation example of E14

I haven't participated offline, but I implemented it in Python. Can I have more fun with numpy?

PS: The added test data is NG, so it has been fixed.

Problem: http://mtsmfm.github.io/2017/06/03/doukaku-e14.html Answer links: http://qiita.com/mtsmfm/items/89b6634f363bbf5b47f5 Event: https://yhpg.doorkeeper.jp/events/59397

#!/usr/bin/env python
# -*- coding:utf-8 -*-

patterns = [    # dice patterns and the opposite surface pair numbers
    "1000,2323,1000", "1000,2323,0100", "1000,2323,0010", "1000,2323,0001",
    "0100,2323,0100", "0100,2323,0010", "1200,0313,0020", "1200,0313,0200",
    "1200,0310,0023", "1200,0313,0002", "12100,00323"]
patterns = [[list(map(int, row)) for row in p.split(',')] for p in patterns]
patterns += [[row[::-1] for row in p] for p in patterns]    # flip horizontal
patterns += [p[::-1] for p in patterns]                     # flip vertical
patterns += [list(zip(*p)) for p in patterns]               # transpose

def solve(data):
    dice = [list(map(int, row)) for row in data.split(',')]
    for dy in range(len(dice)):
        for dx in range(len(dice[dy])):
            for pattern in patterns:
                try:
                    pickup = {dice[dy + py][dx + px] : pair
                              for py, row in enumerate(pattern)
                              for px, pair in enumerate(row)
                              if pair != 0}
                    if (sorted(pickup) == [1, 2, 3, 4, 5, 6] and
                        sum(d * (10 ** p) for d, p in pickup.items()) == 7770):
                        return "true"   # sum of each pair 3,2,1,0 is 7,7,7,0
                except IndexError:  # a index of the dice array is out of range
                    pass            # so, the pattern didn't match
    return "false"

class Result:
    correct = 0
    wrong = 0

def test(data, correct):
    answer = solve(data)
    if answer == correct:
        Result.correct += 1
    else:
        print("%s, %s != %s" % (data, correct, answer))
        Result.wrong += 1

if __name__ == '__main__':
    test("44165,44516", "false");
    test("26265,31436", "true");
    test("46345,54215", "true");
    test("62143,11152", "false");
    test("4242,4314,1562", "false");
    test("5612,3656,4523", "false");
    test("5514,1311,5252", "false");
    test("5262,4631,2644", "true");
    test("6626,3324,2644", "false");
    test("4645,6314,2564", "true");
    test("54,65,23,21,14", "true");
    test("5325,3641,1335", "true");
    test("4163,2156,2553", "true");
    test("3126,6543,4352", "false");
    test("4464,5423,5216", "true");
    test("3564,3634,5631", "false");
    test("4363,3454,2126", "true");
    test("25,25,33,12,52", "false");
    test("1551,4542,3624", "true");
    test("6623,4126,6331", "false");
    test("2432,6215,1623", "true");
    test("1151,6555,3616", "false");
    test("2466,1242,4444", "false");
    test("5646,1463,4244", "true");
    test("1255,6413,4534", "true");
    test("1325,2312,2425", "false");
    test("2544,6413,4656", "true");
    test("1656,4131,3235", "true");
    test("6332,3631,4113", "false");
    test("4525,2151,2336", "true");
    test("1645,2356,4314", "true");
    test("3334,6215,1553", "true");
    test("2622,5251,5165", "false");
    test("1111,5613,3451", "false");
    test("6146,4512,6353", "true");
    test("2455,3312,6461", "false");
    test("1221,1325,1422", "false");
    test("1562,2236,5212", "false");
    test("6622,3324,5155", "true");
    test("2352,4631,1236", "true");
    test("4645,2252,6554", "false");
    test("3542,6515,1231", "true");
    test("12,61,56,45,23", "false");
    test("4643,6522,3625", "false");
    test("1151,1642,4512", "false");
    test("5423,5146,2212", "false");
    test("6224,3412,5653", "true");
    test("3122,5423,6231", "true");
    test("5421,2351,6513", "false");
    test("5652,3542,3313", "true");
    test("5524,3335,1146", "false");
    test("5311,4126,6425", "true");
    test("15,43,62,42,14", "true");
    test("3631,3542,3265", "true");
    test("1232,5364,6135", "true");
    test("2441,4644,5433", "false");
    test("2213,5621,3412", "true");
    test("6644,1264,1235", "true");
    test("5613,1423,6315", "true");
    test("6552,1546,2141", "false");
    test("5623,1461,5645", "true");
    test("1442,1436,6362", "false");
    test("3443,5145,4546", "false");
    test("1244,1313,2316", "false");
    test("2152,1463,2114", "true");
    test("1211,6234,5561", "false");
    test("4152,1252,3142", "false");
    test("6645,1231,6122", "false");
    test("353,241,121,536", "true");
    test("224,444,651,234", "true");
    test("643,214,244,343", "false");
    test("624,542,214,333", "true");
    test("441,426,536,656", "true");
    test("564,642,513,364", "true");
    test("422,136,414,416", "false");
    test("463,356,113,662", "true");
    test("464,515,435,462", "true");
    test("531,145,364,525", "false");
    test("623,564,153,633", "false");
    test("335,462,531,424", "false");
    test("131,111,535,436", "false");
    test("435,414,423,365", "true");
    test("144,512,332,346", "true");
    test("342,246,342,634", "false");
    test("246,566,431,415", "true");
    test("444,554,234,621", "true");
    test("313,624,165,652", "true");
    test("563,262,545,315", "true");
    test("213,264,154,264", "false");
    test("364,434,246,113", "false");
    test("411,656,325,225", "false");
    test("624,234,115,443", "true");
    test("252,214,635,154", "false");
    test("146,213,525,164", "false");
    test("456,423,112,352", "true");
    test("253,156,111,355", "false");
    test("252,161,562,365", "false");
    test("136,553,544,524", "true");
    test("414,351,161,525", "true");
    test("261,442,111,531", "true");
    test("323,664,454,133", "true");
    test("213,415,225,165", "false");
    test("363,162,165,533", "false");
    test("346,441,315,241", "false");
    test("121,312,155,164", "true");
    test("123,311,452,365", "true");
    test("361,145,212,431", "true");
    test("451,264,412,513", "false");
    test("311,456,263,226", "true");
    test("343,442,624,635", "false");
    test("534,644,234,251", "false");
    test("515,346,462,435", "true");
    test("445,126,165,636", "false");
    test("343,355,126,353", "false");
    test("623,533,256,144", "true");
    test("125,341,566,416", "false");
    test("354,434,621,411", "true");
    test("356,435,253,114", "false");
    test("141,265,533,514", "true");
    test("613,426,142,535", "true");
    test("366,322,413,325", "true");
    test("331,542,343,545", "false");
    test("261,512,563,123", "false");
    test("242,132,656,164", "true");
    test("133,545,441,665", "false");
    test("111,151,621,545", "false");
    test("132,154,234,653", "false");
    test("114,455,635,511", "false");
    test("14366,64254,66664,42611,41245", "false");
    test("41114,53116,13122,66613,35111", "false");
    test("22146,34244,16154,62531,51426", "true");
    test("464316,136563,136326,535655,641161,252655", "true");
    test("166255,615452,261224,533566,323335,556213", "false");
    test("126665,245653,343553,254661,365415,361154", "false");
    test("1512663,1525531,5456426,6336325,4324465,6512242,4112466", "true");
    test("2236563,6644542,4425515,6641142,4214543,1156426,3225413", "false");
    test("5545354,6566343,3525411,5356165,4625265,1535435,5522665", "false");
    test("16161,61616,16161", "false");
    test("2431,6354,2341", "false");
    print("OK:%s, NG:%s" % (Result.correct, Result.wrong))

Execution result


OK:139, NG:0

Recommended Posts

Offline real-time how to write Python implementation example of E14
Offline real-time how to write Python implementation example of E15 problem
Offline real-time how to write E11 ruby and python implementation example
How to write offline real-time Solving E05 problems with Python
Answer to "Offline Real-time How to Write E13 Problem"
20th Offline Real-time How to Write Problems in Python
The 15th offline real-time I tried to solve the problem of how to write with python
The 15th offline real-time how to write reference problem in Python
How to write offline real time Solve E04 problems in 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 how to write F02 problem"
Answer to "Offline Real-time How to Write F01 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
How to write offline real time I tried to solve E11 with 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
How to write offline real time I tried to solve E12 with python
How to write a list / dictionary type of Python3
How to write a Python class
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 offline real time I tried to solve the problem of F02 with 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.
The 11th offline real-time writing reference problem. Implementation example by python.
How to write offline real time Solve F01 problems with Python
[Python] Summary of how to use pandas
How to write Python document comments (Docstrings)
Answer to "Offline Real-Time Writing E12 Problem"
[Python2.7] Summary of how to use unittest
Summary of how to use Python list
[Python2.7] Summary of how to use subprocess
How to write Ruby to_s in Python
Summary of how to write AWS Lambda
[Question] How to use plot_surface of python
Part 1 I wrote the answer to the reference problem of how to write offline in real time in Python
A simple example of how to use ArgumentParser
[Python] How to use two types of type ()
Summary of how to import files in Python 3
Summary of how to use MNIST in Python
How to specify attributes with Mock of python
How to get dictionary type elements of Python 2.7
How to install python
Implementation example of simple LISP processing system (Python version)
How to get the number of digits in Python
I tried to summarize how to use matplotlib of python
How to write string concatenation in multiple lines in Python
How to use Python Kivy ① ~ Basics of Kv Language ~
[Python] How to write a docstring that conforms to PEP8
[Python] How to split and modularize files (simple, example)
Review of atcoder ABC158, up to question E (Python)
[Python] Summary of how to specify the color of the figure
[Introduction to Python] How to write repetitive statements using for statements
[Python] Summary of eval / exec functions + How to write character strings with line breaks
[2020.8 latest] How to install Python
How to install Python [Windows]
[Introduction to Python] How to write conditional branches using if statements
python3: How to use bottle (2)