day11
This commit is contained in:
parent
67ae111303
commit
6fb5dfc060
56
day11.sh
Normal file
56
day11.sh
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
unset PATH
|
||||||
|
readonly PATH
|
||||||
|
source ./utils/main.sh
|
||||||
|
source ./utils/array.sh
|
||||||
|
|
||||||
|
count_after_blinks() {
|
||||||
|
local blinks="$1"
|
||||||
|
local -A stones=() new_stones=()
|
||||||
|
|
||||||
|
for stone in "${@:2}"; do stones["$stone"]=1; done
|
||||||
|
|
||||||
|
for ((n = 0; n < blinks; n++)); do
|
||||||
|
for stone in "${!stones[@]}"; do
|
||||||
|
if ((stone == 0)); then
|
||||||
|
new_stones[1]="$((${new_stones[1]:-0} + stones["$stone"]))"
|
||||||
|
elif ((${#stone} % 2 == 0)); then
|
||||||
|
left="${stone::"$((${#stone} / 2))"}"
|
||||||
|
right="${stone:"$((${#stone} / 2))"}"
|
||||||
|
right="$((10#"$right"))"
|
||||||
|
|
||||||
|
new_stones["$left"]="$((${new_stones["$left"]:-0} + stones["$stone"]))"
|
||||||
|
new_stones["$right"]="$((${new_stones["$right"]:-0} + stones["$stone"]))"
|
||||||
|
else
|
||||||
|
new_stones["$((stone * 2024))"]="$((${new_stones["$((stone * 2024))"]:-0} + stones["$stone"]))"
|
||||||
|
fi
|
||||||
|
stones["$stone"]=0
|
||||||
|
done
|
||||||
|
|
||||||
|
local old new
|
||||||
|
old="$(declare -p stones)"
|
||||||
|
new="$(declare -p new_stones)"
|
||||||
|
eval "${old/stones/new_stones}"
|
||||||
|
eval "${new/new_stones/stones}"
|
||||||
|
done
|
||||||
|
|
||||||
|
array.sum stones
|
||||||
|
}
|
||||||
|
|
||||||
|
p1() {
|
||||||
|
local input="$1"
|
||||||
|
local -a stones
|
||||||
|
|
||||||
|
read -ra stones <<<"$input"
|
||||||
|
count_after_blinks 25 "${stones[@]}"
|
||||||
|
}
|
||||||
|
|
||||||
|
p2() {
|
||||||
|
local input="$1"
|
||||||
|
local -a stones
|
||||||
|
|
||||||
|
read -ra stones <<<"$input"
|
||||||
|
count_after_blinks 75 "${stones[@]}"
|
||||||
|
}
|
||||||
|
|
||||||
|
main "$@"
|
Loading…
x
Reference in New Issue
Block a user