Does it fit in the tunnel?

A VPN wraps each packet inside another, adding bytes. If the wrapped packet is bigger than the link's MTU, it must be split or rejected.

What to build

For each inner packet size, print the outer size and whether it fits.

Requirements

  • Line 1: the MTU and the overhead in bytes.
  • Each following line: an inner packet size.
  • Print inner X outer Y fits if Y ≤ MTU, otherwise inner X outer Y too big.
  • Finally print largest that fits: Z, which is MTU − overhead.

Input

As described.

Output

One line per packet, then one more.

Constraints

All numbers are whole, 1 to 100000.

Example

Input:

1500 60
500
1400
1480

Output:

inner 500 outer 560 fits
inner 1400 outer 1460 fits
inner 1480 outer 1540 too big
largest that fits: 1440

Why it works

The outer packet is the inner packet plus the wrapping, so it fits exactly when inner + overhead ≤ MTU, which makes MTU − overhead the largest inner packet.

Examples

Example 1 · Input
1500 60
500
1400
1480
Expected output
inner 500 outer 560 fits
inner 1400 outer 1460 fits
inner 1480 outer 1540 too big
largest that fits: 1440
Example 2 · Input
1500 80
1420
1421
Expected output
inner 1420 outer 1500 fits
inner 1421 outer 1501 too big
largest that fits: 1420

Hints

Hint 1

WireGuard over IPv4 adds 60 bytes, so on a 1500-byte link the largest inner packet is 1440.

Solution

Show one correct solution (try it yourself first)
const [mtu, overhead] = prompt().trim().split(/\s+/).map(Number);
let line = prompt();
while (line !== null) {
  if (line.trim()) {
    const inner = Number(line);
    const outer = inner + overhead;
    console.log(`inner ${inner} outer ${outer} ${outer <= mtu ? "fits" : "too big"}`);
  }
  line = prompt();
}
console.log(`largest that fits: ${mtu - overhead}`);

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

Lesson: VPNs and secure tunnels →