Day 25
This commit is contained in:
51
25/01.sh
Executable file
51
25/01.sh
Executable file
@@ -0,0 +1,51 @@
|
||||
#!/bin/bash
|
||||
|
||||
max_i=0
|
||||
max_j=0
|
||||
declare -A grid
|
||||
while read line; do
|
||||
max_j=${#line}
|
||||
for ((j=0;j<max_j;j++)); do
|
||||
if [[ ${line:$j:1} == '.' ]]; then
|
||||
grid[$max_i,$j]=0
|
||||
elif [[ ${line:$j:1} == '>' ]]; then
|
||||
grid[$max_i,$j]=1
|
||||
elif [[ ${line:$j:1} == 'v' ]]; then
|
||||
grid[$max_i,$j]=2
|
||||
fi
|
||||
done
|
||||
max_i=$((max_i+1))
|
||||
done
|
||||
|
||||
changes=1
|
||||
south=0
|
||||
steps=0
|
||||
while [ $changes -eq 1 ]; do
|
||||
changes=$south
|
||||
declare -A new_grid
|
||||
for ((i=0;i<max_i;i++)); do
|
||||
for ((j=0;j<max_j;j++)); do
|
||||
if [ $south -eq 0 ] && [ ${grid[$i,$j]} -eq 1 ] && [ ${grid[$i,$(((j+1)%max_j))]} -eq 0 ]; then
|
||||
new_grid[$i,$j]=0
|
||||
new_grid[$i,$(((j+1)%max_j))]=1
|
||||
changes=1
|
||||
elif [ $south -eq 1 ] && [ ${grid[$i,$j]} -eq 2 ] && [ ${grid[$(((i+1)%max_i)),$j]} -eq 0 ]; then
|
||||
new_grid[$i,$j]=0
|
||||
new_grid[$(((i+1)%max_i)),$j]=2
|
||||
changes=1
|
||||
elif [ -z ${new_grid[$i,$j]} ]; then
|
||||
new_grid[$i,$j]=${grid[$i,$j]}
|
||||
fi
|
||||
done
|
||||
done
|
||||
south=$((1-south))
|
||||
for ((i=0;i<max_i;i++)); do
|
||||
for ((j=0;j<max_j;j++)); do
|
||||
grid[$i,$j]=${new_grid[$i,$j]}
|
||||
done
|
||||
done
|
||||
unset new_grid
|
||||
steps=$((steps+south))
|
||||
done
|
||||
|
||||
echo $steps
|
||||
Reference in New Issue
Block a user