advent2022

Advent of Code 2022 Solutions
git clone git://bsandro.tech/advent2022
Log | Files | Refs | README | LICENSE

commit 8412a6688aab1731e93b80893b1ed456ad0b4e68
parent 5440938450c4f686a61b0d3acbec74f0858fa0ba
Author: bsandro <email@bsandro.tech>
Date:   Fri,  9 Dec 2022 22:01:28 +0000

Day 09 part 2

Diffstat:
Mday09/main.go | 169+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------
Rday09/sample.txt -> day09/sample1.txt | 0
Aday09/sample2.txt | 8++++++++
Aday09/task.txt | 685+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 838 insertions(+), 24 deletions(-)

diff --git a/day09/main.go b/day09/main.go @@ -4,17 +4,21 @@ import ( "bufio" "fmt" "log" - "os" "math" + "os" ) func abs(a int) int { return int(math.Abs(float64(a))) } +func copysign(x, s int) int { + return int(math.Copysign(float64(x), float64(s))) +} type Pos struct { x, y int } + func (p Pos) Diff(o Pos) Pos { var diff Pos diff.x = p.x - o.x @@ -32,10 +36,14 @@ func (p Pos) Dist(o Pos) int { return dy } } + type Rope struct { + id int head, tail Pos - visited []Pos + visited []Pos + attach *Rope } + func (r *Rope) AddVisited(p Pos) { add := true for _, v := range r.visited { @@ -48,44 +56,105 @@ func (r *Rope) AddVisited(p Pos) { r.visited = append(r.visited, p) } } -func (r *Rope) MoveTail() bool { +func (r *Rope) MoveTail() { if r.head.Dist(r.tail) > 1 { d := r.head.Diff(r.tail) - if abs(d.x)==2 { + if abs(d.x) > 1 { d.x /= 2 } - if abs(d.y)==2 { + if abs(d.y) > 1 { d.y /= 2 } r.tail.x += d.x r.tail.y += d.y - return true + + r.AddVisited(r.tail) } - return false } func (r *Rope) Step(dx, dy int) { r.head.x += dx r.head.y += dy - if r.MoveTail() { - r.AddVisited(r.tail) + r.MoveTail() + + if r.attach != nil { + d := r.tail.Diff(r.attach.head) + if d.x != 0 || d.y != 0 { + r.attach.MoveBy(d) + } } } -func (r *Rope) Move(dir string, dist int) { - //fmt.Println("direction:", dir, "distance:", dist) - for d:=0; d<dist; d++ { - switch dir { - case "L": - r.Step(-1, 0) - case "R": - r.Step(1, 0) - case "U": - r.Step(0, 1) - case "D": - r.Step(0, -1) - default: - log.Fatal("Invalid direction", dir) +func (r *Rope) MoveBy(p Pos) { + ax, ay := abs(p.x), abs(p.y) + if ax != 0 && ay != 0 { + // diagonal move + for { + if ax == 0 || ay == 0 { + break + } + r.Step(copysign(1, p.x), copysign(1, p.y)) + ax-- + ay-- + } + } + if ax != 0 { + for i := 0; i < ax; i++ { + r.Step(copysign(1, p.x), 0) } + } else if ay != 0 { + for i := 0; i < ay; i++ { + r.Step(0, copysign(1, p.y)) + } + } +} +func (r *Rope) Move(dir string, dist int) { + switch dir { + case "L": + r.MoveBy(Pos{x: -dist}) + case "R": + r.MoveBy(Pos{x: dist}) + case "U": + r.MoveBy(Pos{y: dist}) + case "D": + r.MoveBy(Pos{y: -dist}) + default: + log.Fatal("Invalid direction", dir) + } +} + +type Chain struct { + first *Rope + last *Rope + size int +} + +func (c *Chain) Move(dir string, dist int) { + c.first.Move(dir, dist) + // c.Print() +} +func (c *Chain) Print() { + var points []Pos + for rope := c.first; rope != nil; { + points = append(points, rope.head) + points = append(points, rope.tail) + rope = rope.attach } + PrintPoints(points) +} +func makeChain(size int) Chain { + var c Chain + c.size = size + c.first = &Rope{} + c.first.AddVisited(c.first.tail) + + prev := c.first + for i := 1; i < size; i++ { + r := &Rope{id: i} + r.AddVisited(r.tail) + prev.attach = r + prev = r + } + c.last = prev + return c } func main() { @@ -108,6 +177,7 @@ func day09(input_file string) { scanner := bufio.NewScanner(input) var rope Rope rope.AddVisited(rope.tail) + chain := makeChain(9) for scanner.Scan() { in := scanner.Text() var dir string @@ -117,10 +187,61 @@ func day09(input_file string) { log.Fatal("Invalid input string", in, err) } rope.Move(dir, dist) + chain.Move(dir, dist) } if err = scanner.Err(); err != nil { log.Fatal(err) } - fmt.Printf("visited: %d\n", len(rope.visited)) + fmt.Println("Part 1:", len(rope.visited)) + fmt.Println("Part 2:", len(chain.last.visited)) + + // PrintPoints(chain.last.visited) +} + +func PrintPoints(points []Pos) { + var min, max Pos + for _, p := range points { + if p.x < min.x { + min.x = p.x + } + if p.y < min.y { + min.y = p.y + } + if p.x > max.x { + max.x = p.x + } + if p.y > max.y { + max.y = p.y + } + } + if min.x > 0 { + min.x = 0 + } + if min.y > 0 { + min.y = 0 + } + dx, dy := -min.x, -min.y + mx, my := max.x+dx+3, max.y+dy+3 + grid := make([][]bool, my) + for i := 0; i < my; i++ { + grid[i] = make([]bool, mx) + } + for _, p := range points { + p.x += dx + p.y += dy + grid[p.y][p.x] = true + } + for y := my - 1; y >= 0; y-- { + for x := 0; x < mx; x++ { + if grid[y][x] { + fmt.Print("#") + } else if x == dx && y == dy { + fmt.Print("X") + } else { + fmt.Print(".") + } + } + fmt.Print("\n") + } } diff --git a/day09/sample.txt b/day09/sample1.txt diff --git a/day09/sample2.txt b/day09/sample2.txt @@ -0,0 +1,8 @@ +R 5 +U 8 +L 8 +D 3 +R 17 +D 10 +L 25 +U 20 diff --git a/day09/task.txt b/day09/task.txt @@ -0,0 +1,685 @@ +--- 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? + +Your puzzle answer was 6023. +--- 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? + +Your puzzle answer was 2533.