Bash: 2022 day 5 (both parts, brute code)
This commit is contained in:
@@ -88,4 +88,82 @@ Elves the message =CMZ=.
|
||||
/After the rearrangement procedure completes, what crate ends up on top
|
||||
of each stack?/
|
||||
|
||||
To begin, [[file:5/input][get your puzzle input]].
|
||||
Your puzzle answer was =VQZNJMWTR=.
|
||||
|
||||
** --- Part Two ---
|
||||
As you watch the crane operator expertly rearrange the crates, you
|
||||
notice the process isn't following your prediction.
|
||||
|
||||
Some mud was covering the writing on the side of the crane, and you
|
||||
quickly wipe it away. The crane isn't a CrateMover 9000 - it's a
|
||||
/CrateMover 9001/.
|
||||
|
||||
The CrateMover 9001 is notable for many new and exciting features: air
|
||||
conditioning, leather seats, an extra cup holder, and /the ability to
|
||||
pick up and move multiple crates at once/.
|
||||
|
||||
Again considering the example above, the crates begin in the same
|
||||
configuration:
|
||||
|
||||
#+begin_example
|
||||
[D]
|
||||
[N] [C]
|
||||
[Z] [M] [P]
|
||||
1 2 3
|
||||
#+end_example
|
||||
|
||||
Moving a single crate from stack 2 to stack 1 behaves the same as
|
||||
before:
|
||||
|
||||
#+begin_example
|
||||
[D]
|
||||
[N] [C]
|
||||
[Z] [M] [P]
|
||||
1 2 3
|
||||
#+end_example
|
||||
|
||||
However, the action of moving three crates from stack 1 to stack 3 means
|
||||
that those three moved crates /stay in the same order/, resulting in
|
||||
this new configuration:
|
||||
|
||||
#+begin_example
|
||||
[D]
|
||||
[N]
|
||||
[C] [Z]
|
||||
[M] [P]
|
||||
1 2 3
|
||||
#+end_example
|
||||
|
||||
Next, as both crates are moved from stack 2 to stack 1, they /retain
|
||||
their order/ as well:
|
||||
|
||||
#+begin_example
|
||||
[D]
|
||||
[N]
|
||||
[C] [Z]
|
||||
[M] [P]
|
||||
1 2 3
|
||||
#+end_example
|
||||
|
||||
Finally, a single crate is still moved from stack 1 to stack 2, but now
|
||||
it's crate =C= that gets moved:
|
||||
|
||||
#+begin_example
|
||||
[D]
|
||||
[N]
|
||||
[Z]
|
||||
[M] [C] [P]
|
||||
1 2 3
|
||||
#+end_example
|
||||
|
||||
In this example, the CrateMover 9001 has put the crates in a totally
|
||||
different order: =MCD=.
|
||||
|
||||
Before the rearrangement process finishes, update your simulation so
|
||||
that the Elves know where they should stand to be ready to unload the
|
||||
final supplies. /After the rearrangement procedure completes, what crate
|
||||
ends up on top of each stack?/
|
||||
|
||||
Your puzzle answer was =NLCDCLVMQ=.
|
||||
|
||||
Both parts of this puzzle are complete! They provide two gold stars: **
|
||||
|
131
2022/day05/aoc.bash
Executable file
131
2022/day05/aoc.bash
Executable file
@@ -0,0 +1,131 @@
|
||||
#!/usr/bin/env bash
|
||||
#
|
||||
# aoc.bash: Advent of Code 2022, day 3
|
||||
#
|
||||
# Copyright (C) 2022 Bruno Raoult ("br")
|
||||
# Licensed under the GNU General Public License v3.0 or later.
|
||||
# Some rights reserved. See COPYING.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License along with this
|
||||
# program. If not, see <https://www.gnu.org/licenses/gpl-3.0-standalone.html>.
|
||||
#
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
|
||||
|
||||
. common.bash
|
||||
|
||||
declare -a stacks=() rules=()
|
||||
|
||||
printall() {
|
||||
local i
|
||||
for (( i = 0; i < ${#stacks[@]}; ++i )); do
|
||||
printf "stack %d: %s\n" "$i" "${stacks[i]}"
|
||||
done
|
||||
#for (( i = 0; i < ${#rules[@]}; ++i )); do
|
||||
# printf "rule %d: %s\n" "$i" "${rules[i]}"
|
||||
#done
|
||||
}
|
||||
|
||||
parse() {
|
||||
local -i _part="$1" _queue li _state=1
|
||||
local _input
|
||||
local -a _parse
|
||||
declare -ig _res=0
|
||||
|
||||
li=1
|
||||
while IFS= read -r _input; do
|
||||
if [[ -z ${_input} || ${_input:1:1} == "1" ]]; then
|
||||
printf "\tzobi\n"
|
||||
_state=2
|
||||
li=0
|
||||
continue
|
||||
fi
|
||||
case $_state in
|
||||
1) # stacks description
|
||||
# get blocks of 4 characters
|
||||
#echo GGG
|
||||
for ((i=0, _queue=0; i<${#_input}; i+=4, _queue++)); do
|
||||
#printf "line=%d queue=%d\n" "$li" "$_queue"
|
||||
sub=${_input:i:4}
|
||||
if [[ $sub == " " ]]; then
|
||||
:
|
||||
#printf "\t SKIP line %d queue\n" "$li" "$_queue"
|
||||
else
|
||||
# printf "\tline %d queue %d CHAR %s\n" "$li" "$_queue" "${sub:1:1}"
|
||||
stacks[$_queue]+="${sub:1:1}"
|
||||
fi
|
||||
done
|
||||
;;
|
||||
2) # moves description
|
||||
#echo HHH
|
||||
read -ra _parse <<<"$_input"
|
||||
#printf "RULE: %d\n" ${#_parse[@]}
|
||||
#printf "rule %d: %d %d %d\n" "$li" "${_parse[1]}" "${_parse[3]}" "${_parse[5]}"
|
||||
rules[$li]="${_parse[1]} ${_parse[3]} ${_parse[5]}"
|
||||
esac
|
||||
|
||||
((li++))
|
||||
done
|
||||
#printall
|
||||
}
|
||||
|
||||
part1() {
|
||||
local -i _i _j _nb _from
|
||||
local -a _rule
|
||||
local _tmp
|
||||
printall
|
||||
for (( _i=0; _i<${#rules[@]}; ++_i )); do
|
||||
# shellcheck disable=2086
|
||||
read -ra _rule <<< ${rules[$_i]}
|
||||
(( _nb = _rule[0] ))
|
||||
((_from = _rule[1] - 1 ))
|
||||
((_to = _rule[2] - 1 ))
|
||||
#printf "rule=%d nb=%d from=%d=%s to=%d=%s\n" "$_i" "$_nb" "$_from" "${stacks[$_from]}" "$_to" "${stacks[$_to]}"
|
||||
#printf "_rule=%d nb=%s from=%s/%s to=%s/%s\n" ${#_rule[@]} "${_rule[0]}" "${_rule[1]}" "${stacks[${_rule[1]}]}" "${_rule[2]}" "${stacks[${_rule[2]}]}"
|
||||
for ((_j = 0; _j < _nb; ++_j)); do
|
||||
#printf "moving char %d f=%s t=%s\n" "$_i" "${stacks[$_from]}" "${stacks[$_to]}"
|
||||
stacks[$_to]="${stacks[$_from]:0:1}${stacks[$_to]}"
|
||||
stacks[$_from]=${stacks[$_from]:1}
|
||||
#printf " --> f=%s t=%s\n" "${stacks[$_from]}" "${stacks[$_to]}"
|
||||
done
|
||||
|
||||
printall
|
||||
done
|
||||
}
|
||||
|
||||
part2() {
|
||||
local -i _i _nb _from
|
||||
local -a _rule
|
||||
local _tmp
|
||||
#printall
|
||||
for (( _i=0; _i<${#rules[@]}; ++_i )); do
|
||||
# shellcheck disable=2086
|
||||
read -ra _rule <<< ${rules[$_i]}
|
||||
(( _nb = _rule[0] ))
|
||||
(( _from = _rule[1] - 1 ))
|
||||
(( _to = _rule[2] - 1 ))
|
||||
stacks[$_to]="${stacks[$_from]:0:_nb}${stacks[$_to]}"
|
||||
stacks[$_from]=${stacks[$_from]:_nb}
|
||||
|
||||
#printf "rule=%d nb=%d from=%d=%s to=%d=%s\n" "$_i" "$_nb" "$_from" "${stacks[$_from]}" "$_to" "${stacks[$_to]}"
|
||||
#printf "_rule=%d nb=%s from=%s/%s to=%s/%s\n" ${#_rule[@]} "${_rule[0]}" "${_rule[1]}" "${stacks[${_rule[1]}]}" "${_rule[2]}" "${stacks[${_rule[2]}]}"
|
||||
|
||||
#printall
|
||||
done
|
||||
}
|
||||
|
||||
solve() {
|
||||
local -i _i
|
||||
if (($1 == 1)); then
|
||||
part1
|
||||
else
|
||||
part2
|
||||
fi
|
||||
res=""
|
||||
for ((_i = 0; _i < ${#stacks[@]}; ++_i)); do
|
||||
res+="${stacks[_i]:0:1}"
|
||||
done
|
||||
#echo "res=$res"
|
||||
}
|
||||
|
||||
main "$@"
|
||||
exit 0
|
Reference in New Issue
Block a user