Part 1
In this puzzle, there is a list of firewalls consisting of stacks with different depths. Each firewall has its independent scanner starting at the top of its stack and then moving back and forth. At each time step, the scanner goes down one step until the end of the stack and then turns around. A packet is moving from one firewall to the next. If a packet arrives at a firewall when the scanner is at the top of the stack, the packet is caught. We are asked to find all firewalls, in which the packet is caught.For each firewall we are only interested if the scanner is at the top at a certain time. The movement of the scanner is periodically with a period of 2 * depth - 1. So to check if we are caught, we can use the modulus 2*depth - 1.
for depth in depths.keys():
period_at_depth = 2 * depths[depth] - 1
if depth % (period_at_depth - 1) == 0:
# we are caught
Part 2
In part 2, we do not start sending our packet right away but we can wait a certain number of steps instead. We should find the earliest time we can send the packet without it being caught in any of the firewalls.
We introduced a delay parameter increasing from 0 until we find a solution. We can reuse the check from before and change the time we arrive at a certain depth to depth + delay.
for depth in depths.keys():
period_at_depth = 2 * depths[depth] - 1
if (delay + depth) % (period_at_depth - 1) == 0:
# we care caught
The loop running until the solution was quick enough, so no improvements were necessary. But it could be improved, the problem seems a version of the Chinese remainder theorem.
No comments:
Post a Comment