summaryrefslogtreecommitdiff
path: root/11.c
diff options
context:
space:
mode:
authorAdam Spragg <adam@spra.gg>2022-12-11 18:42:43 +0000
committerAdam Spragg <adam@spra.gg>2022-12-11 18:42:43 +0000
commit2e4bd3352ccde9b5380945bf483002416a50d71f (patch)
treeb5a06d4ae3dd2b0383ee8ef0cd6bbf9bc423b7ce /11.c
parente9700bcb5457ccc137956558b1f4eb780a748b0d (diff)
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!
Diffstat (limited to '11.c')
-rw-r--r--11.c5
1 files changed, 5 insertions, 0 deletions
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);