From 9a350e8eeac76e32d16e4f6fa0bc0bf6e2e3c8b1 Mon Sep 17 00:00:00 2001 From: dece Date: Mon, 13 Dec 2021 09:31:16 +0100 Subject: [PATCH] Day 13 --- 2021/day13.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 2021/day13.py diff --git a/2021/day13.py b/2021/day13.py new file mode 100644 index 0000000..9953052 --- /dev/null +++ b/2021/day13.py @@ -0,0 +1,31 @@ +with open("input13.txt") as f: + coords = [] + folds = [] + for line in map(str.rstrip, f): + if "," in line: + coords.append(tuple(map(int, line.split(",")))) + elif line and line[0] == "f": + c, v = line.rsplit(maxsplit=1)[-1].split("=") + folds.append((c == "x", int(v))) + +for i, (is_h, v) in enumerate(folds): + if is_h: + coords = set( + (x - (x - v) * 2, y) if x > v else (x, y) + for x, y in coords + ) + else: + coords = set( + (x, y - (y - v) * 2) if y > v else (x, y) + for x, y in coords + ) + if i == 0: + print(len(coords)) + +dx = max(coords, key=lambda p: p[0])[0] + 1 +dy = max(coords, key=lambda p: p[1])[1] + 1 +screen = [[" "] * dx for _ in range(dy)] +for x, y in coords: + screen[y][x] = "█" +for row in screen: + print("".join(row))