From 5b2e3ebaf07b9657005127ff5554d9962c5c9ac7 Mon Sep 17 00:00:00 2001 From: Luke Hubmayer-Werner Date: Wed, 21 Dec 2022 14:46:51 +1030 Subject: [PATCH] clean up mix amount --- 2022/day20.c | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/2022/day20.c b/2022/day20.c index a88cbb1..352715d 100644 --- a/2022/day20.c +++ b/2022/day20.c @@ -11,7 +11,6 @@ struct list_node { void mix_right(struct list_node* node, long amount) { struct list_node* new_prev = node->next; - amount %= INPUT_LENGTH-1; // Pop from position node->prev->next = node->next; node->next->prev = node->prev; @@ -29,7 +28,6 @@ void mix_right(struct list_node* node, long amount) { void mix_left(struct list_node* node, long amount) { struct list_node* new_next = node->prev; - amount %= INPUT_LENGTH-1; // Pop from position node->prev->next = node->next; node->next->prev = node->prev; @@ -46,12 +44,22 @@ void mix_left(struct list_node* node, long amount) { } void mix(struct list_node* node) { - int amount = node->value; - if (amount > 0) { - mix_right(node, amount); - } else if (amount < 0) { - mix_left(node, -amount); - } // Do nothing for 0 + int forward_amount = node->value; + int reverse_amount; + const int mod = INPUT_LENGTH-1; + if (forward_amount == 0) return; // Do nothing for 0 + if (forward_amount > 0) { + forward_amount = forward_amount % mod; + reverse_amount = mod - forward_amount; + } else { + reverse_amount = -forward_amount % mod; + forward_amount = mod - reverse_amount; + } + if (forward_amount < reverse_amount) { + mix_right(node, forward_amount); + } else { + mix_left(node, reverse_amount); + } } int main(int argc, char *argv[]) {