This commit is contained in:
Max Nuding 2022-12-09 05:48:13 +00:00
parent a3d1031b05
commit ad889622d5
2 changed files with 2089 additions and 6 deletions

File diff suppressed because it is too large Load Diff

View File

@ -1,18 +1,101 @@
use crate::read; use crate::read;
use itertools::Itertools; use itertools::Itertools;
use std::collections::HashSet;
#[derive(Debug, PartialEq, Eq, Default, Clone, Copy, Hash)]
struct Position {
pub x: i32,
pub y: i32,
}
impl Position {
pub fn to_right(&self, count: i32) -> Self {
Self {
x: self.x + count,
y: self.y,
}
}
pub fn to_left(&self, count: i32) -> Self {
Self {
x: self.x - count,
y: self.y,
}
}
pub fn to_up(&self, count: i32) -> Self {
Self {
x: self.x,
y: self.y - count,
}
}
pub fn to_down(&self, count: i32) -> Self {
Self {
x: self.x,
y: self.y + count,
}
}
pub fn follow_head(&self, head: &Position) -> Self {
if head.x == self.x && head.y > self.y + 1 {
self.to_down(1)
} else if head.x == self.x && head.y < self.y - 1 {
self.to_up(1)
} else if head.x > self.x + 1 && head.y == self.y {
self.to_right(1)
} else if head.x < self.x - 1 && head.y == self.y {
self.to_left(1)
} else if (head.x - self.x).abs() == 1 && (head.y - self.y).abs() == 1 {
*self
} else if head.x < self.x && head.y < self.y {
self.to_left(1).to_up(1)
} else if head.x > self.x && head.y < self.y {
self.to_right(1).to_up(1)
} else if head.x > self.x && head.y > self.y {
self.to_right(1).to_down(1)
} else if head.x < self.x && head.y > self.y {
self.to_left(1).to_down(1)
} else {
*self
}
}
}
pub fn run() { pub fn run() {
let input = read("08");
let lines = input.lines();
for line in lines {}
#[cfg(feature = "part1")] #[cfg(feature = "part1")]
{ {
println!("Day 9, Part 01: {}", "TODO"); println!("Day 9, Part 01: {}", simulate(2));
} }
#[cfg(feature = "part2")] #[cfg(feature = "part2")]
{ {
println!("Day 9, Part 02: {}", "TODO"); println!("Day 9, Part 02: {}", simulate(10));
} }
} }
pub fn simulate(num_knots: usize) -> usize {
let mut visited: HashSet<Position> = HashSet::new();
let mut knots = vec![Position::default(); num_knots];
let input = read("09");
let lines = input.lines();
for line in lines {
if line.is_empty() {
continue;
}
let (dir, count) = line.split_once(' ').unwrap();
let count = count.parse().unwrap();
for _c in 0..count {
knots[0] = match dir {
"R" => knots[0].to_right(1),
"U" => knots[0].to_up(1),
"L" => knots[0].to_left(1),
"D" => knots[0].to_down(1),
_ => unreachable!(),
};
for i in 1..knots.len() {
knots[i] = knots[i].follow_head(&knots[i - 1]);
}
visited.insert(*knots.last().unwrap());
}
}
visited.len()
}