2022 day 9, Bash part 1
This commit is contained in:
@@ -173,3 +173,12 @@ aoc.bash: res=672280
|
|||||||
aoc-c: res=672280
|
aoc-c: res=672280
|
||||||
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+91
|
context-switch: 0+1, page-faults: 0+91
|
||||||
|
|
||||||
|
=========================================
|
||||||
|
================= day09 =================
|
||||||
|
=========================================
|
||||||
|
|
||||||
|
+++++++++++++++++ part 1
|
||||||
|
aoc.bash: res=5619
|
||||||
|
time: 0:00.78 real, 0.77 user, 0.00 sys
|
||||||
|
context-switch: 22+1, page-faults: 0+430
|
||||||
|
@@ -283,3 +283,463 @@ So, there are =13= positions the tail visited at least once.
|
|||||||
|
|
||||||
Simulate your complete hypothetical series of motions. /How many
|
Simulate your complete hypothetical series of motions. /How many
|
||||||
positions does the tail of the rope visit at least once?/
|
positions does the tail of the rope visit at least once?/
|
||||||
|
|
||||||
|
Your puzzle answer was =5619=.
|
||||||
|
|
||||||
|
The first half of this puzzle is complete! It provides one gold star: *
|
||||||
|
|
||||||
|
** --- Part Two ---
|
||||||
|
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
|
||||||
|
are now whipping toward you as you fall through the air!
|
||||||
|
|
||||||
|
The ropes are moving too quickly to grab; you only have a few seconds to
|
||||||
|
choose how to arch your body to avoid being hit. Fortunately, your
|
||||||
|
simulation can be extended to support longer ropes.
|
||||||
|
|
||||||
|
Rather than two knots, you now must simulate a rope consisting of /ten/
|
||||||
|
knots. One knot is still the head of the rope and moves according to the
|
||||||
|
series of motions. Each knot further down the rope follows the knot in
|
||||||
|
front of it using the same rules as before.
|
||||||
|
|
||||||
|
Using the same series of motions as the above example, but with the
|
||||||
|
knots marked =H=, =1=, =2=, ..., =9=, the motions now occur as follows:
|
||||||
|
|
||||||
|
#+begin_example
|
||||||
|
== Initial State ==
|
||||||
|
|
||||||
|
......
|
||||||
|
......
|
||||||
|
......
|
||||||
|
......
|
||||||
|
H..... (H covers 1, 2, 3, 4, 5, 6, 7, 8, 9, s)
|
||||||
|
|
||||||
|
== R 4 ==
|
||||||
|
|
||||||
|
......
|
||||||
|
......
|
||||||
|
......
|
||||||
|
......
|
||||||
|
1H.... (1 covers 2, 3, 4, 5, 6, 7, 8, 9, s)
|
||||||
|
|
||||||
|
......
|
||||||
|
......
|
||||||
|
......
|
||||||
|
......
|
||||||
|
21H... (2 covers 3, 4, 5, 6, 7, 8, 9, s)
|
||||||
|
|
||||||
|
......
|
||||||
|
......
|
||||||
|
......
|
||||||
|
......
|
||||||
|
321H.. (3 covers 4, 5, 6, 7, 8, 9, s)
|
||||||
|
|
||||||
|
......
|
||||||
|
......
|
||||||
|
......
|
||||||
|
......
|
||||||
|
4321H. (4 covers 5, 6, 7, 8, 9, s)
|
||||||
|
|
||||||
|
== U 4 ==
|
||||||
|
|
||||||
|
......
|
||||||
|
......
|
||||||
|
......
|
||||||
|
....H.
|
||||||
|
4321.. (4 covers 5, 6, 7, 8, 9, s)
|
||||||
|
|
||||||
|
......
|
||||||
|
......
|
||||||
|
....H.
|
||||||
|
.4321.
|
||||||
|
5..... (5 covers 6, 7, 8, 9, s)
|
||||||
|
|
||||||
|
......
|
||||||
|
....H.
|
||||||
|
....1.
|
||||||
|
.432..
|
||||||
|
5..... (5 covers 6, 7, 8, 9, s)
|
||||||
|
|
||||||
|
....H.
|
||||||
|
....1.
|
||||||
|
..432.
|
||||||
|
.5....
|
||||||
|
6..... (6 covers 7, 8, 9, s)
|
||||||
|
|
||||||
|
== L 3 ==
|
||||||
|
|
||||||
|
...H..
|
||||||
|
....1.
|
||||||
|
..432.
|
||||||
|
.5....
|
||||||
|
6..... (6 covers 7, 8, 9, s)
|
||||||
|
|
||||||
|
..H1..
|
||||||
|
...2..
|
||||||
|
..43..
|
||||||
|
.5....
|
||||||
|
6..... (6 covers 7, 8, 9, s)
|
||||||
|
|
||||||
|
.H1...
|
||||||
|
...2..
|
||||||
|
..43..
|
||||||
|
.5....
|
||||||
|
6..... (6 covers 7, 8, 9, s)
|
||||||
|
|
||||||
|
== D 1 ==
|
||||||
|
|
||||||
|
..1...
|
||||||
|
.H.2..
|
||||||
|
..43..
|
||||||
|
.5....
|
||||||
|
6..... (6 covers 7, 8, 9, s)
|
||||||
|
|
||||||
|
== R 4 ==
|
||||||
|
|
||||||
|
..1...
|
||||||
|
..H2..
|
||||||
|
..43..
|
||||||
|
.5....
|
||||||
|
6..... (6 covers 7, 8, 9, s)
|
||||||
|
|
||||||
|
..1...
|
||||||
|
...H.. (H covers 2)
|
||||||
|
..43..
|
||||||
|
.5....
|
||||||
|
6..... (6 covers 7, 8, 9, s)
|
||||||
|
|
||||||
|
......
|
||||||
|
...1H. (1 covers 2)
|
||||||
|
..43..
|
||||||
|
.5....
|
||||||
|
6..... (6 covers 7, 8, 9, s)
|
||||||
|
|
||||||
|
......
|
||||||
|
...21H
|
||||||
|
..43..
|
||||||
|
.5....
|
||||||
|
6..... (6 covers 7, 8, 9, s)
|
||||||
|
|
||||||
|
== D 1 ==
|
||||||
|
|
||||||
|
......
|
||||||
|
...21.
|
||||||
|
..43.H
|
||||||
|
.5....
|
||||||
|
6..... (6 covers 7, 8, 9, s)
|
||||||
|
|
||||||
|
== L 5 ==
|
||||||
|
|
||||||
|
......
|
||||||
|
...21.
|
||||||
|
..43H.
|
||||||
|
.5....
|
||||||
|
6..... (6 covers 7, 8, 9, s)
|
||||||
|
|
||||||
|
......
|
||||||
|
...21.
|
||||||
|
..4H.. (H covers 3)
|
||||||
|
.5....
|
||||||
|
6..... (6 covers 7, 8, 9, s)
|
||||||
|
|
||||||
|
......
|
||||||
|
...2..
|
||||||
|
..H1.. (H covers 4; 1 covers 3)
|
||||||
|
.5....
|
||||||
|
6..... (6 covers 7, 8, 9, s)
|
||||||
|
|
||||||
|
......
|
||||||
|
...2..
|
||||||
|
.H13.. (1 covers 4)
|
||||||
|
.5....
|
||||||
|
6..... (6 covers 7, 8, 9, s)
|
||||||
|
|
||||||
|
......
|
||||||
|
......
|
||||||
|
H123.. (2 covers 4)
|
||||||
|
.5....
|
||||||
|
6..... (6 covers 7, 8, 9, s)
|
||||||
|
|
||||||
|
== R 2 ==
|
||||||
|
|
||||||
|
......
|
||||||
|
......
|
||||||
|
.H23.. (H covers 1; 2 covers 4)
|
||||||
|
.5....
|
||||||
|
6..... (6 covers 7, 8, 9, s)
|
||||||
|
|
||||||
|
......
|
||||||
|
......
|
||||||
|
.1H3.. (H covers 2, 4)
|
||||||
|
.5....
|
||||||
|
6..... (6 covers 7, 8, 9, s)
|
||||||
|
#+end_example
|
||||||
|
|
||||||
|
Now, you need to keep track of the positions the new tail, =9=, visits.
|
||||||
|
In this example, the tail never moves, and so it only visits =1=
|
||||||
|
position. However, /be careful/: more types of motion are possible than
|
||||||
|
before, so you might want to visually compare your simulated rope to the
|
||||||
|
one above.
|
||||||
|
|
||||||
|
Here's a larger example:
|
||||||
|
|
||||||
|
#+begin_example
|
||||||
|
R 5
|
||||||
|
U 8
|
||||||
|
L 8
|
||||||
|
D 3
|
||||||
|
R 17
|
||||||
|
D 10
|
||||||
|
L 25
|
||||||
|
U 20
|
||||||
|
#+end_example
|
||||||
|
|
||||||
|
These motions occur as follows (individual steps are not shown):
|
||||||
|
|
||||||
|
#+begin_example
|
||||||
|
== Initial State ==
|
||||||
|
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
...........H.............. (H covers 1, 2, 3, 4, 5, 6, 7, 8, 9, s)
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
|
||||||
|
== R 5 ==
|
||||||
|
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
...........54321H......... (5 covers 6, 7, 8, 9, s)
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
|
||||||
|
== U 8 ==
|
||||||
|
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
................H.........
|
||||||
|
................1.........
|
||||||
|
................2.........
|
||||||
|
................3.........
|
||||||
|
...............54.........
|
||||||
|
..............6...........
|
||||||
|
.............7............
|
||||||
|
............8.............
|
||||||
|
...........9.............. (9 covers s)
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
|
||||||
|
== L 8 ==
|
||||||
|
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
........H1234.............
|
||||||
|
............5.............
|
||||||
|
............6.............
|
||||||
|
............7.............
|
||||||
|
............8.............
|
||||||
|
............9.............
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
...........s..............
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
|
||||||
|
== D 3 ==
|
||||||
|
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
.........2345.............
|
||||||
|
........1...6.............
|
||||||
|
........H...7.............
|
||||||
|
............8.............
|
||||||
|
............9.............
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
...........s..............
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
|
||||||
|
== R 17 ==
|
||||||
|
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
................987654321H
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
...........s..............
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
|
||||||
|
== D 10 ==
|
||||||
|
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
...........s.........98765
|
||||||
|
.........................4
|
||||||
|
.........................3
|
||||||
|
.........................2
|
||||||
|
.........................1
|
||||||
|
.........................H
|
||||||
|
|
||||||
|
== L 25 ==
|
||||||
|
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
...........s..............
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
H123456789................
|
||||||
|
|
||||||
|
== U 20 ==
|
||||||
|
|
||||||
|
H.........................
|
||||||
|
1.........................
|
||||||
|
2.........................
|
||||||
|
3.........................
|
||||||
|
4.........................
|
||||||
|
5.........................
|
||||||
|
6.........................
|
||||||
|
7.........................
|
||||||
|
8.........................
|
||||||
|
9.........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
...........s..............
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
#+end_example
|
||||||
|
|
||||||
|
Now, the tail (=9=) visits =36= positions (including =s=) at least once:
|
||||||
|
|
||||||
|
#+begin_example
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
..........................
|
||||||
|
#.........................
|
||||||
|
#.............###.........
|
||||||
|
#............#...#........
|
||||||
|
.#..........#.....#.......
|
||||||
|
..#..........#.....#......
|
||||||
|
...#........#.......#.....
|
||||||
|
....#......s.........#....
|
||||||
|
.....#..............#.....
|
||||||
|
......#............#......
|
||||||
|
.......#..........#.......
|
||||||
|
........#........#........
|
||||||
|
.........########.........
|
||||||
|
#+end_example
|
||||||
|
|
||||||
|
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
|
||||||
|
once?/
|
||||||
|
99
2022/day09/aoc.bash
Executable file
99
2022/day09/aoc.bash
Executable file
@@ -0,0 +1,99 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
#
|
||||||
|
# aoc.bash: Advent of Code 2022, day 9
|
||||||
|
#
|
||||||
|
# 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 -i xh=0 yh=0 xt=0 yt=0
|
||||||
|
declare -A visited=()
|
||||||
|
|
||||||
|
add_pos() {
|
||||||
|
(( visited["$1-$2"]++ ))
|
||||||
|
}
|
||||||
|
|
||||||
|
direction() {
|
||||||
|
local -n _dx="$1" _dy="$2"
|
||||||
|
local dir="$3"
|
||||||
|
(( _dx=0, _dy=0 ))
|
||||||
|
case "$dir" in
|
||||||
|
L) _dx=-1 ;;
|
||||||
|
R) _dx=1 ;;
|
||||||
|
U) _dy=1 ;;
|
||||||
|
D) _dy=-1 ;;
|
||||||
|
esac
|
||||||
|
}
|
||||||
|
|
||||||
|
move_t() {
|
||||||
|
local -i dx dy sx sy
|
||||||
|
|
||||||
|
(( dx = xh - xt, dy = yh - yt ))
|
||||||
|
(( sx = dx > 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
|
||||||
|
(( dx > 0 )) && (( xt++ ))
|
||||||
|
(( dx < 0 )) && (( xt-- ))
|
||||||
|
(( dy > 0 )) && (( yt++ ))
|
||||||
|
(( dy < 0 )) && (( yt-- ))
|
||||||
|
fi
|
||||||
|
#printf "xt=%d yt=%d] " "$xt" "$yt"
|
||||||
|
add_pos "$xt" "$yt"
|
||||||
|
}
|
||||||
|
|
||||||
|
move_h() {
|
||||||
|
local -i dx="$1" dy="$2" n="$3" i
|
||||||
|
#printf "\tmove_h dx=%d dy=%d n=%d\n" "$dx" "$dy" "$n"
|
||||||
|
for ((i = 0; i < n; ++i)); do
|
||||||
|
(( xh += dx, yh += dy ))
|
||||||
|
#printf -- "(%d,%d)" "$xh" "$yh"
|
||||||
|
move_t
|
||||||
|
#printf -- "/(%d,%d)\n" "$xt" "$yt"
|
||||||
|
done
|
||||||
|
}
|
||||||
|
|
||||||
|
parse() {
|
||||||
|
local dir moves dx dy
|
||||||
|
|
||||||
|
while read -r dir moves; do
|
||||||
|
#printf "(%d,%d)/(%d,%d) %s/%d\n" "$xh" "$yh" "$xt" "$yt" "$dir" "$moves"
|
||||||
|
direction dx dy "$dir"
|
||||||
|
#printf "\tafter direction dx=%d dy=%d\n" "$dx" "$dy"
|
||||||
|
move_h "$dx" "$dy" "$moves"
|
||||||
|
#printf "\th: (%d,%d)/(%d,%d)\n" "$xh" "$yh" "$xt" "$yt"
|
||||||
|
add_pos "$xt" "$yt"
|
||||||
|
done
|
||||||
|
}
|
||||||
|
|
||||||
|
part1() {
|
||||||
|
declare -ig res
|
||||||
|
parse
|
||||||
|
#echo FINAL
|
||||||
|
#echo "${!visited[@]}"
|
||||||
|
res=${#visited[@]}
|
||||||
|
}
|
||||||
|
|
||||||
|
part2() {
|
||||||
|
:
|
||||||
|
}
|
||||||
|
|
||||||
|
solve() {
|
||||||
|
add_pos 0 0
|
||||||
|
if ((part == 1)); then
|
||||||
|
part1
|
||||||
|
else
|
||||||
|
part2
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
main "$@"
|
||||||
|
exit 0
|
Reference in New Issue
Block a user