#!/bin/bash # Function to convert a node number to its grid coordinates get_coordinates() { local node=$1 echo "$((node / 5)) $((node % 5))" } # Function to calculate the Chebyshev distance between two nodes chebyshev_distance() { local x1=$1 local y1=$2 local x2=$3 local y2=$4 # Calculate absolute values of differences local dx=$((x1 - x2)) local dy=$((y1 - y2)) dx=${dx#-} # Remove negative sign to get absolute value dy=${dy#-} # Remove negative sign to get absolute value # Return the maximum of the two distances echo $((dx > dy ? dx : dy)) } # Function to calculate the Manhattan distance between two nodes manhattan_distance() { local x1=$1 local y1=$2 local x2=$3 local y2=$4 # Calculate absolute values of differences and sum them local dx=$((x1 - x2)) local dy=$((y1 - y2)) dx=${dx#-} # Remove negative sign to get absolute value dy=${dy#-} # Remove negative sign to get absolute value echo $((dx + dy)) } # Function to calculate absolute value abs() { echo ${1#-} } # Check if exactly one argument is provided and it is in the range 1 to 4 if [[ $# -ne 1 || $1 -lt 1 || $1 -gt 4 ]]; then echo "Usage: $0 " exit 1 fi hops=$1 # Iterate over all pairs of nodes for start in {0..24}; do read x1 y1 <<< $(get_coordinates $start) for end in {0..24}; do if [[ $start -ne $end ]]; then read x2 y2 <<< $(get_coordinates $end) # Calculate Manhattan distance # distance=$(manhattan_distance $x1 $y1 $x2 $y2) distance=$(chebyshev_distance $x1 $y1 $x2 $y2) # If distance matches the given hops, print the pair if [[ $distance -eq $hops ]]; then echo "($start, $end)" fi fi done done