3 filename=${1:-17E_8S_25.txt}
5 declare -a possible_directions=("E" "S" "W" "N")
10 declare -g -a instructions
12 while read -u 3 line; do
13 instructions+=( $line )
26 ship_north=$((ship_north+$amount))
31 go_north $((amount*-1))
36 ship_east=$((ship_east+$amount))
40 go_east $((amount*-1))
69 # well, we want amount to be done
71 amount=$((amount / 90))
72 amount=$((amount*mult))
76 # find where current direction is in the array
77 for a in ${!possible_directions[@]}; do
78 if [ ${possible_directions[$a]} == $ship_direction ]; then
84 dir_pos=$((dir_pos+$amount))
85 dir_pos=$((dir_pos % ${#possible_directions[@]}))
86 ship_direction=${possible_directions[$dir_pos]}
95 waypoint_north=$((waypoint_north+$amount))
98 waypoint_north=$((waypoint_north-$amount))
101 waypoint_east=$((waypoint_east+$amount))
104 waypoint_east=$((waypoint_east-$amount))
111 local steps=$((degrees/90))
114 if [ $steps -lt 0 ]; then
118 for (( a=0; a<steps; a++ )); do
120 waypoint_north=$((waypoint_east*-1))
125 do_move_ship_to_waypoint() {
127 ship_east=$((ship_east+($times*$waypoint_east)))
128 ship_north=$((ship_north+($times*$waypoint_north)))
133 for instruction in "${instructions[@]}"; do
134 direction=${instruction:0:1}
135 count=${instruction:1}
139 do_move $direction $count
142 do_move $ship_direction $count
153 echo " Ship ended at $ship_east E, $ship_north N"
157 echo " Manhatten distance: $md"
166 for instruction in "${instructions[@]}"; do
167 direction=${instruction:0:1}
168 count=${instruction:1}
172 do_waypoint_move $direction $count
175 do_move_ship_to_waypoint $count
178 do_waypoint_turn $((count*-1))
181 do_waypoint_turn $((count))
186 echo " Ship ended at $ship_east E, $ship_north N"
190 echo " Manhatten distance: $md"