Fixes / changes to re-run against the google login data
[advent-of-code-2019.git] / day03 / 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 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     wire_number=$4
30     cur_vert=0
31     cur_horz=0
32     moved=0
33     last_replace_char="-"
34
35     for operation in "${w[@]}"; do
36         direction=${operation:0:1}
37         amount=${operation:1}
38         case $direction in
39             U)
40                 operator=+
41                 ;;
42             D)
43                 operator=-
44                 ;;
45             L)
46                 operator=-
47                 ;;
48             R)
49                 operator=+
50                 ;;
51         esac
52
53         case $direction in
54             U|D)
55                 for (( moved=1; moved <= $amount; moved++ )); do
56                     cur_vert=$((cur_vert${operator}1))
57                     if [ ${b[$cur_horz,$cur_vert]+a} ] && [ $wire_number -eq 2 ]; then
58                         if [ ${b[$cur_horz,$cur_vert]} -eq 1 ]; then
59                             c+=($cur_horz,$cur_vert)
60                             b[$cur_horz,$cur_vert]=3
61                         fi
62                         continue
63                     fi
64                     b[$cur_horz,$cur_vert]=$wire_number
65                 done
66                 ;;
67             L|R)
68                 for (( moved=1; moved <= $amount; moved++ )); do
69                     cur_horz=$((cur_horz${operator}1))
70                     if [ ${b[$cur_horz,$cur_vert]+a} ] && [ $wire_number -eq 2 ]; then
71                         if [ ${b[$cur_horz,$cur_vert]} -eq 1 ]; then
72                             c+=($cur_horz,$cur_vert)
73                             b[$cur_horz,$cur_vert]=3
74                         fi
75                         continue
76                     fi
77                     b[$cur_horz,$cur_vert]=$wire_number
78                 done
79                 ;;
80         esac
81     done
82 }
83
84 declare -A board
85 declare -a cross_wires
86 echo "Tracing first wire"
87 trace_wire wire_1 board cross_wires 1
88 echo "Tracing second wire"
89 trace_wire wire_2 board cross_wires 2
90
91 echo "Wires cross at:"
92 min_distance=-1
93 for cross in "${cross_wires[@]}"; do
94     echo "  $cross"
95     horz=${cross%,*}
96     horz=${horz//-/}
97     vert=${cross#*,}
98     vert=${vert//-/}
99     distance=$((horz+$vert))
100     if [ $min_distance -lt 0 ] || [ $min_distance -gt $distance ]; then
101         min_distance=$distance
102     fi
103 done
104
105 echo Min distance: $min_distance