8 filename="${1:-example.txt}"
14 while read -u 3 line; do
15 if [ "$line" == "" ]; then
20 if [ $x -gt $max_x ]; then
23 if [ $y -gt $max_y ]; then
32 while read -u 3 line; do
33 folds+=(${line#fold along })
37 for (( y=0; y<$max_y; y++ )); do
38 for (( x=0; x<$max_x; x++ )); do
39 case "${map[$x,$y]}" in
55 for (( y=0; y<$max_y; y++ )); do
56 for (( x=0; x<$max_x; x++ )); do
61 for point in "${points[@]}"; do
70 local point=${line#*=}
76 local new_max_x=$(($max_x - $point - 1))
77 if [ $point -gt $new_max_x ]; then
80 for (( y=0; y<$max_y; y++ )); do
81 for (( x=0; x<$new_max_x; x++ )); do
82 # be sensible instead of insane, work from the middle out
83 x_1=$(($point-$x-1)) # always one left of the point
84 x_2=$(($point+$x+1)) # always one right of the point
85 if [ $x_1 -lt 0 ]; then
88 if [ $x_2 -ge $max_x ]; then
91 new_x=$(($new_max_x-$x-1)) # work from the right of the new image, left
92 new_map["$new_x,$y"]=$((${map["$x_1,$y"]} | ${map["$x_2,$y"]}))
98 local new_max_y=$(($max_y - $point - 1))
99 if [ $point -gt $new_max_y ]; then
102 for (( y=0; y<$new_max_y; y++ )); do
103 y_1=$(($point-$y-1)) # always above the fold
104 y_2=$(($point+$y+1)) # always below the fold
105 if [ $y_1 -lt 0 ]; then
108 if [ $y_2 -ge $max_y ]; then
111 new_y=$(($new_max_y-$y-1)) # work from the bottom of the new image, up
112 for (( x=0; x<$max_x; x++ )); do
113 new_map["$x,$new_y"]=$((${map["$x,$y_1"]} | ${map["$x,$y_2"]}))
120 for k in "${!new_map[@]}"; do
121 map[$k]=${new_map[$k]}
127 for (( y=0; y<$max_y; y++ )); do
128 for (( x=0; x<$max_x; x++ )); do
129 ((count+=${map[$x,$y]}))
137 echo "Doing first fold"
139 echo "There are $(get_points_count) dots visible after first fold"
140 for (( f=1; f<${#folds[@]}; f++ )); do
141 echo "Doing fold $((f+1)) of ${#folds[@]}"
145 echo "After all folds:"