From ca854d2cd3aefe24fcacb45755efd9f395beb451 Mon Sep 17 00:00:00 2001 From: Joe Ardent Date: Fri, 9 Dec 2022 16:01:08 -0800 Subject: [PATCH] clean up part1 helper fn --- 2022-aoc/src/d9.rs | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/2022-aoc/src/d9.rs b/2022-aoc/src/d9.rs index 013bf76..41d42b3 100644 --- a/2022-aoc/src/d9.rs +++ b/2022-aoc/src/d9.rs @@ -49,16 +49,9 @@ fn new_tail(head: &Position, tail: &Position) -> Position { let dy = head.1 - tail.1; let dx = head.0 - tail.0; match (dx, dy) { - (0, 0) => *tail, - (n, m) if (n.abs() == 1) && (m.abs() == 1) => *tail, - (n, 0) | (0, n) if n.abs() == 1 => *tail, - (_, 2) | (_, -2) if dx.abs() == 1 => (tail.0 + dx, tail.1 + (dy / 2)), - (2, y) | (-2, y) if y.abs() == 1 => { - let sign = dx / dx.abs(); - (tail.0 + sign * (dx.abs() - 1), tail.1 + dy) - } - (n, _) if n.abs() == 2 => (tail.0 + n / 2, tail.1), - (_, n) if n.abs() == 2 => (tail.0, tail.1 + n / 2), - _ => unreachable!("got {:?}", (dx, dy)), + (m, n) if m.abs() < 2 && n.abs() < 2 => *tail, + (m, _) if m.abs() == 2 => (tail.0 + m / 2, tail.1 + dy), + (_, m) if m.abs() == 2 => (tail.0 + dx, tail.1 + m / 2), + _ => unreachable!(), } }