count matches w/ string manipulation: ~75% gain, 85-90% gain from V1
This commit is contained in:
13
2020/day20/ex1.bash
Normal file → Executable file
13
2020/day20/ex1.bash
Normal file → Executable file
@@ -54,17 +54,20 @@ for key in "${!nums[@]}"; do
|
|||||||
|
|
||||||
done
|
done
|
||||||
|
|
||||||
ALL=("${T[@]}" "${R[@]}" "${B[@]}" "${L[@]}" "${RT[@]}" "${RR[@]}" "${RB[@]}" "${RL[@]}")
|
ALL="${T[@]} ${R[@]} ${B[@]} ${L[@]} ${RT[@]} ${RR[@]} ${RB[@]} ${RL[@]}"
|
||||||
|
ALLSIZE=${#ALL}
|
||||||
|
|
||||||
declare -i res=1 count
|
declare -i res=1 count
|
||||||
for ((i=0; i<${#nums[@]}; ++i)); do
|
for ((i=0; i<${#nums[@]}; ++i)); do
|
||||||
count=0
|
count=0
|
||||||
|
|
||||||
for t in ${T[$i]} ${R[$i]} ${B[$i]} ${L[$i]}; do
|
for t in ${T[$i]} ${R[$i]} ${B[$i]} ${L[$i]}; do
|
||||||
for s in "${ALL[@]}"; do
|
# https://stackoverflow.com/questions/26212889/bash-counting-substrings-in-a-string/50601141#50601141
|
||||||
[[ $t == "$s" ]] && ((count++))
|
S=${ALL//$t}
|
||||||
|
# 10 is line size
|
||||||
|
((count += (ALLSIZE-${#S})/10))
|
||||||
done
|
done
|
||||||
done
|
# 6 is 4 for itself, + 2 for other matching
|
||||||
|
|
||||||
((count == 6)) && ((res*=${nums[$i]}))
|
((count == 6)) && ((res*=${nums[$i]}))
|
||||||
done
|
done
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user