Compare commits
No commits in common. "9a0a6300399716b00052715ee4b1d04ef3119df0" and "67ae111303e1fa7be067ec699b1d9736150c608e" have entirely different histories.
9a0a630039
...
67ae111303
54
day11.sh
54
day11.sh
@ -1,54 +0,0 @@
|
|||||||
#!/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=()
|
|
||||||
|
|
||||||
for stone in "${@:2}"; do stones["$stone"]=1; done
|
|
||||||
|
|
||||||
for ((n = 0; n < blinks; n++)); do
|
|
||||||
local -A new_stones=()
|
|
||||||
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
|
|
||||||
done
|
|
||||||
|
|
||||||
local tmp
|
|
||||||
tmp="$(declare -p new_stones)"
|
|
||||||
eval "${tmp/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