advent-of-code/2024-python/output/day_14.py
Anders Englöf Ytterström e6307795a4 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.
2025-01-05 00:06:18 +01:00

55 lines
1.7 KiB
Python

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)