Day 5
[advent-of-code-2021.git] / day05 / vents.sh
1 #!/bin/bash
2
3 set -e
4 set -u
5
6 filename="${1:-example.txt}"
7
8 exec 3<"$filename"
9
10 declare -a lines
11
12 max_x=0
13 max_y=0
14 x1=0
15 y1=0
16 x2=0
17 y2=0
18
19 declare -a grid
20
21 get_coords() {
22     coords_line="$1"
23     local -n lx1=$2
24     local -n ly1=$3
25     local -n lx2=$4
26     local -n ly2=$5
27
28     local first_x_y=${coords_line% -> *}
29     local second_x_y=${coords_line#* -> }
30     lx1=${first_x_y%,*}
31     ly1=${first_x_y#*,}
32     lx2=${second_x_y%,*}
33     ly2=${second_x_y#*,}
34 }
35
36 generate_grid() {
37     local max_x=$1
38     local max_y=$2
39     local -n grid_arr=$3
40
41     for (( i=0; i<$max_y; i++ )); do
42         for (( j=0; j<$max_x; j++ )); do
43             grid_arr+=(0)
44         done
45     done
46 }
47
48 display_grid() {
49     local max_x=$1
50     local -n grid_arr=$2
51
52     local cur_pos=0
53
54     while (( $cur_pos < ${#grid_arr[@]} )); do
55         case ${grid_arr[$cur_pos]} in
56             0)
57                 echo -n .
58                 ;;
59             *)
60                 echo -n ${grid_arr[$cur_pos]}
61                 ;;
62         esac
63         ((cur_pos+=1))
64         if [ $((cur_pos % ($max_x))) -eq 0 ] && [ $cur_pos -gt 0 ]; then
65             echo
66         fi
67     done
68 }
69
70 get_intersections() {
71     local -n grid_arr=$1
72     local count=0
73
74     for num in "${grid_arr[@]}"; do
75         if [ $num -gt 1 ]; then
76             ((count+=1))
77         fi
78     done
79
80     echo $count
81 }
82
83 while read -u3 line; do
84     lines+=("$line")
85     get_coords "$line" x1 y1 x2 y2
86     if [ $x1 -gt $max_x ]; then
87         max_x=$x1
88     fi
89     if [ $x2 -gt $max_x ]; then
90         max_x=$x2
91     fi
92     if [ $y1 -gt $max_y ]; then
93         max_y=$y1
94     fi
95     if [ $y2 -gt $max_y ]; then
96         max_y=$y2
97     fi
98 done
99
100 # because we're 0 based, max_x and max_y want 1 added to make the maths work
101 ((max_x+=1))
102 ((max_y+=1))
103
104 echo "Size of box: $max_x, $max_y"
105
106 generate_grid $max_x $max_y grid
107
108 for coords in "${lines[@]}"; do
109     get_coords "$coords" x1 y1 x2 y2
110     if [ $x1 -eq $x2 ] || [ $y1 -eq $y2 ]; then
111         if [ $x1 -eq $x2 ]; then
112             # we're working on a vertical line
113             if [ $y1 -gt $y2 ]; then
114                 ytemp=$y1
115                 y1=$y2
116                 y2=$ytemp
117             fi
118             for (( i=$y1; i<=$y2; i+=1 )); do
119                 cur_pos=$(($x1 + ($max_x * $i)))
120                 ((grid[$cur_pos]+=1))
121             done
122         elif [ $y1 -eq $y2 ]; then
123             direction=1
124             # we're working on a horizonal line
125             if [ $x1 -gt $x2 ]; then
126                 xtemp=$x1
127                 x1=$x2
128                 x2=$xtemp
129             fi
130             for (( i=$x1; i<=$x2; i+=1 )); do
131                 cur_pos=$(($i + ($max_x * $y1)))
132                 ((grid[$cur_pos]+=1))
133             done
134         fi
135     fi
136 done
137
138 #display_grid $max_x grid
139
140 intersection_count=$(get_intersections grid)
141
142 echo "Intersections (h+v): $intersection_count"
143
144 # do the diagonals
145 for coords in "${lines[@]}"; do
146     get_coords "$coords" x1 y1 x2 y2
147     if [ $x1 -ne $x2 ] && [ $y1 -ne $y2 ]; then
148         # we've got a diagonal line, woo
149         if [ $y1 -gt $y2 ]; then
150             # swap everything
151             temp=$y1
152             y1=$y2
153             y2=$temp
154             temp=$x1
155             x1=$x2
156             x2=$temp
157         fi
158         cur_x=$x1
159         for (( i=$y1; i<=$y2; i++ )); do
160             cur_pos=$((cur_x + ($max_x * i)))
161             ((grid[$cur_pos]+=1))
162             if [ $x1 -gt $x2 ]; then
163                 ((cur_x-=1)) || cur_x=0
164             else
165                 ((cur_x+=1))
166             fi
167         done
168     fi
169 done
170
171 #display_grid $max_x grid
172
173 intersection_count=$(get_intersections grid)
174
175 echo "Intersections (h+v+d): $intersection_count"