2020 day 24 part 2 (bash)
This commit is contained in:
@@ -42,8 +42,6 @@ In the above example, 10 tiles are flipped once (to black), and 5 more are flipp
|
||||
Go through the renovation crew's list and determine which tiles they need to flip. After all of the instructions have been followed, how many tiles are left with the black side up?
|
||||
|
||||
Your puzzle answer was 450.
|
||||
|
||||
The first half of this puzzle is complete! It provides one gold star: *
|
||||
--- Part Two ---
|
||||
|
||||
The tile floor in the lobby is meant to be a living art exhibit. Every day, the tiles are all flipped according to the following rules:
|
||||
@@ -81,3 +79,7 @@ Day 100: 2208
|
||||
After executing this process a total of 100 times, there would be 2208 black tiles facing up.
|
||||
|
||||
How many tiles will be black after 100 days?
|
||||
|
||||
Your puzzle answer was 4059.
|
||||
|
||||
Both parts of this puzzle are complete! They provide two gold stars: **
|
||||
|
67
2020/day24/ex2.bash
Executable file
67
2020/day24/ex2.bash
Executable file
@@ -0,0 +1,67 @@
|
||||
#!/bin/bash
|
||||
#
|
||||
# ex1.bash: Advent2020 game, day 24/game 2.
|
||||
|
||||
CMD=${0##*/}
|
||||
shopt -s extglob
|
||||
set -o noglob
|
||||
|
||||
declare -A plan=() count=()
|
||||
declare -i x y loops=100
|
||||
while read -r line; do
|
||||
x=0
|
||||
y=0
|
||||
for ((i=0; i<${#line}; ++i)); do
|
||||
c=${line:i:1}
|
||||
case "$c" in
|
||||
e) ((++x))
|
||||
;;
|
||||
w) ((--x))
|
||||
;;
|
||||
s) ((--y, ++i))
|
||||
c=${line:i:1}
|
||||
;;
|
||||
n) ((++y, ++i))
|
||||
c=${line:i:1}
|
||||
;;
|
||||
esac
|
||||
if [[ "$c" = e ]]; then
|
||||
((++x))
|
||||
elif [[ "$c" = w ]]; then
|
||||
((--x))
|
||||
else
|
||||
printf "error c=%s\n" "$c"
|
||||
exit 1
|
||||
fi
|
||||
done
|
||||
[[ -v plan[$x,$y] ]] && unset "plan[$x,$y]" || plan[$x,$y]=1
|
||||
done
|
||||
|
||||
# adjacent cells (x1 y1 x2 y2 etc...)
|
||||
declare -a directions=(
|
||||
2 0 -2 0 # east and west
|
||||
1 -1 1 1 # SE and NE
|
||||
-1 -1 -1 1 # SW and NW
|
||||
)
|
||||
|
||||
for ((_c = 0; _c < loops; ++_c)) do
|
||||
count=()
|
||||
for cell in "${!plan[@]}"; do # count adjacent tiles
|
||||
x=${cell%,*}
|
||||
y=${cell#*,}
|
||||
for ((i = 0; i < ${#directions[@]}; i += 2)); do
|
||||
(( ++count[$((x + directions[$i])),$((y + directions[$((i+1))]))] ))
|
||||
done
|
||||
done
|
||||
for cell in "${!plan[@]}"; do # check black tiles
|
||||
(( count[$cell] == 0 || count[$cell] > 2)) && unset "plan[$cell]"
|
||||
unset "count[$cell]"
|
||||
done
|
||||
for cell in "${!count[@]}"; do # remaining ones are white
|
||||
((count[$cell] == 2)) && plan[$cell]=1
|
||||
done
|
||||
done
|
||||
|
||||
printf "%s: res=%d\n" "$CMD" "${#plan[@]}"
|
||||
|
||||
exit 0
|
Reference in New Issue
Block a user