2022 day 6: Bash part 2 (lucky me: part 1 code worked) + optimizations

This commit is contained in:
2022-12-09 14:38:21 +01:00
parent 6a5a0da435
commit 05643127c2
3 changed files with 36 additions and 55 deletions

View File

@@ -107,3 +107,17 @@ aoc.bash: res=NLCDCLVMQ
aoc-c: res=NLCDCLVMQ aoc-c: res=NLCDCLVMQ
time: 0:00.00 real, 0.00 user, 0.00 sys time: 0:00.00 real, 0.00 user, 0.00 sys
context-switch: 0+1, page-faults: 0+86 context-switch: 0+1, page-faults: 0+86
=========================================
================= day06 =================
=========================================
+++++++++++++++++ part 1
aoc.bash: res=1658
time: 0:00.06 real, 0.06 user, 0.00 sys
context-switch: 1+1, page-faults: 0+266
+++++++++++++++++ part 2
aoc.bash: res=2260
time: 0:00.09 real, 0.09 user, 0.00 sys
context-switch: 1+1, page-faults: 0+265

View File

@@ -61,8 +61,6 @@ start-of-packet marker is detected?/
Your puzzle answer was =1658=. Your puzzle answer was =1658=.
The first half of this puzzle is complete! It provides one gold star: *
** --- Part Two --- ** --- Part Two ---
Your device's communication system is correctly detecting packets, but Your device's communication system is correctly detecting packets, but
still isn't working. It looks like it also needs to look for /messages/. still isn't working. It looks like it also needs to look for /messages/.
@@ -82,11 +80,6 @@ above examples:
/How many characters need to be processed before the first /How many characters need to be processed before the first
start-of-message marker is detected?/ start-of-message marker is detected?/
Answer: Your puzzle answer was =2260=.
Although it hasn't changed, you can still [[file:6/input][get your Both parts of this puzzle are complete! They provide two gold stars: **
puzzle input]].
You can also [Shareon
[[https://twitter.com/intent/tweet?text=I%27ve+completed+Part+One+of+%22Tuning+Trouble%22+%2D+Day+6+%2D+Advent+of+Code+2022&url=https%3A%2F%2Fadventofcode%2Ecom%2F2022%2Fday%2F6&related=ericwastl&hashtags=AdventOfCode][Twitter]]
[[javascript:void(0);][Mastodon]]] this puzzle.

View File

@@ -13,59 +13,33 @@
. common.bash . common.bash
declare -a stacks=() rules=() declare input # datastream
declare -i len # len of datastream
parse() { parse() {
local -i _queue _state=1 read -r input
local _input len=${#input}
local -a _parse
local cur=""
local -i lcur=0 i
read -r _input
len=${#_input}
lcur=0
for (( i = 0; i < len - 4; ++i )); do
echo
nextc=${_input:i:1} # get next char
printf "next=%s cur=%s lcur=%d\n" "$nextc" "$cur" "$lcur"
for ((j = 0; j < lcur; ++j)); do # compare with previous ones
printf "compare %s with %d:%s\n" "$nextc" "$j" "${cur:j:1}"
if [[ $nextc == "${cur:j:1}" ]]; then
cur=${cur:j+1}$nextc
(( lcur = lcur - (j + 1) + 1))
printf "\t-> equal cur=%d:%s" "$lcur" "$cur"
continue 2
fi
done
cur+=$nextc
((lcur++))
((lcur == 4)) && break
done
echo "$cur" $((i + 1))
} }
solve() { solve() {
local -i _part="$1" _i=0 _j=0 _nb _from _to declare -ig res
local -a _rule local -i mark=4 lcur=0 part="$1"
local cur next
for ((; _i < ${#rules[@]}; ++_i )); do ((part == 2)) && mark=14
read -ra _rule <<< "${rules[$_i]}" for (( i = 0; i < len - mark; ++i )); do # loop on all chars
(( _nb = _rule[0], _from = _rule[1] - 1, _to = _rule[2] - 1 )) next=${input:i:1} # next char
if ((_part == 1)); then for ((j = 0; j < lcur; ++j)); do # compare with previous ones
# move elements one by one if [[ $next == "${cur:j:1}" ]]; then # duplicate
for ((_j = 0; _j < _nb; ++_j)); do cur="${cur:j+1}$next" # keep str after dup + new char
stacks[$_to]="${stacks[$_from]:0:1}${stacks[$_to]}" (( lcur = lcur - j ))
stacks[$_from]=${stacks[$_from]:1} continue 2
done fi
else done
# move elements by block cur+="$next" # add new char
stacks[$_to]="${stacks[$_from]:0:_nb}${stacks[$_to]}" ((++lcur == mark)) && break # mark len found
stacks[$_from]=${stacks[$_from]:_nb}
fi
done done
printf -v res "%c" "${stacks[@]}" ((res = i + 1))
} }
main "$@" main "$@"