A switch that forgets

Real switches forget entries that have not been heard from for a while, so a table does not grow forever and a machine that left is not remembered.

What to build

A program that runs a learning switch whose entries expire after T seconds without a frame from that source.

Requirements

  • Before each frame at time t, remove every entry whose last frame is more than T seconds older than t (that is, t − last_seen > T).
  • Then learn the source with the current time and port, and decide: flood (destination ff or unknown), filter (destination behind the arrival port), or forward P.
  • After the last frame print table and then the entries as NAME=PORT, in alphabetical order, on one line.

Input

Line 1: T and n. Then n lines time src dst port, with times in non-decreasing order.

Output

One action line per frame, then one line table NAME=PORT ....

Constraints

1 ≤ n ≤ 100; 0 ≤ T ≤ 1000. The name ff is never a source.

Example

Input:

10 3
0 A B 1
5 B A 2
20 A B 1

Output:

flood
forward 1
flood
table A=1

Why it works

Each source frame refreshes its entry. An entry that is not refreshed for more than T seconds is removed, so the switch floods again until it hears from that machine, which is how it copes with machines that move or go away.

Examples

Example 1 · Input
10 3
0 A B 1
5 B A 2
20 A B 1
Expected output
flood
forward 1
flood
table A=1
Example 2 · Input
5 3
0 A B 1
1 B A 2
3 A B 1
Expected output
flood
forward 1
forward 2
table A=1 B=2

Prompts inside input() are ignored when checking, so input("n: ") is fine.

Hints

Hint 1

Store for each name both the port and the time it was last seen as a source.

Hint 2

Before handling a frame at time t, delete every entry with t minus last_seen greater than T.

Hint 3

Then learn the source (update port and time), then decide as in a normal learning switch.

Solution

Show one correct solution (try it yourself first)
t_max, n = map(int, input().split())
table = {}
for _ in range(n):
    t, src, dst, port = input().split()
    t, port = int(t), int(port)
    for name in list(table):
        if t - table[name][1] > t_max:
            del table[name]
    table[src] = (port, t)
    if dst == "ff" or dst not in table:
        print("flood")
    elif table[dst][0] == port:
        print("filter")
    else:
        print("forward", table[dst][0])
print("table", " ".join(name + "=" + str(table[name][0]) for name in sorted(table)))

▶ Open this solution in the compiler, then press Visualize to watch it run step by step

Lesson: Ethernet, MAC addresses and switches →