2022 day 9: Bash part 2, same code for both part (slower #1)

This commit is contained in:
2022-12-13 18:38:24 +01:00
parent 13d183de79
commit 38ef781f0a
4 changed files with 51 additions and 34 deletions

View File

@@ -180,5 +180,10 @@ aoc-c: res=672280
+++++++++++++++++ part 1 +++++++++++++++++ part 1
aoc.bash: res=5619 aoc.bash: res=5619
time: 0:00.78 real, 0.77 user, 0.00 sys time: 0:01.24 real, 1.23 user, 0.01 sys
context-switch: 22+1, page-faults: 0+430 context-switch: 75+2, page-faults: 1+430
+++++++++++++++++ part 2
aoc.bash: res=2376
time: 0:05.61 real, 5.59 user, 0.01 sys
context-switch: 329+1, page-faults: 0+345

View File

@@ -286,8 +286,6 @@ positions does the tail of the rope visit at least once?/
Your puzzle answer was =5619=. Your puzzle answer was =5619=.
The first half of this puzzle is complete! It provides one gold star: *
** --- Part Two --- ** --- Part Two ---
A rope snaps! Suddenly, the river is getting a lot closer than you A rope snaps! Suddenly, the river is getting a lot closer than you
remember. The bridge is still there, but some of the ropes that broke remember. The bridge is still there, but some of the ropes that broke
@@ -743,3 +741,7 @@ Now, the tail (=9=) visits =36= positions (including =s=) at least once:
Simulate your complete series of motions on a larger rope with ten Simulate your complete series of motions on a larger rope with ten
knots. /How many positions does the tail of the rope visit at least knots. /How many positions does the tail of the rope visit at least
once?/ once?/
Your puzzle answer was =2376=.
Both parts of this puzzle are complete! They provide two gold stars: **

View File

@@ -13,11 +13,13 @@
. common.bash . common.bash
declare -i xh=0 yh=0 xt=0 yt=0 declare -a h{0..10} # h0 is head
declare -A visited=() declare -A visited=() # keep count of visited
declare -i last=1 # tail (last knot)
add_pos() { add_pos() {
(( visited["$1-$2"]++ )) local -n queue="$1"
visited["${queue[0]}"/"${queue[1]}"]=1
} }
direction() { direction() {
@@ -32,62 +34,62 @@ direction() {
esac esac
} }
move_t() { do_tail() {
local -n _h="$1" _t="$2"
local -i dx dy sx sy local -i dx dy sx sy
(( dx = xh - xt, dy = yh - yt )) (( dx = _h[0] - _t[0], dy = _h[1] - _t[1] ))
(( sx = dx > 0? 1: -1 )) (( sx = dx > 0? 1: -1 ))
(( sy = dy > 0? 1: -1 )) (( sy = dy > 0? 1: -1 ))
#printf " [move_t: dx=%d dy=%d " "$dx" "$dy"
if (( sx * dx > 1 || sy * dy > 1)); then if (( sx * dx > 1 || sy * dy > 1)); then
(( dx > 0 )) && (( xt++ )) (( dx > 0 )) && (( _t[0]++ ))
(( dx < 0 )) && (( xt-- )) (( dx < 0 )) && (( _t[0]-- ))
(( dy > 0 )) && (( yt++ )) (( dy > 0 )) && (( _t[1]++ ))
(( dy < 0 )) && (( yt-- )) (( dy < 0 )) && (( _t[1]-- ))
fi fi
#printf "xt=%d yt=%d] " "$xt" "$yt" }
add_pos "$xt" "$yt"
move_t() {
local -i _n
for (( _n = 0; _n < last; ++_n )); do
do_tail "h$_n" "h$((_n + 1))"
done
add_pos "h$last"
return
} }
move_h() { move_h() {
local -i dx="$1" dy="$2" n="$3" i local -i _dx="$1" _dy="$2" _m="$3" _i _n
#printf "\tmove_h dx=%d dy=%d n=%d\n" "$dx" "$dy" "$n" for ((_i = 0; _i < _m; ++_i)); do
for ((i = 0; i < n; ++i)); do (( h0[0] += _dx, h0[1] += _dy ))
(( xh += dx, yh += dy ))
#printf -- "(%d,%d)" "$xh" "$yh"
move_t move_t
#printf -- "/(%d,%d)\n" "$xt" "$yt"
done done
} }
parse() { parse() {
local dir moves dx dy local dir moves dx dy
(( last = $1 == 1? 1: 9 ))
while read -r dir moves; do while read -r dir moves; do
#printf "(%d,%d)/(%d,%d) %s/%d\n" "$xh" "$yh" "$xt" "$yt" "$dir" "$moves"
direction dx dy "$dir" direction dx dy "$dir"
#printf "\tafter direction dx=%d dy=%d\n" "$dx" "$dy"
move_h "$dx" "$dy" "$moves" move_h "$dx" "$dy" "$moves"
#printf "\th: (%d,%d)/(%d,%d)\n" "$xh" "$yh" "$xt" "$yt"
add_pos "$xt" "$yt"
done done
} }
part1() { part1() {
declare -ig res local -n _t="h$last"
parse
#echo FINAL
#echo "${!visited[@]}"
res=${#visited[@]} res=${#visited[@]}
} }
part2() { part2() {
: local -n _t="h$last"
res=${#visited[@]}
} }
solve() { solve() {
add_pos 0 0 local part="$1"
add_pos "h$last"
if ((part == 1)); then if ((part == 1)); then
part1 part1
else else

View File

@@ -0,0 +1,8 @@
R 5
U 8
L 8
D 3
R 17
D 10
L 25
U 20