Simulate chains simultaneously
This commit is contained in:
parent
1e9d553c22
commit
fe66170e3e
@ -1,5 +1,6 @@
|
|||||||
use crate::read;
|
use crate::read;
|
||||||
use std::collections::HashSet;
|
use std::collections::HashSet;
|
||||||
|
use itertools::Itertools;
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq, Default, Clone, Hash)]
|
#[derive(Debug, PartialEq, Eq, Default, Clone, Hash)]
|
||||||
struct Position {
|
struct Position {
|
||||||
@ -59,43 +60,70 @@ impl Position {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct Chain {
|
||||||
|
pub knots: Vec<Position>,
|
||||||
|
visited: HashSet<Position>
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Chain {
|
||||||
|
pub fn new(num_knots: usize) -> Self {
|
||||||
|
Self {
|
||||||
|
knots: vec![Position::default(); num_knots],
|
||||||
|
visited: HashSet::with_capacity(10000)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn num_visited(&self) -> usize {
|
||||||
|
self.visited.len()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn move_knots(&mut self, direction: &str, count: usize) {
|
||||||
|
let move_head_func = match direction {
|
||||||
|
"R" => Position::move_right,
|
||||||
|
"U" => Position::move_up,
|
||||||
|
"L" => Position::move_left,
|
||||||
|
"D" => Position::move_down,
|
||||||
|
_ => unreachable!(),
|
||||||
|
};
|
||||||
|
for _c in 0..count {
|
||||||
|
self.knots[0] = move_head_func(&mut self.knots[0]);
|
||||||
|
for i in 1..self.knots.len() {
|
||||||
|
self.knots[i] = self.knots[i].follow_head(&self.knots[i - 1]);
|
||||||
|
}
|
||||||
|
|
||||||
|
self.visited.insert(self.knots.last().unwrap().clone());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn run() {
|
pub fn run() {
|
||||||
|
let res = simulate(vec![2, 10]);
|
||||||
#[cfg(feature = "part1")]
|
#[cfg(feature = "part1")]
|
||||||
{
|
{
|
||||||
println!("Day 9, Part 01: {}", simulate(2));
|
println!("Day 9, Part 01: {}", res[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "part2")]
|
#[cfg(feature = "part2")]
|
||||||
{
|
{
|
||||||
println!("Day 9, Part 02: {}", simulate(10));
|
println!("Day 9, Part 02: {}", res[1]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
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");
|
fn simulate(chain_sizes: Vec<usize>) -> Vec<usize> {
|
||||||
let lines = input.lines();
|
let chains = read("09")
|
||||||
for line in lines {
|
.lines()
|
||||||
if line.is_empty() {
|
.filter(|l|!l.is_empty())
|
||||||
continue;
|
.map(|l|l.split_once(' ').unwrap())
|
||||||
}
|
.map(|(dir, count)|(dir, count.parse::<usize>().unwrap()))
|
||||||
let (dir, count) = line.split_once(' ').unwrap();
|
.fold(chain_sizes
|
||||||
let count = count.parse().unwrap();
|
.iter()
|
||||||
for _c in 0..count {
|
.map(|c|Chain::new(*c))
|
||||||
knots[0] = match dir {
|
.collect_vec(),
|
||||||
"R" => knots[0].move_right(),
|
|mut chains, (dir, count)| {
|
||||||
"U" => knots[0].move_up(),
|
|
||||||
"L" => knots[0].move_left(),
|
|
||||||
"D" => knots[0].move_down(),
|
|
||||||
_ => unreachable!(),
|
|
||||||
};
|
|
||||||
for i in 1..knots.len() {
|
|
||||||
knots[i] = knots[i].follow_head(&knots[i - 1]);
|
|
||||||
}
|
|
||||||
|
|
||||||
visited.insert(knots.last().unwrap().clone());
|
chains
|
||||||
}
|
.iter_mut()
|
||||||
}
|
.for_each(|chain|chain.move_knots(dir, count));
|
||||||
visited.len()
|
chains
|
||||||
|
});
|
||||||
|
chains.iter().map(|c|c.num_visited()).collect()
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user