Fixes / changes to re-run against the google login data
[advent-of-code-2019.git] / day03 / wires_2.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 filename=${1:-input.txt}
19
20 # read in data from file instead
21 exec 3<"$filename"
22 IFS="," read -u 3 -a wire_1
23 IFS="," read -u 3 -a wire_2
24
25 trace_wire() {
26     local -n w=$1
27     local -n b=$2
28     local -n c=$3
29     local -n w1cost=$4
30     wire_number=$5
31     cur_vert=0
32     cur_horz=0
33     moved=0
34     last_replace_char="-"
35     cost=0
36
37     for operation in "${w[@]}"; do
38         direction=${operation:0:1}
39         amount=${operation:1}
40         case $direction in
41             U)
42                 operator=+
43                 ;;
44             D)
45                 operator=-
46                 ;;
47             L)
48                 operator=-
49                 ;;
50             R)
51                 operator=+
52                 ;;
53         esac
54
55         case $direction in
56             U|D)
57                 for (( moved=1; moved <= $amount; moved++ )); do
58                     cur_vert=$((cur_vert${operator}1))
59                     cost=$((cost+1))
60                     if [ $wire_number -eq 1 ]; then
61                         if ! [ ${w1cost[$cur_horz,$cur_vert]+a} ]; then
62                             w1cost[$cur_horz,$cur_vert]=$cost
63                         fi
64                     fi
65                     if [ ${b[$cur_horz,$cur_vert]+a} ] && [ $wire_number -eq 2 ]; then
66                         if [ ${b[$cur_horz,$cur_vert]} -eq 1 ]; then
67                             cross_wires[$cur_horz,$cur_vert]=$((${w1cost[$cur_horz,$cur_vert]}+$cost))
68                             b[$cur_horz,$cur_vert]=3
69                         fi
70                         continue
71                     fi
72                     b[$cur_horz,$cur_vert]=$wire_number
73                 done
74                 ;;
75             L|R)
76                 for (( moved=1; moved <= $amount; moved++ )); do
77                     cur_horz=$((cur_horz${operator}1))
78                     cost=$((cost+1))
79                     if [ $wire_number -eq 1 ]; then
80                         if ! [ ${w1cost[$cur_horz,$cur_vert]+a} ]; then
81                             w1cost[$cur_horz,$cur_vert]=$cost
82                         fi
83                     fi
84                     if [ ${b[$cur_horz,$cur_vert]+a} ] && [ $wire_number -eq 2 ]; then
85                         if [ ${b[$cur_horz,$cur_vert]} -eq 1 ]; then
86                             cross_wires[$cur_horz,$cur_vert]=$((${w1cost[$cur_horz,$cur_vert]}+$cost))
87                             b[$cur_horz,$cur_vert]=3
88                         fi
89                         continue
90                     fi
91                     b[$cur_horz,$cur_vert]=$wire_number
92                 done
93                 ;;
94         esac
95     done
96 }
97
98 declare -A board
99 declare -A cross_wires
100 declare -A wire_1_cost
101 echo "Tracing first wire"
102 trace_wire wire_1 board cross_wires wire_1_cost 1
103 echo "Tracing second wire"
104 trace_wire wire_2 board cross_wires wire_1_cost 2
105
106 echo "Wires cross at:"
107 min_cost=-1
108 for cross in "${!cross_wires[@]}"; do
109     echo "  $cross ${cross_wires[$cross]}"
110     cost=${cross_wires[$cross]}
111     if [ $min_cost -lt 0 ] || [ $min_cost -gt $cost ]; then
112         min_cost=$cost
113     fi
114 done
115
116 echo Min cost: $min_cost