mirror of
https://github.com/wezm/advent-of-code.git
synced 2024-12-18 10:19:55 +00:00
Day 9 part 2
This commit is contained in:
parent
ad2c828913
commit
61559271c8
1 changed files with 19 additions and 10 deletions
|
@ -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 {
|
||||||
|
|
Loading…
Reference in a new issue