summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTomasz Kramkowski <tk@the-tk.com>2021-12-13 10:12:10 +0000
committerTomasz Kramkowski <tk@the-tk.com>2021-12-13 10:12:10 +0000
commitdee3d1832176a0caa60770e87ff668811c1ebd8e (patch)
tree44cc5e910720315c8b50c5070f4b739bb23219ba
parent95983a345218edff91ac88a81960babcb2749539 (diff)
downloadaoc2021-dee3d1832176a0caa60770e87ff668811c1ebd8e.tar.gz
aoc2021-dee3d1832176a0caa60770e87ff668811c1ebd8e.tar.xz
aoc2021-dee3d1832176a0caa60770e87ff668811c1ebd8e.zip
day 13
-rw-r--r--12.py6
-rw-r--r--13.py41
2 files changed, 44 insertions, 3 deletions
diff --git a/12.py b/12.py
index f471e07..c46fbe6 100644
--- a/12.py
+++ b/12.py
@@ -10,7 +10,7 @@ with open_day(12) as f:
adj[a].add(b)
adj[b].add(a)
-def count_paths(look_twice=False):
+def count_paths(adj, look_twice=False):
@cache
def f(node, seen, saw_once=None):
def descend(node, seen, saw_once):
@@ -27,5 +27,5 @@ def count_paths(look_twice=False):
return total + descend(node, seen, saw_once)
return sum(f(n, frozenset(('start',))) for n in adj['start'])
-print(count_paths())
-print(count_paths(True))
+print(count_paths(adj))
+print(count_paths(adj, True))
diff --git a/13.py b/13.py
new file mode 100644
index 0000000..4003f9c
--- /dev/null
+++ b/13.py
@@ -0,0 +1,41 @@
+from utils import open_day
+from dataclasses import dataclass
+
+@dataclass
+class Point:
+ x: int
+ y: int
+ @staticmethod
+ def from_str(s: str) -> 'Point':
+ return Point(*map(int, s.split(',', maxsplit=1)))
+
+@dataclass
+class Fold:
+ axis: str
+ pos: int
+ @staticmethod
+ def from_str(s: str) -> 'Fold':
+ axis, pos = s.split(' ')[2].split('=', maxsplit=1)
+ return Fold(axis, int(pos))
+
+with open_day(13) as f:
+ parts = f.read().rstrip().split('\n\n')
+ points = [Point.from_str(l) for l in parts[0].split('\n')]
+ folds = [Fold.from_str(l) for l in parts[1].split('\n')]
+
+def fold(points: list[Point], fold: Fold):
+ for i in range(len(points)):
+ point_pos = getattr(points[i], fold.axis)
+ if point_pos > fold.pos:
+ setattr(points[i], fold.axis, 2 * fold.pos - point_pos)
+
+for i, f in enumerate(folds):
+ fold(points, f)
+ if i == 0: print(len(set((p.x, p.y) for p in points)))
+maxx = min(f.pos for f in folds if f.axis == 'x')
+maxy = min(f.pos for f in folds if f.axis == 'y')
+points = set((p.x, p.y) for p in points)
+for y in range(maxy):
+ for x in range(maxx):
+ print('#' if (x, y) in points else '.', end='')
+ print()