From 2e4bd3352ccde9b5380945bf483002416a50d71f Mon Sep 17 00:00:00 2001 From: Adam Spragg Date: Sun, 11 Dec 2022 18:42:43 +0000 Subject: Solve puzzle 11 part 2 The trick is that we don't care about how much worry there is, we only care how much worry modulo the test divisor, to figure out which monkey gets the item next. Now, I know that addition is stable under modulo arithmetic, but I seem to remember that multiplication is stable under modulo arithmetic also? (I think public key cryptography relies on this?) So, by only keeping track of our worry, modulo the product of all the test divisors, everything should still work correctly? At least, that's what I tried, and it stopped all the overflow, and I seemed to get the right numbers according to the expected test results, and my final answer was also accepted. So I guess it does work correctly! --- 11.c | 5 +++++ 1 file changed, 5 insertions(+) (limited to '11.c') diff --git a/11.c b/11.c index 09894d8..de76e5c 100644 --- a/11.c +++ b/11.c @@ -95,6 +95,7 @@ main(int argc, char ** argv) int nmonkeys = 0; long i, j; int calming = 3, rounds = 20; + unsigned long test_mod = 1; while ((i = getopt(argc, argv, "p:c:r:")) != -1) { switch (i) { @@ -205,6 +206,8 @@ main(int argc, char ** argv) free(monkeys); return -1; } + + test_mod *= m->test_div; } // Perform monkey inspection rounds @@ -242,6 +245,8 @@ main(int argc, char ** argv) } *item /= calming; + *item %= test_mod; + catcher = *item % m->test_div ? m->test_false : m->test_true; monkey_additem(monkeys + catcher, *item); -- cgit v1.2.1