mirror of
https://github.com/bertptrs/adventofcode.git
synced 2025-12-26 21:30:31 +01:00
Implement 2022 day 9
This commit is contained in:
2000
2022/inputs/09.txt
Normal file
2000
2022/inputs/09.txt
Normal file
File diff suppressed because it is too large
Load Diff
@@ -25,12 +25,12 @@ enum Direction {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Direction {
|
impl Direction {
|
||||||
fn vec_for(self, dist: u32) -> Vec2 {
|
fn vec_for(self) -> Vec2 {
|
||||||
Vec2(match self {
|
Vec2(match self {
|
||||||
Direction::Up => [0, -(dist as i32)],
|
Direction::Up => [0, -1],
|
||||||
Direction::Left => [dist as i32, 0],
|
Direction::Left => [1, 0],
|
||||||
Direction::Right => [-(dist as i32), 0],
|
Direction::Right => [-1, 0],
|
||||||
Direction::Down => [0, dist as i32],
|
Direction::Down => [0, 1],
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -105,12 +105,14 @@ fn part_generic<const N: usize>(input: &[u8]) -> Result<String> {
|
|||||||
visited.insert(head_pos);
|
visited.insert(head_pos);
|
||||||
|
|
||||||
for (direction, steps) in moves {
|
for (direction, steps) in moves {
|
||||||
head_pos = head_pos + direction.vec_for(steps);
|
let step = direction.vec_for();
|
||||||
|
|
||||||
let mut ref_pos = head_pos;
|
for _ in 0..steps {
|
||||||
|
head_pos = head_pos + step;
|
||||||
|
|
||||||
for (i, tail_pos) in tails.iter_mut().enumerate() {
|
let mut ref_pos = head_pos;
|
||||||
loop {
|
|
||||||
|
for (i, tail_pos) in tails.iter_mut().enumerate() {
|
||||||
let delta = ref_pos - *tail_pos;
|
let delta = ref_pos - *tail_pos;
|
||||||
|
|
||||||
if delta[0].abs() <= 1 && delta[1].abs() <= 1 {
|
if delta[0].abs() <= 1 && delta[1].abs() <= 1 {
|
||||||
@@ -124,8 +126,10 @@ fn part_generic<const N: usize>(input: &[u8]) -> Result<String> {
|
|||||||
if i == N - 1 {
|
if i == N - 1 {
|
||||||
visited.insert(*tail_pos);
|
visited.insert(*tail_pos);
|
||||||
}
|
}
|
||||||
|
ref_pos = *tail_pos;
|
||||||
}
|
}
|
||||||
ref_pos = *tail_pos;
|
|
||||||
|
visited.insert(*tails.last().unwrap());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -137,7 +141,6 @@ pub fn part1(input: &[u8]) -> Result<String> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn part2(input: &[u8]) -> Result<String> {
|
pub fn part2(input: &[u8]) -> Result<String> {
|
||||||
// Guesses: 2363 (too low)
|
|
||||||
part_generic::<9>(input)
|
part_generic::<9>(input)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
8
2022/src/samples/09.large.txt
Normal file
8
2022/src/samples/09.large.txt
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
R 5
|
||||||
|
U 8
|
||||||
|
L 8
|
||||||
|
D 3
|
||||||
|
R 17
|
||||||
|
D 10
|
||||||
|
L 25
|
||||||
|
U 20
|
||||||
8
2022/src/samples/09.txt
Normal file
8
2022/src/samples/09.txt
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
R 4
|
||||||
|
U 4
|
||||||
|
L 3
|
||||||
|
D 1
|
||||||
|
R 4
|
||||||
|
D 1
|
||||||
|
L 5
|
||||||
|
R 2
|
||||||
Reference in New Issue
Block a user