Day 9 part 2

This commit is contained in:
Wesley Moore 2022-12-09 19:29:11 +10:00
parent ad2c828913
commit 61559271c8
No known key found for this signature in database

View file

@ -14,11 +14,10 @@ fn main() -> Result<(), Box<dyn Error>> {
fn part1(input: &str) -> Result<(), Box<dyn Error>> { fn part1(input: &str) -> Result<(), Box<dyn Error>> {
let mut tail_visited = HashSet::new(); let mut tail_visited = HashSet::new();
let mut head = (0, 0); let mut knots = [(0, 0); 2];
let mut tail = (0, 0);
for line in input.lines() { for line in input.lines() {
execute(line, &mut head, &mut tail, &mut tail_visited)?; execute(line, &mut knots, &mut tail_visited)?;
} }
println!("Part 1: tail visited {} positions", tail_visited.len()); println!("Part 1: tail visited {} positions", tail_visited.len());
@ -27,13 +26,21 @@ fn part1(input: &str) -> Result<(), Box<dyn Error>> {
} }
fn part2(input: &str) -> Result<(), Box<dyn Error>> { fn part2(input: &str) -> Result<(), Box<dyn Error>> {
let mut tail_visited = HashSet::new();
let mut knots = [(0, 0); 10];
for line in input.lines() {
execute(line, &mut knots, &mut tail_visited)?;
}
println!("Part 2: tail visited {} positions", tail_visited.len());
Ok(()) Ok(())
} }
fn execute( fn execute(
line: &str, line: &str,
head: &mut Pos, knots: &mut [Pos],
tail: &mut Pos,
visited: &mut HashSet<Pos>, visited: &mut HashSet<Pos>,
) -> Result<(), Box<dyn Error>> { ) -> Result<(), Box<dyn Error>> {
// let h = *head; // let h = *head;
@ -48,15 +55,17 @@ fn execute(
Some(("R", count)) => ((1, 0), count), Some(("R", count)) => ((1, 0), count),
_ => Err(format!("invalid line: '{}'", line))?, _ => Err(format!("invalid line: '{}'", line))?,
}; };
(0..count).for_each(|_| do_move(delta, head, tail, visited)); (0..count).for_each(|_| do_move(delta, knots, visited));
// println!("{}: H{:?} T{:?} -> H{:?} T{:?}", line, h, t, head, tail); // println!("{}: H{:?} T{:?} -> H{:?} T{:?}", line, h, t, head, tail);
Ok(()) Ok(())
} }
fn do_move(delta: Pos, head: &mut Pos, tail: &mut Pos, visited: &mut HashSet<Pos>) { fn do_move(delta: Pos, knots: &mut [Pos], visited: &mut HashSet<Pos>) {
*head = add(*head, delta); knots[0] = add(knots[0], delta);
*tail = move_tail(*head, *tail); for i in 1..knots.len() {
visited.insert(*tail); knots[i] = move_tail(knots[i - 1], knots[i]);
}
visited.insert(*knots.last().unwrap());
} }
fn move_tail(head: Pos, tail: Pos) -> Pos { fn move_tail(head: Pos, tail: Pos) -> Pos {