2022-12-09

# Day 09: Rope Bridge

Description:
--- Day 9: Rope Bridge ---

This rope bridge creaks as you walk along it. You aren't sure how old it is, or whether it can even support your weight.

It seems to support the Elves just fine, though. The bridge spans a gorge which was carved out by the massive river far below you.

You step carefully; as you do, the ropes stretch and twist. You decide to distract yourself by modeling rope physics; maybe you can even figure out where not to step.

Consider a rope with a knot at each end; these knots mark the head and the tail of the rope. If the head moves far enough away from the tail, the tail is pulled toward the head.

Due to nebulous reasoning involving Planck lengths, you should be able to model the positions of the knots on a two-dimensional grid. Then, by following a hypothetical series of motions (your puzzle input) for the head, you can determine how the tail will move.

Due to the aforementioned Planck lengths, the rope must be quite short; in fact, the head (H) and tail (T) must always be touching (diagonally adjacent and even overlapping both count as touching):

....
.TH.
....

....
.H..
..T.
....

...
.H. (H covers T)
...

If the head is ever two steps directly up, down, left, or right from the tail, the tail must also move one step in that direction so it remains close enough:

..... ..... .....
.TH.. -> .T.H. -> ..TH.
..... ..... .....

... ... ...
.T. .T. ...
.H. -> ... -> .T.
... .H. .H.
... ... ...

Otherwise, if the head and tail aren't touching and aren't in the same row or column, the tail always moves one step diagonally to keep up:

..... ..... .....
..... ..H.. ..H..
..H.. -> ..... -> ..T..
.T... .T... .....
..... ..... .....

..... ..... .....
..... ..... .....
..H.. -> ...H. -> ..TH.
.T... .T... .....
..... ..... .....

You just need to work out where the tail goes as the head follows a series of motions. Assume the head and the tail both start at the same position, overlapping.

For example:

R 4
U 4
L 3
D 1
R 4
D 1
L 5
R 2

This series of motions moves the head right four steps, then up four steps, then left three steps, then down one step, and so on. After each step, you'll need to update the position of the tail if the step means the head is no longer adjacent to the tail. Visually, these motions occur as follows (s marks the starting position as a reference point):

== Initial State ==

......
......
......
......
H..... (H covers T, s)

== R 4 ==

......
......
......
......
TH.... (T covers s)

......
......
......
......
sTH...

......
......
......
......
s.TH..

......
......
......
......
s..TH.

== U 4 ==

......
......
......
....H.
s..T..

......
......
....H.
....T.
s.....

......
....H.
....T.
......
s.....

....H.
....T.
......
......
s.....

== L 3 ==

...H..
....T.
......
......
s.....

..HT..
......
......
......
s.....

.HT...
......
......
......
s.....

== D 1 ==

..T...
.H....
......
......
s.....

== R 4 ==

..T...
..H...
......
......
s.....

..T...
...H..
......
......
s.....

......
...TH.
......
......
s.....

......
....TH
......
......
s.....

== D 1 ==

......
....T.
.....H
......
s.....

== L 5 ==

......
....T.
....H.
......
s.....

......
....T.
...H..
......
s.....

......
......
..HT..
......
s.....

......
......
.HT...
......
s.....

......
......
HT....
......
s.....

== R 2 ==

......
......
.H.... (H covers T)
......
s.....

......
......
.TH...
......
s.....

After simulating the rope, you can count up all of the positions the tail visited at least once. In this diagram, s again marks the starting position (which the tail also visited) and # marks other positions the tail visited:

..##..
...##.
.####.
....#.
s###..

So, there are 13 positions the tail visited at least once.

Simulate your complete hypothetical series of motions. How many positions does the tail of the rope visit at least once?

--- 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:

== 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)

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:

R 5
U 8
L 8
D 3
R 17
D 10
L 25
U 20

These motions occur as follows (individual steps are not shown):

== 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..............
..........................
..........................
..........................
..........................
..........................

Now, the tail (9) visits 36 positions (including s) at least once:

..........................
..........................
..........................
..........................
..........................
..........................
..........................
..........................
..........................
#.........................
#.............###.........
#............#...#........
.#..........#.....#.......
..#..........#.....#......
...#........#.......#.....
....#......s.........#....
.....#..............#.....
......#............#......
.......#..........#.......
........#........#........
.........########.........

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?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
D 3
U 15
D 13
U 9
D 1
U 13
D 12
L 4
U 6
R 11
D 2
R 7
U 9
R 11
U 3
L 7
U 4
R 2
D 14
U 3
D 14
R 13
L 4
R 1
U 12
D 10
L 8
D 2
L 15
U 1
D 4
U 16
R 15
D 1
R 8
U 14
L 12
U 2
R 1
D 16
L 10
R 11
D 10
L 15
R 7
U 10
L 3
U 11
D 1
L 5
D 3
U 8
R 3
U 5
R 2
D 11
L 2
D 7
L 2
U 3
L 12
D 13
L 12
U 7
R 11
L 4
R 11
L 8
U 16
L 1
R 11
L 4
U 15
L 16
R 15
U 6
R 4
U 3
D 16
R 9
U 8
R 5
U 6
L 2
R 2
D 10
R 7
D 13
U 13
R 14
D 3
L 15
R 3
D 11
L 4
U 12
D 1
U 8
R 8
L 4
D 6
L 15
U 7
D 15
L 11
U 14
D 8
U 15
L 12
R 6
U 13
R 5
U 2
L 1
R 13
D 10
R 7
L 9
U 14
R 1
D 13
L 11
U 9
L 13
R 5
D 14
R 16
U 5
D 9
R 2
D 5
R 8
L 8
U 16
D 12
R 3
U 3
D 5
U 12
D 14
L 9
U 12
R 6
L 9
U 3
D 16
U 15
R 6
D 11
R 10
D 3
U 16
L 11
R 2
L 2
R 9
D 12
L 8
D 8
R 13
L 5
D 17
L 7
D 10
U 13
L 11
U 17
L 13
R 2
D 6
R 14
D 14
L 15
R 8
D 4
U 7
L 6
R 6
U 7
D 12
R 9
L 8
D 15
L 14
R 1
U 8
L 13
D 16
R 2
U 2
L 2
D 13
U 8
D 6
U 10
L 9
R 2
L 7
U 5
R 6
U 12
D 3
R 1
U 4
R 17
U 11
L 3
U 13
L 12
U 6
R 5
D 12
R 4
U 4
R 3
D 11
U 7
L 10
D 12
U 12
D 9
R 4
L 2
R 9
L 13
D 16
L 14
D 5
L 8
D 6
L 13
U 1
R 7
U 5
R 2
U 11
L 10
D 14
R 11
L 5
D 16
U 9
D 3
U 11
D 1
U 5
D 3
U 8
L 1
D 1
U 5
L 17
D 9
L 14
R 8
D 15
R 10
U 4
R 1
U 3
R 5
L 13
D 6
L 15
U 10
L 5
R 14
L 1
R 6
D 10
L 13
D 3
L 1
U 10
D 6
R 16
D 6
R 4
D 9
U 9
D 4
R 1
D 10
R 7
D 14
R 10
D 14
U 14
D 15
R 11
L 16
D 18
R 6
U 8
D 18
R 18
D 14
U 9
R 18
U 14
L 18
U 1
R 14
D 1
U 6
R 3
L 13
D 4
L 9
D 2
U 5
D 6
U 9
L 13
U 12
R 11
D 17
U 17
L 8
U 15
R 16
L 9
D 2
R 16
D 2
L 9
D 9
R 15
D 10
L 12
R 14
L 4
R 17
D 15
U 16
R 7
D 4
U 4
L 7
D 16
R 11
D 2
U 2
L 7
U 15
L 5
U 14
L 7
U 15
D 9
L 14
D 13
U 11
R 10
U 15
L 5
U 16
D 13
U 15
L 18
R 1
D 17
U 18
R 19
L 4
R 15
U 12
D 19
R 5
D 18
L 14
U 5
L 4
R 6
U 15
L 7
R 11
L 15
U 12
D 3
R 14
U 1
L 4
R 3
U 15
L 15
R 6
L 3
U 2
D 6
L 6
D 16
L 19
U 9
L 2
R 13
U 8
R 17
D 17
L 4
R 2
L 7
R 11
D 5
L 14
D 7
R 13
D 2
L 18
U 11
R 2
D 18
U 2
D 6
L 4
U 2
L 15
R 4
D 8
R 1
U 16
L 4
R 5
U 16
D 14
U 2
D 19
L 6
D 14
U 17
L 3
R 11
L 2
R 11
U 11
D 4
U 13
L 6
U 14
D 17
U 15
D 7
R 8
U 3
R 15
D 17
U 16
R 3
D 17
L 9
U 15
R 15
L 11
R 18
D 16
R 17
D 3
L 17
D 4
L 10
U 2
R 2
D 9
L 17
R 19

Part 1:
``````import std/strutils
import std/sequtils
import system

type Pos = object
X: int
Y: int

type Rope = object
Tail: Pos

proc move_rope(input: Rope, vec: Pos): Rope =
var rope = input

let deltaX = rope.Head.X - rope.Tail.X
let deltaY = rope.Head.Y - rope.Tail.Y

if not (abs(deltaX) >= 2 or abs(deltaY) >= 2): return rope

if deltaX > 0:
rope.Tail.X += 1
elif deltaX < 0:
rope.Tail.X -= 1

if deltaY > 0:
rope.Tail.Y += 1
elif deltaY < 0:
rope.Tail.Y -= 1

return rope

proc print_rope(r: Rope, width: int, height: int) =
for y in -height .. height:
for x in -width .. width:
let istail = (x == r.Tail.X and y == r.Tail.Y)
let iszero = (x == 0        and y == 0       )
io.stdout.write "@"
io.stdout.write "H"
elif istail:
io.stdout.write "T"
elif iszero:
io.stdout.write "s"
else:
io.stdout.write "."
io.stdout.write "\n"
io.stdout.write "\n"

proc run09_1(): string =

let lines = splitLines(input).filter(proc(p: string): bool = p != "")

var rope = Rope(Head: Pos(X: 0, Y: 0), Tail: Pos(X: 0, Y: 0))

var tailposmap = @[rope.Tail]

# print_rope(rope, 200, 200)

for line in lines:
let dir = case line.split(" ")[0][0]:
of 'U': Pos(X: 00, Y: -1)
of 'D': Pos(X: 00, Y: +1)
of 'L': Pos(X: -1, Y: 00)
of 'R': Pos(X: +1, Y: 00)
else:   Pos(X: 00, Y: 00)

let cnt = parseInt(line.split(" ")[1])

for _ in 0 ..< cnt:
# echo "== ", line, " ==", "    (dir: [", dir.X, "|", dir.Y, "] )"

rope = move_rope(rope, dir)

# print_rope(rope, 100, 100)
#echo "[", rope.Tail.X, ";", rope.Tail.Y, "] -> [", rope.Head.X, ";", rope.Head.Y, "]"

# echo tailposmap.deduplicate()

return tailposmap.deduplicate().len().intToStr()

when not defined(js):
echo run09_1()
else:
proc js_run09_1(): cstring {.exportc.} =
return cstring(run09_1())
``````
Result: 6037

Part 2:
``````import std/strutils
import std/sequtils
import system

type Pos = object
X: int
Y: int

type Rope = object
Knots: array[10, Pos]

proc move_rope(input: Rope, vec: Pos): Rope =
var rope = input

rope.Knots[0].X += vec.X
rope.Knots[0].Y += vec.Y

for i in 0 ..< 9:

let deltaX = rope.Knots[i].X - rope.Knots[i+1].X
let deltaY = rope.Knots[i].Y - rope.Knots[i+1].Y

if not (abs(deltaX) >= 2 or abs(deltaY) >= 2): return rope

if deltaX > 0:
rope.Knots[i+1].X += 1
elif deltaX < 0:
rope.Knots[i+1].X -= 1

if deltaY > 0:
rope.Knots[i+1].Y += 1
elif deltaY < 0:
rope.Knots[i+1].Y -= 1

return rope

proc run09_2(): string =

let lines = splitLines(input).filter(proc(p: string): bool = p != "")

let Z = Pos(X: 0, Y: 0)

var rope = Rope(Knots: [Z, Z, Z, Z, Z, Z, Z, Z, Z, Z])

var tailposmap = @[rope.Knots[9]]

for line in lines:
let dir = case line.split(" ")[0][0]:
of 'U': Pos(X: 00, Y: -1)
of 'D': Pos(X: 00, Y: +1)
of 'L': Pos(X: -1, Y: 00)
of 'R': Pos(X: +1, Y: 00)
else:   Pos(X: 00, Y: 00)

let cnt = parseInt(line.split(" ")[1])

for _ in 0 ..< cnt:
rope = move_rope(rope, dir)