Day 3 2019
[advent-of-code-2019.git] / day3 / wires.sh
1 #!/bin/bash
2
3 set -u
4 set -e
5
6 # these wires should give 6
7 #wire_1=(R8 U5 L5 D3)
8 #wire_2=(U7 R6 D4 L4)
9
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)
13
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)
17
18 # read in data from file instead
19 exec 3<input.txt
20 IFS="," read -u 3 -a wire_1
21 IFS="," read -u 3 -a wire_2
22
23 trace_wire() {
24     local -n w=$1
25     local -n b=$2
26     local -n c=$3
27     wire_number=$4
28     cur_vert=0
29     cur_horz=0
30     moved=0
31     last_replace_char="-"
32
33     for operation in "${w[@]}"; do
34         direction=${operation:0:1}
35         amount=${operation:1}
36         case $direction in
37             U)
38                 operator=+
39                 ;;
40             D)
41                 operator=-
42                 ;;
43             L)
44                 operator=-
45                 ;;
46             R)
47                 operator=+
48                 ;;
49         esac
50
51         case $direction in
52             U|D)
53                 for (( moved=1; moved <= $amount; moved++ )); do
54                     cur_vert=$((cur_vert${operator}1))
55                     if [ ${b[$cur_horz,$cur_vert]+a} ] && [ $wire_number -eq 2 ]; then
56                         if [ ${b[$cur_horz,$cur_vert]} -eq 1 ]; then
57                             c+=($cur_horz,$cur_vert)
58                             b[$cur_horz,$cur_vert]=3
59                         fi
60                         continue
61                     fi
62                     b[$cur_horz,$cur_vert]=$wire_number
63                 done
64                 ;;
65             L|R)
66                 for (( moved=1; moved <= $amount; moved++ )); do
67                     cur_horz=$((cur_horz${operator}1))
68                     if [ ${b[$cur_horz,$cur_vert]+a} ] && [ $wire_number -eq 2 ]; then
69                         if [ ${b[$cur_horz,$cur_vert]} -eq 1 ]; then
70                             c+=($cur_horz,$cur_vert)
71                             b[$cur_horz,$cur_vert]=3
72                         fi
73                         continue
74                     fi
75                     b[$cur_horz,$cur_vert]=$wire_number
76                 done
77                 ;;
78         esac
79     done
80 }
81
82 declare -A board
83 declare -a cross_wires
84 echo "Tracing first wire"
85 trace_wire wire_1 board cross_wires 1
86 echo "Tracing second wire"
87 trace_wire wire_2 board cross_wires 2
88
89 echo "Wires cross at:"
90 min_distance=-1
91 for cross in "${cross_wires[@]}"; do
92     echo "  $cross"
93     horz=${cross%,*}
94     horz=${horz//-/}
95     vert=${cross#*,}
96     vert=${vert//-/}
97     distance=$((horz+$vert))
98     if [ $min_distance -lt 0 ] || [ $min_distance -gt $distance ]; then
99         min_distance=$distance
100     fi
101 done
102
103 echo Min distance: $min_distance