From fd961566c0aba44db962ace7033ccb2b4d3845ea Mon Sep 17 00:00:00 2001 From: Viacheslav Slinko Date: Sun, 22 Dec 2024 10:53:43 +0300 Subject: [PATCH] day22 --- src/day22.rs | 65 ++++++++++++++++++++++++---------------------------- 1 file changed, 30 insertions(+), 35 deletions(-) diff --git a/src/day22.rs b/src/day22.rs index 640446b..2b454a7 100644 --- a/src/day22.rs +++ b/src/day22.rs @@ -68,46 +68,41 @@ pub fn part2(input: &str) -> i64 { }; } + macro_rules! iter2 { + ($number:expr, $prev:expr, |$new_price:ident| $block:block) => { + iter!($number); + let $new_price = $number % 10; + $block + $prev = $new_price; + }; + } + for line in input.lines() { let mut number = line.parse::().unwrap(); - let mut new_price = number % 10; - let mut prev = new_price; - - iter!(number); - new_price = number % 10; - diff_seq[0] = new_price - prev; - prev = new_price; - - iter!(number); - new_price = number % 10; - diff_seq[1] = new_price - prev; - prev = new_price; - - iter!(number); - new_price = number % 10; - diff_seq[2] = new_price - prev; - prev = new_price; - - iter!(number); - new_price = number % 10; - diff_seq[3] = new_price - prev; - prev = new_price; - - remember_seq!(diff_seq, new_price); - - for _ in 4..2000 { - iter!(number); - - new_price = number % 10; - - diff_seq[0] = diff_seq[1]; - diff_seq[1] = diff_seq[2]; - diff_seq[2] = diff_seq[3]; + let mut prev = number % 10; + + iter2!(number, prev, |new_price| { + diff_seq[0] = new_price - prev; + }); + iter2!(number, prev, |new_price| { + diff_seq[1] = new_price - prev; + }); + iter2!(number, prev, |new_price| { + diff_seq[2] = new_price - prev; + }); + iter2!(number, prev, |new_price| { diff_seq[3] = new_price - prev; - remember_seq!(diff_seq, new_price); + }); - prev = new_price; + for _ in 4..2000 { + iter2!(number, prev, |new_price| { + diff_seq[0] = diff_seq[1]; + diff_seq[1] = diff_seq[2]; + diff_seq[2] = diff_seq[3]; + diff_seq[3] = new_price - prev; + remember_seq!(diff_seq, new_price); + }); } already_done.clear();