From e6307795a499a0bb7b8e74af73cf5853cc561fe3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anders=20Engl=C3=B6f=20Ytterstr=C3=B6m?= Date: Sun, 15 Dec 2024 13:15:39 +0100 Subject: [PATCH] Solve 2024:14 p1-2 "Restroom Redoubt" After working overtime to solve day 12, this brief task was a much welcome change. For pt 1, the code uses integer division and modulus to update all positions. The tricky part was to determine which quadrant got the middle column and row (since dimensions was odd numbers, and either left or right quandrants is +1 longer), which was straightforward to verify by the test cases. For pt 2, the remains of the code used to visually identify the tree is left under the `find_easter_egg` toggle. Basically, the code prints the grid with all robots marked as a `#`, and free space as `.`.` This wording of the puzzle is important: > very rarely, _most of the robots_ should arrange > themselves into a picture of a Christmas tree. "most of the robots" means the tree will be quite visible, which also means it is safe to asume a significant cluster of "#" would appear over time. By keeping track of the counter (seconds the robots has traveled), it was evident that the cluster of `#` occoured th first time at i=64 and every 103th time forward. In other words, `i % 103 == 64` will most likely give a tree. The print statement is therefore limited to those i's, and at `i == easter_egg_appearance` the tree is visible. So, to be extra clear: 64, 103 and pt2 answer is unique to my puzzle input. If this code is used with any other puzzle input, these numbers will most likely vary. For the fun, the code also contains the `display_easter_egg` flag to actually print the tree. This is provided to indicate how big the actual tree is: 33*36 chars. Also, the `sints` utility function was added to extract all signed ints from a string. --- 2024-python/output/__init__.py | 5 ++++ 2024-python/output/day_14.py | 55 ++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 2024-python/output/day_14.py diff --git a/2024-python/output/__init__.py b/2024-python/output/__init__.py index 26611f0..4ad56dc 100644 --- a/2024-python/output/__init__.py +++ b/2024-python/output/__init__.py @@ -42,6 +42,11 @@ def ints(s): return [int(n) for n in re.findall(r"\d+", s)] +def sints(s): + """Extract all signed integers from a string""" + return [int(n) for n in re.findall(r"-?\d+", s)] + + def mhd(a, b): """Calculates the Manhattan distance between 2 positions in the format (y, x) or (x, y)""" ar, ac = a diff --git a/2024-python/output/day_14.py b/2024-python/output/day_14.py new file mode 100644 index 0000000..7d58cf9 --- /dev/null +++ b/2024-python/output/day_14.py @@ -0,0 +1,55 @@ +from output import sints, vdbg + + +def solve(data, find_easter_egg=False, display_easter_egg=True): + W = 101 + H = 103 + easter_egg_recurring = 103 + easter_egg_offset = 64 + robots = [sints(line) for line in data.splitlines()] + state = {} + for id, robot in enumerate(robots): + x, y, *_ = robot + state[id] = (x, y) + i = 0 + easter_egg_appearance = 7892 + while i < easter_egg_appearance: + if i == 100: + midW = W // 2 + midH = H // 2 + safety_factor_t100 = ( + sum(0 <= x < midW and 0 <= y < midH for x, y in state.values()) + * sum(midW < x < W and 0 <= y < midH for x, y in state.values()) + * sum(0 <= x < midW and midH < y < H for x, y in state.values()) + * sum(midW < x < W and midH < y < H for x, y in state.values()) + ) + for id, props in enumerate(robots): + _ix, _iy, vx, vy = props + x, y = state[id] + state[id] = ((x + vx) % W, (y + vy) % H) + i += 1 + if find_easter_egg: + if i % easter_egg_recurring == easter_egg_offset: + vdbg(state.values(), H, W) + print(i) + input("--- any key pls ---") + if display_easter_egg: + easter_egg = [ + (y - 26, x - 23) + for x, y in state.values() + if 20 <= x <= 53 and 24 <= y <= 60 + ] + max_y, max_x = max(easter_egg) + vdbg(easter_egg, max_y + 1, max_x + 1) + print("") + return safety_factor_t100, easter_egg_appearance + + +if __name__ == "__main__": + with open("./input/14.txt", "r") as f: + inp = f.read().strip() + + p1, p2 = solve(inp, display_easter_egg=False) + + print(p1) + print(p2)