6 filename="${1:-example.txt}"
28 local first_x_y=${coords_line% -> *}
29 local second_x_y=${coords_line#* -> }
41 for (( i=0; i<$max_y; i++ )); do
42 for (( j=0; j<$max_x; j++ )); do
54 while (( $cur_pos < ${#grid_arr[@]} )); do
55 case ${grid_arr[$cur_pos]} in
60 echo -n ${grid_arr[$cur_pos]}
64 if [ $((cur_pos % ($max_x))) -eq 0 ] && [ $cur_pos -gt 0 ]; then
74 for num in "${grid_arr[@]}"; do
75 if [ $num -gt 1 ]; then
83 while read -u3 line; do
85 get_coords "$line" x1 y1 x2 y2
86 if [ $x1 -gt $max_x ]; then
89 if [ $x2 -gt $max_x ]; then
92 if [ $y1 -gt $max_y ]; then
95 if [ $y2 -gt $max_y ]; then
100 # because we're 0 based, max_x and max_y want 1 added to make the maths work
104 echo "Size of box: $max_x, $max_y"
106 generate_grid $max_x $max_y grid
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
118 for (( i=$y1; i<=$y2; i+=1 )); do
119 cur_pos=$(($x1 + ($max_x * $i)))
120 ((grid[$cur_pos]+=1))
122 elif [ $y1 -eq $y2 ]; then
124 # we're working on a horizonal line
125 if [ $x1 -gt $x2 ]; then
130 for (( i=$x1; i<=$x2; i+=1 )); do
131 cur_pos=$(($i + ($max_x * $y1)))
132 ((grid[$cur_pos]+=1))
138 #display_grid $max_x grid
140 intersection_count=$(get_intersections grid)
142 echo "Intersections (h+v): $intersection_count"
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
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
171 #display_grid $max_x grid
173 intersection_count=$(get_intersections grid)
175 echo "Intersections (h+v+d): $intersection_count"