2020 day 24 part 2 (bash)

This commit is contained in:
2022-10-10 16:09:24 +02:00
parent 563798871a
commit d2b5a9dc34
4 changed files with 84 additions and 3 deletions

View File

@@ -497,3 +497,15 @@ ex1.bash: res=75893264
ex2.bash: res=38162588308
time: 6:52.50 real, 412.30 user, 0.14 sys
context-switch: 2219+1, page-faults: 0+30233
=========================================
================= day24 =================
=========================================
ex1.bash: res=450
time: 0:00.17 real, 0.17 user, 0.00 sys
context-switch: 5+1, page-faults: 0+177
ex2.bash: res=4059
time: 0:22.35 real, 22.22 user, 0.07 sys
context-switch: 1471+1, page-faults: 0+858

View File

@@ -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
View 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

View File

@@ -9,7 +9,7 @@
#### Advent of Code 2020
- `C`: Days 1-18
- `Bash`: Days 1-23
- `Bash`: Days 1-24
- `Cobol`: Day 1 (!!)
#### Advent of Code 2021