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)