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
time: 0:00.00 real, 0.00 user, 0.00 sys
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=.
The first half of this puzzle is complete! It provides one gold star: *
** --- Part Two ---
Your device's communication system is correctly detecting packets, but
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
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
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.
Both parts of this puzzle are complete! They provide two gold stars: **

View File

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