Comments (7)
Example board is 6 wide, 5 high.
But... there are values in input.txt
higher than that, so assume that there's no bounds on the board.
from advent-of-code-2022-in-rust.
Movement rules:
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:
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:
[...]
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.
from advent-of-code-2022-in-rust.
I need to model the state of the board, then loop through each move in turn applying it to the head. After each move is applied to the head I can run code which decides if the tail should be updated or not - and logs the co-ordinates that the tail has visited so I can count them at the end.
from advent-of-code-2022-in-rust.
I got 14 against the example. I stopped counting the starting point as somewhere visited by the tail and got the expected answer 13.
Then I tried against input.txt
and got the right answer, 6339.
CORRECTION: That's because I modified example.txt
to add a 1 up 1 right as a test and forgot to remove that again. You actually SHOULD count the 0, 0 square.
from advent-of-code-2022-in-rust.
Output against example.txt
:
day_09 % cargo run
Compiling day_09 v0.1.0 (/Users/simon/Dropbox/Development/advent-of-code-2022-in-rust/day_09)
Finished dev [unoptimized + debuginfo] target(s) in 0.49s
Running `target/debug/day_09`
X.........
..........
..........
..........
..........
..........
..........
..........
..........
..........
direction = R distance = 4
head_pos = (1, 0)
Touching, no need to move
TH........
..........
..........
..........
..........
..........
..........
..........
..........
..........
head_pos = (2, 0)
tail_pos = (1, 0)
.TH.......
..........
..........
..........
..........
..........
..........
..........
..........
..........
head_pos = (3, 0)
tail_pos = (2, 0)
..TH......
..........
..........
..........
..........
..........
..........
..........
..........
..........
head_pos = (4, 0)
tail_pos = (3, 0)
...TH.....
..........
..........
..........
..........
..........
..........
..........
..........
..........
direction = U distance = 4
head_pos = (4, 1)
Diagonally touching, no need to move
...T......
....H.....
..........
..........
..........
..........
..........
..........
..........
..........
head_pos = (4, 2)
tail_pos = (4, 1)
..........
....T.....
....H.....
..........
..........
..........
..........
..........
..........
..........
head_pos = (4, 3)
tail_pos = (4, 2)
..........
..........
....T.....
....H.....
..........
..........
..........
..........
..........
..........
head_pos = (4, 4)
tail_pos = (4, 3)
..........
..........
..........
....T.....
....H.....
..........
..........
..........
..........
..........
direction = L distance = 3
head_pos = (3, 4)
Diagonally touching, no need to move
..........
..........
..........
....T.....
...H......
..........
..........
..........
..........
..........
head_pos = (2, 4)
tail_pos = (3, 4)
..........
..........
..........
..........
..HT......
..........
..........
..........
..........
..........
head_pos = (1, 4)
tail_pos = (2, 4)
..........
..........
..........
..........
.HT.......
..........
..........
..........
..........
..........
direction = D distance = 1
head_pos = (1, 3)
Diagonally touching, no need to move
..........
..........
..........
.H........
..T.......
..........
..........
..........
..........
..........
direction = R distance = 4
head_pos = (2, 3)
Touching, no need to move
..........
..........
..........
..H.......
..T.......
..........
..........
..........
..........
..........
head_pos = (3, 3)
Diagonally touching, no need to move
..........
..........
..........
...H......
..T.......
..........
..........
..........
..........
..........
head_pos = (4, 3)
tail_pos = (3, 3)
..........
..........
..........
...TH.....
..........
..........
..........
..........
..........
..........
head_pos = (5, 3)
tail_pos = (4, 3)
..........
..........
..........
....TH....
..........
..........
..........
..........
..........
..........
direction = D distance = 1
head_pos = (5, 2)
Diagonally touching, no need to move
..........
..........
.....H....
....T.....
..........
..........
..........
..........
..........
..........
direction = L distance = 5
head_pos = (4, 2)
Touching, no need to move
..........
..........
....H.....
....T.....
..........
..........
..........
..........
..........
..........
head_pos = (3, 2)
Diagonally touching, no need to move
..........
..........
...H......
....T.....
..........
..........
..........
..........
..........
..........
head_pos = (2, 2)
tail_pos = (3, 2)
..........
..........
..HT......
..........
..........
..........
..........
..........
..........
..........
head_pos = (1, 2)
tail_pos = (2, 2)
..........
..........
.HT.......
..........
..........
..........
..........
..........
..........
..........
head_pos = (0, 2)
tail_pos = (1, 2)
..........
..........
HT........
..........
..........
..........
..........
..........
..........
..........
direction = R distance = 2
head_pos = (1, 2)
tail_pos = (1, 2)
..........
..........
.X........
..........
..........
..........
..........
..........
..........
..........
head_pos = (2, 2)
Touching, no need to move
..........
..........
.TH.......
..........
..........
..........
..........
..........
..........
..........
tail_visited = [(0, 0), (1, 0), (2, 0), (3, 0), (4, 1), (4, 2), (4, 3), (3, 4), (2, 4), (3, 3), (4, 3), (3, 2), (2, 2), (1, 2), (1, 2)]
Number of unique locations = 13
from advent-of-code-2022-in-rust.
Part 2:
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.
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?
from advent-of-code-2022-in-rust.
Got it to work against the larger example AND got the right result for my input - 2541.
I modified the original script without creating a backup, so now my solution to part 1 lives on in version control:
advent-of-code-2022-in-rust/day_09/src/main.rs
Lines 1 to 98 in b46f205
Here's my part 2 solution:
advent-of-code-2022-in-rust/day_09/src/main.rs
Lines 1 to 108 in 58aae1d
from advent-of-code-2022-in-rust.
Related Issues (17)
- Day 1: Calorie Counting HOT 2
- Day 8: Treetop Tree House HOT 27
- Day 10: Cathode-Ray Tube HOT 8
- Day 11: Monkey in the Middle HOT 18
- Day 12: Hill Climbing Algorithm HOT 29
- Day 13: Distress Signal HOT 33
- Day 14: Regolith Reservoir HOT 17
- Day 15: Beacon Exclusion Zone HOT 18
- Day 16: Proboscidea Volcanium HOT 3
- Figure out CI HOT 2
- Day 2: Rock Paper Scissors HOT 18
- Day 3: Rucksack Reorganization HOT 28
- Day 4: Camp Cleanup HOT 14
- Day 5: Supply Stacks HOT 17
- Day 6: Tuning Trouble HOT 16
- Day 7: No Space Left On Device HOT 41
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from advent-of-code-2022-in-rust.