|
| 1 | +import sys |
| 2 | +import re |
| 3 | +from collections import namedtuple |
| 4 | +import gridutil.coord as cu |
| 5 | +import gridutil.grid as gu |
| 6 | + |
| 7 | + |
| 8 | +Instruction = namedtuple("Instruction", ["direction", "dist", "colour"]) |
| 9 | +PARSE_RE = re.compile(r"([RDUL]) (\d+) \(#([a-f\d]{6})\)") |
| 10 | +DIRECTION_TRANSFORMATION = { |
| 11 | + "R": cu.Direction.Right, |
| 12 | + "L": cu.Direction.Left, |
| 13 | + "U": cu.Direction.Up, |
| 14 | + "D": cu.Direction.Down, |
| 15 | + "0": cu.Direction.Right, |
| 16 | + "1": cu.Direction.Down, |
| 17 | + "2": cu.Direction.Left, |
| 18 | + "3": cu.Direction.Up, |
| 19 | +} |
| 20 | + |
| 21 | + |
| 22 | +def parse(instr: str) -> list[Instruction]: |
| 23 | + res = [] |
| 24 | + for line in instr.splitlines(): |
| 25 | + m = PARSE_RE.match(line) |
| 26 | + assert m is not None |
| 27 | + |
| 28 | + raw_dir, dist, colour = m.groups() |
| 29 | + parsed_dir = DIRECTION_TRANSFORMATION[raw_dir] |
| 30 | + assert parsed_dir is not None |
| 31 | + |
| 32 | + res.append(Instruction(parsed_dir, int(dist), colour)) |
| 33 | + return res |
| 34 | + |
| 35 | + |
| 36 | +def run(instructions: list[Instruction]) -> int: |
| 37 | + perimeter = 0 |
| 38 | + vertices = [cu.Coordinate(0, 0)] |
| 39 | + for instruction in instructions: |
| 40 | + perimeter += instruction.dist |
| 41 | + vertices.append( |
| 42 | + cu.add( |
| 43 | + vertices[-1], cu.mult(instruction.direction.delta(), instruction.dist) |
| 44 | + ) |
| 45 | + ) |
| 46 | + |
| 47 | + vertices = vertices[:-1] |
| 48 | + |
| 49 | + area = cu.area(vertices) |
| 50 | + |
| 51 | + # This is Pick's theorem. |
| 52 | + # Normally, we'd want to just get the internal area, which the Shoelace formula would do. |
| 53 | + # But since we want the area including walls that we assume are a single |
| 54 | + # unit thick, we apply Pick's theorem as this counts all coordinates that |
| 55 | + # the walls pass through, which in this case is effectively the same thing. |
| 56 | + return int(area + perimeter / 2) + 1 |
| 57 | + |
| 58 | + |
| 59 | +def one(instr: str): |
| 60 | + instructions = parse(instr) |
| 61 | + return run(instructions) |
| 62 | + |
| 63 | + |
| 64 | +def two(instr: str): |
| 65 | + instructions = parse(instr) |
| 66 | + for i, instruction in enumerate(instructions): |
| 67 | + instructions[i] = Instruction( |
| 68 | + DIRECTION_TRANSFORMATION[instruction.colour[-1]], |
| 69 | + int(instruction.colour[:5], base=16), |
| 70 | + "", |
| 71 | + ) |
| 72 | + return run(instructions) |
| 73 | + |
| 74 | + |
| 75 | +def _debug(*args, **kwargs): |
| 76 | + kwargs["file"] = sys.stderr |
| 77 | + print(*args, **kwargs) |
| 78 | + |
| 79 | + |
| 80 | +if __name__ == "__main__": |
| 81 | + if len(sys.argv) < 2 or sys.argv[1] not in ["1", "2"]: |
| 82 | + print("Missing day argument", file=sys.stderr) |
| 83 | + sys.exit(1) |
| 84 | + inp = sys.stdin.read().strip() |
| 85 | + if sys.argv[1] == "1": |
| 86 | + print(one(inp)) |
| 87 | + else: |
| 88 | + print(two(inp)) |
0 commit comments