File tree Expand file tree Collapse file tree 1 file changed +85
-0
lines changed
Expand file tree Collapse file tree 1 file changed +85
-0
lines changed Original file line number Diff line number Diff line change 1+ #! /bin/bash
2+
3+ FILE=" $1 "
4+
5+ declare -A grid
6+
7+ y=0
8+ while read line; do
9+ x=0
10+ while read -n1 c; do
11+ [[ " $c " == " " ]] && continue
12+ grid[$x ,$y ]=$c
13+ (( x++ ))
14+ done <<< " $line"
15+ (( y++ ))
16+ done < " $FILE "
17+
18+ mx=$x
19+ my=$y
20+ function print_grid() {
21+ for (( y= 0 ;y< my;++ y)) ; do
22+ for (( x= 0 ;x< mx;++ x)) ; do
23+ echo -ne " ${grid[$x,$y]}${grid[$x,$y]} "
24+ done
25+ echo
26+ done
27+ }
28+
29+ print_grid 1>&2
30+
31+ function count_neighbors() {
32+ local x=$1
33+ local y=$2
34+ local count=0
35+ for (( a=- 1 ; a<= 1 ; a++ )) ; do
36+ for (( b=- 1 ; b<= 1 ; b++ )) ; do
37+ [[ a -eq b && a -eq 0 ]] && continue
38+ local c=" ${grid[$((x+a)),$((y+b))]} "
39+ [[ $c == " @" || $c == " x" ]] || continue
40+ (( count++ ))
41+ done
42+ done
43+ echo $count
44+ }
45+
46+ mark_for_removal () {
47+ marked=0
48+ for (( y= 0 ;y< my;++ y)) ; do
49+ for (( x= 0 ;x< mx;++ x)) ; do
50+ c=" ${grid[$x,$y]} "
51+ if [[ $c == " ." ]]; then
52+ continue
53+ fi
54+ count=${ count_neighbors $x $y ; }
55+ if [[ count -lt 4 ]]; then
56+ grid[$x ,$y ]=' \e[0;31mx\e[0;0m'
57+ (( marked++ ))
58+ fi
59+ done
60+ echo
61+ done
62+ echo $marked
63+ }
64+
65+ function remove_the_x() {
66+ for (( y= 0 ;y< my;++ y)) ; do
67+ for (( x= 0 ;x< mx;++ x)) ; do
68+ c=" ${grid[$x,$y]} "
69+ [[ $c != " \e[0;31mx\e[0;0m" ]] && continue
70+ grid[$x ,$y ]=' .'
71+ done
72+ done
73+ }
74+ m=100
75+ total=0
76+ while [[ m -gt 0 ]]; do
77+ m=${ mark_for_removal; }
78+ tput cup 0 0
79+ print_grid 1>&2
80+ remove_the_x
81+ tput cup 0 0
82+ print_grid 1>&2
83+ (( total+= m))
84+ done
85+ echo $total
You can’t perform that action at this time.
0 commit comments