6 # these wires should give 6
10 # these wires should get 159
11 #wire_1=(R75 D30 R83 U83 L12 D49 R71 U7 L72)
12 #wire_2=(U62 R66 U55 R34 D71 R55 D58 R83)
14 # these wires should get 135
15 wire_1=(R98 U47 R26 D63 R33 U87 L62 D20 R33 U53 R51)
16 wire_2=(U98 R91 D20 R16 D67 R40 U7 R15 U6 R7)
18 # read in data from file instead
20 #IFS="," read -u 3 -a wire_1
21 #IFS="," read -u 3 -a wire_2
31 for instruction in "${wire[@]}"; do
32 direction=${instruction:0:1}
33 amount=${instruction:1}
36 cur_vert=$((cur_vert+$amount))
37 if [ $cur_vert -gt $vert_max ]; then
42 cur_vert=$((cur_vert-$amount))
43 if [ $cur_vert -lt $vert_min ]; then
48 cur_horz=$((cur_horz+$amount))
49 if [ $cur_horz -gt $horz_max ]; then
54 cur_horz=$((cur_horz-$amount))
55 if [ $cur_horz -lt $horz_min ]; then
62 echo "$horz_min,$horz_max,$vert_min,$vert_max"
75 for operation in "${w[@]}"; do
76 direction=${operation:0:1}
96 offset=$((0-$min_left))
97 offset=$((offset+$cur_horz))
98 for (( moved=1; moved <= $amount; moved++ )); do
99 cur_vert=$((cur_vert${operator}1))
100 cur_line=${b[$cur_vert]}
101 cur_char=${cur_line:$offset:1}
103 if [ $cur_char = "-" ] || [ $cur_char = "|" ]; then
105 c+=($cur_horz,$cur_vert)
107 if [ $moved -eq $amount ]; then
110 if [ $cur_vert -eq 0 ] && [ $cur_horz -eq 0 ]; then
113 new_line="${cur_line:0:$((offset))}${replace_char}${cur_line:$((offset+1))}"
114 b[$cur_vert]=$new_line
118 last_replace_char="-"
119 # this could change multiple characters in the line
120 for (( moved=1; moved <= $amount; moved++ )); do
121 cur_horz=$((cur_horz${operator}1))
122 offset=$((0-$min_left))
123 offset=$((offset+$cur_horz))
124 cur_line=${b[$cur_vert]}
125 cur_char=${cur_line:$offset:1}
127 if [ $cur_char = "-" ] || [ $cur_char = "|" ]; then
129 c+=($cur_horz,$cur_vert)
131 if [ $moved -eq $amount ]; then
134 if [ $cur_vert -eq 0 ] && [ $cur_horz -eq 0 ]; then
137 new_line="${cur_line:0:$((offset))}${replace_char}${cur_line:$((offset+1))}"
138 b[$cur_vert]=$new_line
144 cur_line=${b[$cur_vert]}
145 offset=$((0-$min_left))
146 offset=$((offset+$cur_horz))
147 if [ ${cur_line:$offset:1} != 'X' ]; then
148 new_line="${cur_line:0:$((offset))}${last_replace_char}${cur_line:$((offset+1))}"
149 b[$cur_vert]=$new_line
159 if [ $min_horz -gt 0 ]; then
164 if [ $min_vert -gt 0 ]; then
169 total_horz=$(($max_horz - $min_horz))
174 blank_string=$(printf ".%.0s" $(eval echo '{0..'$total_horz'}'))
175 while [[ $cur_vert -le ${max_vert} ]]; do
176 if [[ $cur_vert -eq 0 ]]; then
177 offset=$((0-${mm[0]}))
178 line="${blank_string:0:$offset}o${blank_string:$((offset+1))}"
181 b[$cur_vert]=$blank_string
183 cur_vert=$((cur_vert+1))
187 echo "Getting board boundaries"
188 IFS="," read -a min_max < <(get_min_max wire_1)
189 IFS="," read -a min_max_2 < <(get_min_max wire_2)
191 # do the minimums first
193 if [ ${min_max[$pos]} -gt ${min_max_2[$pos]} ]; then
194 min_max[$pos]=${min_max_2[$pos]}
200 if [ ${min_max[$pos]} -lt ${min_max_2[$pos]} ]; then
201 min_max[$pos]=${min_max_2[$pos]}
205 echo " ${min_max[@]}"
207 # we now know exactly how big the grid is, which is handy, as we'll use it to do offsets
209 declare -a cross_wires
210 echo "Building board"
211 make_board min_max board
212 echo "Tracing first wire"
213 trace_wire wire_1 board cross_wires ${min_max[0]}
214 echo "Tracing second wire"
215 trace_wire wire_2 board cross_wires ${min_max[0]}
217 for (( a=${min_max[3]}; a>=${min_max[2]}; a-- )); do
221 echo "Wires cross at:"
223 for cross in "${cross_wires[@]}"; do
229 distance=$((horz+$vert))
230 if [ $min_distance -lt 0 ] || [ $min_distance -gt $distance ]; then
231 min_distance=$distance
235 echo Min distance: $min_distance