entry #1
written by LyricLy
submitted at
1 like
guesses
- LyricLy (by taswelll)
- LyricLy (by Dolphy)
- LyricLy (by luatic)
- LyricLy (by kimapr)
- kimapr (by olus2000)
- luatic (by essaie)
- luatic (by JJRubes)
comments 0
yeah.py Unicode text, UTF-8 text
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 | import itertools from collections import defaultdict RIGHT = (1, 0) DOWN = (0, 1) LEFT = (-1, 0) UP = (0, -1) LINES = { " ": set(), "─": {LEFT, RIGHT}, "│": {UP, DOWN}, "┌": {RIGHT, DOWN}, "┐": {LEFT, DOWN}, "└": {UP, RIGHT}, "┘": {UP, LEFT}, "├": {UP, RIGHT, DOWN}, "┤": {UP, LEFT, DOWN}, "┬": {LEFT, DOWN, RIGHT}, "┴": {UP, LEFT, RIGHT}, } def onward(p, d): return p[0]+d[0], p[1]+d[1] def behind(d): return -d[0], -d[1] class Drawing: def __init__(self, width, height): self.width = width self.height = height self.grid = defaultdict(set) def points(self): return itertools.product(range(self.width), range(self.height)) def has(self, at, d): return d in self.grid[at] def erase(self, at, d): self.grid[at].discard(d) def area_is_empty(self, left, right, top, bottom): for x in range(left, right+1): for y in range(top, bottom+1): if self.grid[x, y]: return False return True @classmethod def parse(cls, s): lines = s.split("\n") new = cls(max(map(len, lines)), len(lines)) for y, row in enumerate(lines): for x, c in enumerate(row): new.grid[x, y] = LINES[c].copy() return new def __repr__(self): lines = [] for y in range(self.height): lines.append("") for x in range(self.width): for k, v in LINES.items(): if v == self.grid[x, y]: lines[-1] += k break return "\n".join(lines) def __bool__(self): return not self.area_is_empty(0, self.width, 0, self.height) class Box: def __init__(self, left, right, top, bottom): assert left <= right and top <= bottom self.left = left self.right = right self.top = top self.bottom = bottom def sides(self): # these ranges should be fully exclusive to avoid the literal "corner cases" for x in range(self.left+1, self.right): yield (x, self.top), UP yield (x, self.bottom), DOWN for y in range(self.top+1, self.bottom): yield (self.left, y), LEFT yield (self.right, y), RIGHT def __repr__(self): return f"Box(left={self.left}, right={self.right}, top={self.top}, bottom={self.bottom})" def yield_side(drawing, at, d): to_yield = [] while drawing.has(at, d): to_yield.append((at, d)) at = onward(at, d) to_yield.append((at, behind(d))) if not drawing.has(at, behind(d)): return None, [] return at, to_yield def consume_box(drawing, at): to_consume = [] ats = [] for d in (RIGHT, DOWN, LEFT, UP): ats.append(at) at, l = yield_side(drawing, at, d) if not l: return None to_consume.extend(l) box = Box(ats[0][0], ats[2][0], ats[0][1], ats[2][1]) if at != ats[0] or not drawing.area_is_empty(box.left+1, box.right-1, box.top+1, box.bottom-1): return None for p, d in to_consume: drawing.erase(p, d) return box def yield_line(drawing, at, d): segments = [] while True: b = behind(d) segments.append((at, b)) if not drawing.has(at, b): return segments for x in (LEFT, RIGHT, DOWN, UP): if x == b: continue if drawing.has(at, x): at = onward(at, x) d = x break else: return segments def entry(s): drawing = Drawing.parse(s) boxes = [] for point in drawing.points(): if box := consume_box(drawing, point): boxes.append(box) connections = set() for box in boxes: for at, d in box.sides(): if not drawing.has(at, d): continue line = yield_line(drawing, onward(at, d), d) for box2 in boxes: if (box, box2) in connections or (box2, box) in connections or box == box2: continue if line[-1] not in list(box2.sides()): continue connections.add((box, box2)) drawing.erase(at, d) for p, back in line: drawing.erase(p, back) drawing.erase(p, behind(back)) break # if we deleted everything, it's correct return not drawing |
post a comment