13th Offline Real-time How to Solve Writing Problems in Python

The problem is this Qiita 13th Offline Real-Time Writing Problem I solved it with Python. I saw a problem in the morning and wondered if that would be good during work. Implementation takes about 1 hour.

doukaku_13.py


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

def create_aquarium(input):
    #0 is an empty space,1 is a square with building blocks
    aquarium = []
    for num in input:
        brick_line = [0 for i in range(9)]
        for i in range(int(num)):
            brick_line[8 - i] = 1
        aquarium.append(brick_line)
    aquarium.append([0 for i in range(9)])
    aquarium.insert(0, [0 for i in range(9)])
    return aquarium

def fill_air(aquarium):
    #Breadth-first search
    def fill(y, x):
        aquarium[y][x] = 2
        try:
            if aquarium[y + 1][x] == 0: fills.append((y + 1, x))
        except: pass
        try:
            if aquarium[y - 1][x] == 0: fills.append((y - 1, x))
        except: pass
        try:
            if aquarium[y][x - 1] == 0: fills.append((y, x - 1))
        except: pass

    fills = []
    #setup
    for i in range(len(aquarium)):
        if aquarium[i][8] == 0:
            fills.append((i, 8))

    for y, x in fills:
        fill(y, x)

    return aquarium

def count_water(aquarium):
    return sum([line.count(0) for line in aquarium])

def test(input, answer):
    aquarium = fill_air(create_aquarium(input))
    my_answer = str(count_water(aquarium))
    if my_answer == answer:
        return True
    else:
        print "answer is %s but my answer is %s" % (answer, my_answer)



if __name__ == "__main__":
    test( "83141310145169154671122", "24" )
    test( "923111128", "45" )
    test( "923101128", "1" )
    test( "903111128", "9" )
    test( "3", "0" )
    test( "31", "0" )
    test( "412", "1" )
    test( "3124", "3" )
    test( "11111", "0" )
    test( "222111", "0" )
    test( "335544", "0" )
    test( "1223455321", "0" )
    test( "000", "0" )
    test( "000100020003121", "1" )
    test( "1213141516171819181716151413121", "56" )
    test( "712131415161718191817161514131216", "117" )
    test( "712131405161718191817161514031216", "64" )
    test( "03205301204342100", "1" )
    test( "0912830485711120342", "18" )
    test( "1113241120998943327631001", "20" )
    test( "7688167781598943035023813337019904732", "41" )
    test( "2032075902729233234129146823006063388", "79" )
    test( "8323636570846582397534533", "44" )
    test( "2142555257761672319599209190604843", "41" )
    test( "06424633785085474133925235", "51" )
    test( "503144400846933212134", "21" )
    test( "1204706243676306476295999864", "21" )
    test( "050527640248767717738306306596466224", "29" )
    test( "5926294098216193922825", "65" )
    test( "655589141599534035", "29" )
    test( "7411279689677738", "34" )
    test( "268131111165754619136819109839402", "102" )

Rather than looking for a square that collects water, I solved it as if I were looking for a square that does not collect water. Search from the bottom to the top of the field, and when you finish searching to the top, water will collect in the unsearched squares.

Recommended Posts

13th Offline Real-time How to Solve Writing Problems in Python
How to write offline real time Solve E04 problems in Python
The 18th offline real-time writing problem in Python
The 14th offline real-time how to write reference problem in python
The 19th offline real-time writing problem in Python
The 18th offline real-time how to write reference problem in 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
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
How to write offline real time Solve F01 problems with Python
The 16th offline real-time how to write problem was solved with Python
The 15th offline real-time how to write problem was solved with python
Solve optimization problems in Python
How to develop in Python
[Python] How to do PCA in Python
Offline real-time how to write Python implementation example of E14
How to collect images in Python
How to use Mysql in python
How to wrap C in Python
How to use ChemSpider in Python
How to use PubChem in Python
How to handle Japanese in Python
Offline real-time how to write E11 ruby and python implementation example
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.
[Introduction to Python] How to use class in Python?
How to access environment variables in Python
Answer to "Offline real-time writing F04 problem"
How to dynamically define variables in Python
How to do R chartr () in Python
Answer to "Offline real-time writing F05 problem"
[Itertools.permutations] How to put permutations in Python
How to get a stacktrace in python
How to display multiplication table in python
How to extract polygon area in Python
Answer to "Offline Real-Time Writing E12 Problem"
How to check opencv version in python
I wanted to solve ABC159 in Python
How to switch python versions in cloud9
How to use __slots__ in Python class
How to dynamically zero pad in Python
How to use regular expressions in Python
How to display Hello world in python
How to use is and == in Python
How to write Ruby to_s in Python
How to write offline real time I tried to solve E11 with python
How to write offline real time I tried to solve E12 with python
How to use the C library in Python
How to receive command line arguments in Python
[REAPER] How to play with Reascript in Python
How to clear tuples in a list (Python)
How to generate permutations in Python and C ++
How to embed a variable in a python string
How to implement Discord Slash Command in Python
How to use Python Image Library in python3 series
How to create a JSON file in Python
Answer to "Offline real-time how to write F02 problem"
Summary of how to use MNIST in Python