From 4ad7075785c2541fe04f4722757fe56b5950ae76 Mon Sep 17 00:00:00 2001 From: felixm Date: Sat, 14 Dec 2024 00:40:22 -0500 Subject: [PATCH] Solve day 2024 day 14 --- 2024/d14.py | 96 +++++++++++++++++++++++++++++++++++++++++++++++++++++ README.md | 26 +++++++-------- 2 files changed, 109 insertions(+), 13 deletions(-) create mode 100644 2024/d14.py diff --git a/2024/d14.py b/2024/d14.py new file mode 100644 index 0000000..afff48f --- /dev/null +++ b/2024/d14.py @@ -0,0 +1,96 @@ +from lib import get_data +from lib import ints +from collections import defaultdict + +rows = 103 +cols = 101 +data = get_data(__file__) + + +def score(robots): + quadrants = defaultdict(int) + for _, r, c in robots: + if r < rows // 2 and c < cols // 2: + quadrant = 1 # Top-left + quadrants[quadrant] += 1 + elif r < rows // 2 and c > cols // 2: + quadrant = 2 # Top-right + quadrants[quadrant] += 1 + elif r > rows // 2 and c < cols // 2: + quadrant = 3 # Bottom-left + quadrants[quadrant] += 1 + elif r > rows // 2 and c > cols // 2: + quadrant = 4 # Bottom-right + quadrants[quadrant] += 1 + else: + pass + + t = 1 + for q in quadrants.values(): + t *= q + return t + + +def get_easter_egg_coords(rows, cols): + coords = set() + center_y, center_x = rows // 2, cols // 2 + height = rows * 0.8 + width = cols * 0.6 + + for i in range(rows): + for j in range(cols): + squeeze = 1 - 0.3 * ((i - center_y + height / 4) / height) + egg_eq = (j - center_x) ** 2 / (width / 2) ** 2 + (i - center_y) ** 2 / ( + (height / 2 * squeeze) ** 2 + ) + + if egg_eq <= 1: + coords.add((i, j)) + + return coords + + +def print_from_xy(xs): + x_min = min(v[1] for v in xs) + x_max = max(v[1] for v in xs) + y_min = min(v[0] for v in xs) + y_max = max(v[0] 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) + + +robots_speed = {} +robots = list() +for i, line in enumerate(data.splitlines()): + c, r, vc, vr = ints(line) + robots_speed[i] = (vr, vc) + robots.append((i, r, c)) + +egg_coords = get_easter_egg_coords(rows, cols) + +max_count = 0 + +for j in range(10_000_000_000): + nrobots = list() + count = 0 + for i, r, c in robots: + vr, vc = robots_speed[i] + nr = (r + vr) % rows + nc = (c + vc) % cols + if (nr, nc) in egg_coords: + count += 1 + nrobots.append((i, nr, nc)) + robots = nrobots + max_count = max(count, max_count) + if j == 99: + print(score(robots)) + if count > 380: + print(j + 1) + # print_from_xy([(r, c) for _, r, c in robots]) + break diff --git a/README.md b/README.md index bb6719a..17a2e86 100644 --- a/README.md +++ b/README.md @@ -291,18 +291,18 @@ and focus. Of course, learning more algorithms and techniques helps. ## AoC 2024 -- Day 1: `00:01:30 124 0 00:02:49 141 0` -- Day 2: `00:05:34 686 0 00:08:21 531 0` -- Day 3: `00:01:48 165 0 00:02:40 56 45` -- Day 4: `00:07:47 1101 0 00:24:07 2410 0` -- Day 5: `00:07:07 679 0 00:23:02 1998 0` -- Day 6: `00:09:43 1082 0 00:16:29 464 0` -- Day 7: `00:12:29 2058 0 00:13:08 1170 0` -- Day 8: `00:15:59 1742 0 00:26:56 2190 0` -- Day 9: `00:48:23 6055 0 01:09:38 3159 0` -- Day 10: `00:19:47 2976 0 00:20:47 2244 0` -- Day 11: `00:05:13 642 0 00:33:07 2673 0` -- Day 12: `00:30:30 3540 0 11:41:33 16212 0` +- Day 1: `00:01:30 124 0 00:02:49 141 0` +- Day 2: `00:05:34 686 0 00:08:21 531 0` +- Day 3: `00:01:48 165 0 00:02:40 56 45` +- Day 4: `00:07:47 1101 0 00:24:07 2410 0` +- Day 5: `00:07:07 679 0 00:23:02 1998 0` +- Day 6: `00:09:43 1082 0 00:16:29 464 0` +- Day 7: `00:12:29 2058 0 00:13:08 1170 0` +- Day 8: `00:15:59 1742 0 00:26:56 2190 0` +- Day 9: `00:48:23 6055 0 01:09:38 3159 0` +- Day 10: `00:19:47 2976 0 00:20:47 2244 0` +- Day 11: `00:05:13 642 0 00:33:07 2673 0` +- Day 12: `00:30:30 3540 0 11:41:33 16212 0` - Day 13: `00:05:35 225 0 00:55:48 2544 0` -- Day 14: +- Day 14: `00:20:41 2136 0 00:35:14 1048 0`