From 43767f7b62907229924307e2f68639929862defb Mon Sep 17 00:00:00 2001 From: felixm Date: Sat, 23 Nov 2024 09:36:43 -0500 Subject: [PATCH] Solve 2021 day 12-14 --- 2021/d12.py | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2021/d13.py | 51 ++++++++++++++++++++++++++++++++++++++++++++++++ 2021/d14.py | 43 ++++++++++++++++++++++++++++++++++++++++ README.md | 5 ++++- 4 files changed, 154 insertions(+), 1 deletion(-) create mode 100644 2021/d12.py create mode 100644 2021/d13.py create mode 100644 2021/d14.py diff --git a/2021/d12.py b/2021/d12.py new file mode 100644 index 0000000..7ab7289 --- /dev/null +++ b/2021/d12.py @@ -0,0 +1,56 @@ +from lib import get_data +from lib import Grid2D +from lib import ints +from collections import deque +from collections import defaultdict + +data = get_data(__file__).strip() +g = defaultdict(list) + + +for line in data.splitlines(): + a, b = line.strip().split("-") + g[a].append(b) + g[b].append(a) + + +def no_doubles(path): + path = [p for p in path if p.islower()] + return len(path) == len(set(path)) + + +for part in [1, 2]: + start = ("start", ()) + visited = set() + queue = deque([start]) + + total = 0 + while queue: + vertex = queue.popleft() + if vertex in visited: + continue + visited.add(vertex) + + current, path = vertex + + neighbors = [] + for neighbor in g[current]: + if neighbor == "end": + total += 1 + elif neighbor == "start": + continue + elif neighbor.islower(): + if neighbor not in path or (part == 2 and no_doubles(path)): + new_path = tuple(list(path) + [neighbor]) + nb = (neighbor, new_path) + if nb not in visited: + queue.append(nb) + elif neighbor.isupper(): + new_path = tuple(list(path) + [neighbor]) + nb = (neighbor, new_path) + if nb not in visited: + queue.append(nb) + else: + assert False + + print(total) diff --git a/2021/d13.py b/2021/d13.py new file mode 100644 index 0000000..fb63e61 --- /dev/null +++ b/2021/d13.py @@ -0,0 +1,51 @@ +from lib import get_data +from lib import ints + +data = get_data(__file__).strip() + + +def print_from_xy(xs): + x_min = min(v[0] for v in xs) + x_max = max(v[0] for v in xs) + y_min = min(v[1] for v in xs) + y_max = max(v[1] for v in xs) + for y in range(y_min, y_max + 1): + row = "" + for x in range(x_min, x_max + 1): + if (x, y) in xs: + row += "#" + else: + row += " " + print(row) + + +p1, p2 = data.split("\n\n") +xs = set([tuple(ints(line)) for line in p1.splitlines()]) + +folds = [] +for line in p2.splitlines(): + d = "x" if "x=" in line else "y" + (n,) = ints(line) + folds.append((d, n)) + + +first = None +for d, n in folds: + if d == "x": + to_move = [(x, y) for (x, y) in xs if x > n] + for x, y in to_move: + xs.remove((x, y)) + new_x = n - (x - n) + xs.add((new_x, y)) + elif d == "y": + to_move = [(x, y) for (x, y) in xs if y > n] + for x, y in to_move: + xs.remove((x, y)) + new_y = n - (y - n) + xs.add((x, new_y)) + else: + assert False + first = first or len(xs) + +print(first) +print_from_xy(xs) diff --git a/2021/d14.py b/2021/d14.py new file mode 100644 index 0000000..cd6ff47 --- /dev/null +++ b/2021/d14.py @@ -0,0 +1,43 @@ +from lib import get_data +from itertools import pairwise +from collections import defaultdict + +data = get_data(__file__) +orig_t, p2 = data.split("\n\n") + +pairs = {} +for line in p2.splitlines(): + ls, rs = line.split(" -> ") + assert ls not in pairs + pairs[ls] = rs + +for steps in [10, 40]: + t = str(orig_t) + start_letter = t[0] + end_letter = t[-1] + td = defaultdict(int) + for a, b in pairwise(t): + td[a + b] += 1 + + for _ in range(steps): + ntd = defaultdict(int) + for pair, count in td.items(): + if pair in pairs: + a, b = pair + c = pairs[pair] + ntd[a + c] += count + ntd[c + b] += count + else: + ntd[pair] += count + td = ntd + + counts = defaultdict(int) + for (a, b), count in td.items(): + counts[a] += count + counts[b] += count + counts = {k: v // 2 for k, v in counts.items()} + counts[start_letter] += 1 + counts[end_letter] += 1 + + r = max(counts.values()) - min(counts.values()) + print(r) diff --git a/README.md b/README.md index 71ee3bb..0f4e189 100644 --- a/README.md +++ b/README.md @@ -183,7 +183,10 @@ Solutions and utility script for Advent of Code challenges in Python. - Day 9: 8:07 (37th okay, okay) - Day 10: 15:50 (I was pretty happy with that but double the 100th place.) - Day 11: 11:43 (Could have been much faster here.) -- Day 12: +- Day 12: 19:11 (Okayish, but of course too slow for leaderboard.) +- Day 13: 16:48 (Should have been way faster.) +- Day 14: 25:52 (Not hard but just too slow.) +- Day 15: ## AoC 2022