Start on day 9

This commit is contained in:
Timothy Warren 2022-12-09 17:43:10 -05:00
parent dea3674ac4
commit 27483d052f
8 changed files with 2383 additions and 1 deletions

View File

@ -70,4 +70,4 @@ However, you can do even better: consider the tree of height 5 in the middle of
This tree's scenic score is 8 (2 * 2 * 1 * 2); this is the ideal spot for the tree house. This tree's scenic score is 8 (2 * 2 * 1 * 2); this is the ideal spot for the tree house.
Consider each tree on your map. What is the highest scenic score possible for any tree? Consider each tree on your map. **What is the highest scenic score possible for any tree?**

7
day9/Cargo.lock generated Normal file
View File

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "day9"
version = "0.1.0"

8
day9/Cargo.toml Normal file
View File

@ -0,0 +1,8 @@
[package]
name = "day9"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

249
day9/README.md Normal file
View File

@ -0,0 +1,249 @@
# Day 9: Rope Bridge
## Part 1
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?

92
day9/src/grid.rs Normal file
View File

@ -0,0 +1,92 @@
/// A virtual 2d grid in an ordinary vector. Taken from day 8's puzzle solving implementation
#[derive(Debug)]
pub struct Grid<T> {
width: usize,
pub vec: Vec<T>,
}
impl<T> Grid<T> {
pub fn new(width: usize) -> Self {
Grid {
width,
vec: Vec::new(),
}
}
// Convert x,y coordinate into linear array index
pub fn xy_idx(&self, x: usize, y: usize) -> usize {
(y * self.width) + x
}
/// Convert linear array index to x,y coordinate
pub fn idx_xy(&self, idx: usize) -> (usize, usize) {
(idx % self.width, idx / self.width)
}
pub fn get(&self, idx: usize) -> Option<&T> {
self.vec.get(idx)
}
pub fn get_mut(&mut self, idx: usize) -> Option<&mut T> {
self.vec.get_mut(idx)
}
pub fn row_first_idx(&self, row: usize) -> usize {
let idx = row * self.width;
if idx < self.vec.len() {
idx
} else {
self.vec.len()
}
}
pub fn row_last_idx(&self, row: usize) -> usize {
if (row + 1) > self.num_rows() {
return self.vec.len();
}
self.row_first_idx(row + 1) - 1
}
pub fn num_rows(&self) -> usize {
self.vec.len() / self.width
}
pub fn num_cols(&self) -> usize {
self.width
}
pub fn get_row(&mut self, row_num: usize) -> &mut [T] {
let start = self.row_first_idx(row_num);
let end = self.row_last_idx(row_num);
&mut self.vec[start..=end]
}
pub fn get_row_indexes(&self, row_num: usize) -> Vec<usize> {
let start = self.row_first_idx(row_num);
let end = self.row_last_idx(row_num);
(start..=end).collect()
}
pub fn get_column_indexes(&self, col_num: usize) -> Vec<usize> {
let mut indexes = Vec::new();
if col_num >= self.num_cols() {
panic!(
"Asked for column {}, there are {} columns",
col_num,
self.num_cols()
);
}
for r in 0..self.num_rows() {
let idx = self.width * r + col_num;
indexes.push(idx);
}
indexes
}
}

2000
day9/src/input.txt Normal file

File diff suppressed because it is too large Load Diff

18
day9/src/main.rs Normal file
View File

@ -0,0 +1,18 @@
mod grid;
use grid::Grid;
// ----------------------------------------------------------------------------
fn main() {
let file_str = include_str!("input.txt");
}
#[cfg(test)]
mod tests {
use super::*;
fn get_data() -> &'static str {
include_str!("test-input.txt")
}
}

8
day9/src/test-input.txt Normal file
View File

@ -0,0 +1,8 @@
R 4
U 4
L 3
D 1
R 4
D 1
L 5
R 2