예제 #1
0
 def solve_one_way(self, line: CellsLine,
                   instructions: List[int]) -> CellsLine:
     number_of_instructions = len(instructions)
     for instruction_index in range(number_of_instructions):
         end_index = self.get_end_index(line, instructions,
                                        instruction_index)
         start_index = self.get_start_index(line, instructions,
                                            instruction_index)
         if start_index > end_index:
             continue
         line.mark_inclusive(start_index, end_index, CellMark.FILLED)
     return line
예제 #2
0
    def solve_one_way(self, line: CellsLine,
                      instructions: List[int]) -> CellsLine:
        filled_sections = line.filled_sections
        if len(filled_sections) != len(instructions):
            return line
        for i in range(0, len(filled_sections) - 1):
            section, next_section = filled_sections[i], filled_sections[i + 1]
            instruction, next_instruction = instructions[i], instructions[i +
                                                                          1]
            if not self.splitted_sections(
                    line=line,
                    section=section,
                    next_section=next_section,
                    instruction=instruction,
                    next_instruction=next_instruction,
            ):
                return line

        for i in range(0, len(filled_sections) - 1):
            section, next_section = filled_sections[i], filled_sections[i + 1]
            instruction, next_instruction = instructions[i], instructions[i +
                                                                          1]
            cross_start = section.end + instruction - section.length + 1
            cross_end = next_section.start - next_instruction + next_section.length - 1
            line.mark_inclusive(cross_start, cross_end, CellMark.CROSSED)
        first_section, last_section = filled_sections[0], filled_sections[-1]
        first_instruction, last_instruction = instructions[0], instructions[-1]
        first_cross_end = first_section.start - first_instruction + first_section.length - 1
        if 0 <= first_cross_end < first_section.start:
            line.mark_inclusive(0, first_cross_end, CellMark.CROSSED)
        last_cross_start = last_section.end + last_instruction - last_section.length + 1
        if last_section.end < last_cross_start < len(line):
            line.mark_inclusive(last_cross_start,
                                len(line) - 1, CellMark.CROSSED)
        return line
예제 #3
0
 def solve_one_way(self, line: CellsLine,
                   instructions: List[int]) -> CellsLine:
     if line.is_completed:
         return line
     first_empty_section = line.empty_sections[0]
     if (first_empty_section.start != 0
             and line[first_empty_section.start - 1] == CellMark.FILLED):
         return line
     number_of_known_instructions = len([
         section for section in line.filled_sections
         if section.end < first_empty_section.start
     ])
     remaining_instructions = instructions[number_of_known_instructions:]
     if len(remaining_instructions) == 0:
         return line
     first_remaining_instruction = remaining_instructions[0]
     min_remaining_instruction = min(remaining_instructions)
     remaining_sections = [
         section for section in line.sections
         if section.start >= first_empty_section.start
     ]
     fill_first_gap = True
     for i in range(len(remaining_sections)):
         section = remaining_sections[i]
         if section.mark == CellMark.FILLED:
             if not fill_first_gap:
                 continue
             fill_first_gap = False
             previous_section = remaining_sections[i - 1]
             if (previous_section.mark == CellMark.EMPTY
                     and previous_section.length <=
                     first_remaining_instruction):
                 line.mark_inclusive(
                     section.start, previous_section.start +
                     first_remaining_instruction - 1, CellMark.FILLED)
                 if section.blocked_above:
                     line.mark_inclusive(
                         section.end - first_remaining_instruction + 1,
                         section.end, CellMark.FILLED)
                     if section.end - first_remaining_instruction >= 0:
                         line[
                             section.end -
                             first_remaining_instruction] = CellMark.CROSSED
             continue
         if section.mark == CellMark.CROSSED:
             continue
         if not section.blocked:
             continue
         if section.length >= first_remaining_instruction:
             fill_first_gap = False
         if (section.length < min_remaining_instruction
                 or (fill_first_gap
                     and section.length < first_remaining_instruction)):
             line.mark_inclusive(section.start, section.end,
                                 CellMark.CROSSED)
     return line