Commit 51109486 authored by Kento HASEGAWA's avatar Kento HASEGAWA
Browse files

Add scripts for analyzing the groups of the blocks in a problem

parent b6afe55f
Loading
Loading
Loading
Loading
+1 −0
Changes for .gitignore: 1 added line, 0 removed lines.
Original line number Diff line number Diff line
@@ -2,6 +2,7 @@

# Project-specific files
/conf*.json
/test.py
/problems/*
!/problems/.gitkeep
/solutions/*
+18 −6
Changes for templates/part_problems.html: 18 added lines, 6 removed lines.
Original line number Diff line number Diff line
@@ -2,18 +2,30 @@
    <thead>
        <tr>
            <th class="large-cell">File Name</th>
            <th class="small-cell">Size</th>
            <th class="small-cell">#Blocks</th>
            <th class="large-cell">Status</th>
            <th class="small-cell">
                Size<br />
                #B/#L/#G
            </th>
            <!-- <th class="small-cell">B/L/G</th> -->
            <th class="small-cell">Status</th>
        </tr>
    </thead>
    <tbody>
        {% for k, v in problems.items() %}
        <tr class="problem-row" data-problem="{{v.name}}">
            <td class="large-cell">{{v.name}}</td>
            <td class="small-cell">{{v.size_str}}</td>
            <td class="small-cell">{{v.block_num}}</td>
            <td class="large-cell">{{v.status}}</td>
            <td class="small-cell">
                {{v.size_str}}<br />
                {{v.block_num}} /
                {{v.line_numbers | length - 1}} /
                {{v.block_groups | length}}
            </td>
            <!-- <td class="small-cell">
                {{v.block_num}}/
                {{v.line_numbers | length}}/
                {{v.block_groups | length}}
            </td> -->
            <td class="small-cell">{{v.status}}</td>
        </tr>
        {% endfor %}
    </tbody>
+108 −2
Changes for utils/data.py: 108 added lines, 2 removed lines.
Original line number Diff line number Diff line
import datetime
import json
import os
import queue
import re
import time
import uuid

class Problem(object):

    def __init__(self, problem_path, solution_path):
    def __init__(self, problem_path, solution_path=None):

        self.path = problem_path
        self.name = ''
@@ -19,6 +20,9 @@ class Problem(object):
        self.solutions = dict()
        self.solution_path = solution_path
        self.best_solution = None
        self.line_numbers = list()
        self.connection = tuple()
        self.block_groups = list()

        self._load_problem(problem_path)
    
@@ -26,6 +30,35 @@ class Problem(object):
    def size_str(self):
        return f'{self.size[0]}X{self.size[1]}'

    @property
    def problem_text(self):
        
        def intplus(v):
            if v.isdecimal():
                return int(v)
            else:
                return v

        _text = ''

        _text += f'SIZE {self.size_str}\n'
        _text += f'BLOCK_NUM {self.block_num}\n'
        _text += '\n'

        for bi, block in self.blocks.items():
            _text += f'BLOCK#{bi} {block["w"]}X{block["h"]}\n'
            for cr in block['cells']:
                for cci, cc in enumerate(cr):
                    if cci > 0:
                        _text += ','
                    if isinstance(cc, int) and cc > 0:
                        cc = self.line_numbers.index(cc)
                    _text += f'{cc}'
                _text += '\n'
            _text += '\n'
        
        return _text
    
    @property
    def status(self):
        _status = 'None'
@@ -64,6 +97,11 @@ class Problem(object):
            else:
                return v

        line_number_list = [0]
        
        block_to_line = dict()
        line_to_block = dict()

        li = 0
        while li < len(q_lines):
            _l = q_lines[li]
@@ -87,7 +125,25 @@ class Problem(object):
                for _h in range(bh):
                    li += 1
                    _l = q_lines[li].strip()
                    blocks[bi]['cells'].append([intplus(v.strip()) for v in _l.split(',')])
                    _block_row = []
                    for v in _l.split(','):
                        _line_num = intplus(v.strip())
                        if isinstance(_line_num, int) and _line_num > 0:
                            # Line number conversion
                            if not _line_num in line_number_list:
                                line_number_list.append(_line_num)
                            # _line_num = line_number_list.index(_line_num)

                            # Make connection list
                            if not bi in block_to_line:
                                block_to_line[bi] = list()
                            block_to_line[bi].append(_line_num)
                            if not _line_num in line_to_block:
                                line_to_block[_line_num] = list()
                            line_to_block[_line_num].append(bi)
                        _block_row.append(_line_num)
                    blocks[bi]['cells'].append(_block_row)
                    # blocks[bi]['cells'].append([intplus(v.strip()) for v in _l.split(',')])

            li += 1
        
@@ -98,8 +154,58 @@ class Problem(object):
        self.blocks = blocks
        self.name = name
        self.problem = q_text
        self.line_numbers = line_number_list
        self.connection = (line_to_block, block_to_line)
        # self.status = 'Ready'

        self._analyze_block_groups()

    def _analyze_block_groups(self):
                
        traversed_block = list()

        line_to_block = self.connection[0]
        block_to_line = self.connection[1]

        block_groups = list()

        for b in block_to_line.keys():

            if b in traversed_block:
                continue

            traverse_block_queue = queue.Queue()
            traverse_block_queue.put(b)

            target_group_blocks = list()

            while not traverse_block_queue.empty():

                target_block = traverse_block_queue.get()
                target_block_lines = block_to_line[target_block]

                if target_block in traversed_block:
                    continue

                traversed_block.append(target_block)
                target_group_blocks.append(target_block)

                for line in target_block_lines:
                    corresponding_blocks = line_to_block[line]
                    if corresponding_blocks[0] == target_block:
                        next_block = corresponding_blocks[1]
                    else:
                        next_block = corresponding_blocks[0]

                    if next_block in traversed_block:
                        continue
                    else:
                        traverse_block_queue.put(next_block)

            block_groups.append(target_group_blocks)

        self.block_groups = block_groups

    def get_dict(self):
        return {
            'name': self.name,