2023-11-20 21:40:03 +01:00
|
|
|
use std::collections::HashSet;
|
2023-11-20 05:27:00 +01:00
|
|
|
|
2023-11-20 21:40:03 +01:00
|
|
|
use aoc_libs::points::Point;
|
|
|
|
|
2023-11-23 00:00:27 +01:00
|
|
|
use crate::{parse::Direction, rope::Rope};
|
2023-11-20 21:40:03 +01:00
|
|
|
|
|
|
|
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());
|
2023-11-20 21:40:03 +01:00
|
|
|
for direction in input {
|
|
|
|
rope.update_rope(direction);
|
2023-11-23 00:00:27 +01:00
|
|
|
visited.insert(*rope.get_tail_pos());
|
2023-11-20 21:40:03 +01:00
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|