2020 day 24 (bash) part 1.

This commit is contained in:
2022-10-09 14:28:58 +02:00
parent 4653101623
commit cced357154
2 changed files with 89 additions and 0 deletions

View File

@@ -40,3 +40,44 @@ wseweeenwnesenwwwswnew
In the above example, 10 tiles are flipped once (to black), and 5 more are flipped twice (to black, then back to white). After all of these instructions have been followed, a total of 10 tiles are black. In the above example, 10 tiles are flipped once (to black), and 5 more are flipped twice (to black, then back to white). After all of these instructions have been followed, a total of 10 tiles are black.
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? 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:
Any black tile with zero or more than 2 black tiles immediately adjacent to it is flipped to white.
Any white tile with exactly 2 black tiles immediately adjacent to it is flipped to black.
Here, tiles immediately adjacent means the six tiles directly touching the tile in question.
The rules are applied simultaneously to every tile; put another way, it is first determined which tiles need to be flipped, then they are all flipped at the same time.
In the above example, the number of black tiles that are facing up after the given number of days has passed is as follows:
Day 1: 15
Day 2: 12
Day 3: 25
Day 4: 14
Day 5: 23
Day 6: 28
Day 7: 41
Day 8: 37
Day 9: 49
Day 10: 37
Day 20: 132
Day 30: 259
Day 40: 406
Day 50: 566
Day 60: 788
Day 70: 1106
Day 80: 1373
Day 90: 1844
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?

48
2020/day24/ex1.bash Executable file
View File

@@ -0,0 +1,48 @@
#!/bin/bash
#
# ex1.bash: Advent2020 game, day 24/game 1.
CMD=${0##*/}
shopt -s extglob
set -o noglob
declare -A plan
declare -i x y
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"
fi
done
printf "new tile at (%d,%d): " "$x" "$y"
if [[ -v plan[$x,$y] ]]; then
printf "already set\n"
unset "plan[$x,$y]"
else
printf "new\n"
plan[$x,$y]=1
fi
done
res=${#plan[@]}
printf "%s: res=%s\n" "$CMD" "$res"
exit 0