Skip to content

Instantly share code, notes, and snippets.

@Savageman
Created April 26, 2025 19:13
Show Gist options
  • Save Savageman/b41e7722ac7f01213b211ee829d461e0 to your computer and use it in GitHub Desktop.
Save Savageman/b41e7722ac7f01213b211ee829d461e0 to your computer and use it in GitHub Desktop.
Code for the 1000 subscribers challenge at The Coder Cafe: https://www.thecoder.cafe/p/1000
#!/bin/bash
OIFS=$IFS
IFS=$'\n'
primary=""
declare -A routes
declare -a arrivals
for line in $(cat "$1"); do
IFS=$OIFS
read _ type _ <<< "$line"
if [[ "$type" == "TRANSMISSION:" ]]
then
read _ _ _ from _ to _ _ capacity _ <<< "$line"
echo "from=$from / to=$to / capacity=$capacity"
routes[$from:$to]=$capacity
fi
if [[ "$type" == "ALERT:" ]]
then
read _ _ _ _ _ primary _ <<< "$line"
echo "primary=$primary"
fi
if [[ "$type" == "CRITICAL:" ]]
then
read _ _ _ _ _ _ end <<< "$line"
IFS=', ' read -a arrivals <<< "$end"
OFS=','
echo "arrivals=[${arrivals[@]}]"
fi
done
declare -i total=0
for i in "${!arrivals[@]}"; do
arrival="${arrivals[$i]}"
route="$primary:$arrival"
capacity="${routes[$route]:-0}"
total=$((total + capacity))
printf "arrivals[%d]='%s' / route='%s' / capacity=%d / total=%d\n" "$i" "$arrival" "$route" "$capacity" "$total"
done
echo "Total passengers from $primary to [${arrivals[@]}] is $total"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment