Count to infinity

Routers A, B and C are in a line: A - B - C. The link B - C breaks. Watch A and B argue about the way to C.

What to build

A program that simulates distance vector after the failure, with and without split horizon.

Requirements

  • Start (round 0) with A = ab + bc and B = bc, both costs to C. In each round both routers update at the same time from the previous values.
  • Mode plain: B = min(inf, ab + A) and A = min(inf, ab + B). Mode split: A's route to C goes through B, so A does not tell B about it: B = inf, and A = min(inf, ab + B).
  • Print round k: A=x B=y for round 0 and for each following round, stopping at the first round in which both are inf. A cost never goes above the value of infinity given in the input.

Input

One line: the mode (plain or split), the cost ab of the link A - B, the cost bc that B had to C, and the value of infinity.

Output

One line per round.

Constraints

1 ≤ ab, bc; ab + bc < infinity ≤ 100.

Example

Input:

plain 1 1 16

Output:

round 0: A=2 B=1
round 1: A=2 B=3
round 2: A=4 B=3
round 3: A=4 B=5
round 4: A=6 B=5
round 5: A=6 B=7
round 6: A=8 B=7
round 7: A=8 B=9
round 8: A=10 B=9
round 9: A=10 B=11
round 10: A=12 B=11
round 11: A=12 B=13
round 12: A=14 B=13
round 13: A=14 B=15
round 14: A=16 B=15
round 15: A=16 B=16

Why it works

Without split horizon each router believes the other's old news, so the cost to the dead destination climbs by 2 per round until it reaches infinity. Split horizon stops B from believing A, so the bad news spreads in two rounds.

Examples

Example 1 · Input
plain 1 1 16
Expected output
round 0: A=2 B=1
round 1: A=2 B=3
round 2: A=4 B=3
round 3: A=4 B=5
round 4: A=6 B=5
round 5: A=6 B=7
round 6: A=8 B=7
round 7: A=8 B=9
round 8: A=10 B=9
round 9: A=10 B=11
round 10: A=12 B=11
round 11: A=12 B=13
round 12: A=14 B=13
round 13: A=14 B=15
round 14: A=16 B=15
round 15: A=16 B=16
Example 2 · Input
split 1 1 16
Expected output
round 0: A=2 B=1
round 1: A=2 B=16
round 2: A=16 B=16

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

Hints

Hint 1

Keep a and b, the costs that A and B have for C. Round 0 shows the starting values: a = ab + bc and b = bc.

Hint 2

plain: new_b = min(inf, ab + a) and new_a = min(inf, ab + b), both from the old values. split: new_b = inf, because A's route goes through B and is not advertised back.

Hint 3

Stop at the first round where both are inf. Print every round, including round 0.

Solution

Show one correct solution (try it yourself first)
mode, ab, bc, inf = input().split()
ab, bc, inf = int(ab), int(bc), int(inf)
a, b = ab + bc, bc
k = 0
print("round 0: A=" + str(a) + " B=" + str(b))
while not (a >= inf and b >= inf):
    k += 1
    if mode == "plain":
        new_b = min(inf, ab + a)
    else:
        new_b = inf
    new_a = min(inf, ab + b)
    a, b = new_a, new_b
    print("round " + str(k) + ": A=" + str(a) + " B=" + str(b))

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

Lesson: Routing algorithms: link state and distance vector →