#!/bin/bash declare -A rooms declare -a stack let 'max=-1^1<<63' input=${1:-input.txt} while read -n1 c do case $c in ^) rooms=() stack=() rooms[0 0]=0 x=0 y=0 dist=0 farthest=0 sp=-1 ;; \() stack[++sp]="x=$x y=$y dist=$dist" ;; \|) declare -i ${stack[sp]} ;; \)) declare -i ${stack[sp--]} ;; N) let --y ;;& S) let ++y ;;& E) let ++x ;;& W) let --x ;;& [NSEW]) if let "++dist < ${rooms[$x $y]:-$max}" then rooms[$x $y]=$dist let 'dist > farthest' && farthest=$dist fi ;; esac done <"$input" echo $farthest