advent_of_code_2022/days/day09/src/part2.rs

32 lines
721 B
Rust
Raw Normal View History

use std::collections::HashSet;
2023-11-20 05:27:00 +01:00
use aoc_libs::points::Point;
2023-11-23 00:00:27 +01:00
use crate::{parse::Direction, rope::Rope};
pub fn part2(input: &Vec<Direction>) -> usize {
let mut visited: HashSet<Point> = HashSet::new();
let mut rope: Rope<10> = Rope::new();
2023-11-23 00:00:27 +01:00
visited.insert(*rope.get_tail_pos());
for direction in input {
rope.update_rope(direction);
2023-11-23 00:00:27 +01:00
visited.insert(*rope.get_tail_pos());
}
visited.len()
2023-11-20 05:27:00 +01:00
}
#[cfg(test)]
mod tests {
2023-11-23 00:00:27 +01:00
use crate::parse::parse;
2023-11-20 05:27:00 +01:00
use super::*;
#[test]
fn test_part2() {
2023-11-23 00:00:27 +01:00
let input = parse(concat!(
"R 5\n", "U 8\n", "L 8\n", "D 3\n", "R 17\n", "D 10\n", "L 25\n", "U 20\n",
));
assert_eq!(part2(&input), 36);
2023-11-20 05:27:00 +01:00
}
}