From 3055794744e64ef2b8165e9316ce6e2d49780c9a Mon Sep 17 00:00:00 2001 From: kageru Date: Fri, 9 Dec 2022 13:05:41 +0100 Subject: [PATCH] make tuples immutable --- 2022/src/bin/day09.rs | 25 +++++++++---------------- 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/2022/src/bin/day09.rs b/2022/src/bin/day09.rs index d06831e..eda048d 100644 --- a/2022/src/bin/day09.rs +++ b/2022/src/bin/day09.rs @@ -24,7 +24,7 @@ fn rope(parsed: &Parsed) -> usize { let mut positions = HashSet::default(); positions.insert((0, 0)); for &(dir, n) in parsed { - for _ in 0..n { + 'outer: for _ in 0..n { match dir { b'U' => knots[0].1 += 1, b'D' => knots[0].1 -= 1, @@ -32,33 +32,26 @@ fn rope(parsed: &Parsed) -> usize { b'R' => knots[0].0 += 1, _ => unreachable!(), } - let mut tail_moved = true; for i in 0..KNOTS - 1 { - let ro = knots[i]; - if !step_towards(&mut knots[i + 1], ro) { - tail_moved = false; - break; + if let Some(p) = step_towards(knots[i + 1], knots[i]) { + knots[i + 1] = p; + } else { + continue 'outer; } } - if tail_moved { - positions.insert(knots[KNOTS - 1]); - } + positions.insert(knots[KNOTS - 1]); } } positions.len() } #[inline] -fn step_towards(tail: &mut (i32, i32), head: (i32, i32)) -> bool { +fn step_towards(tail: (i32, i32), head: (i32, i32)) -> Option<(i32, i32)> { let xdiff = head.0 - tail.0; let ydiff = head.1 - tail.1; match (xdiff, ydiff) { - (-1 | 0 | 1, -1 | 0 | 1) => false, - _ => { - tail.0 += xdiff.signum(); - tail.1 += ydiff.signum(); - true - } + (-1 | 0 | 1, -1 | 0 | 1) => None, + _ => Some((tail.0 + xdiff.signum(), tail.1 + ydiff.signum())), } }