summaryrefslogtreecommitdiff
path: root/20.c
diff options
context:
space:
mode:
authorAdam Spragg <adam@spra.gg>2023-01-19 13:56:59 +0000
committerAdam Spragg <adam@spra.gg>2023-01-19 13:56:59 +0000
commit816b7d55d6d3dfb31904cb5d4813fb500984ab5b (patch)
treeafae8fddf2117f17d132e0671858169acdeee1a4 /20.c
parent8f0114df375c7b9bd750e3e0e6fbecb964639d3d (diff)
Puzzle 20: Do a linear scan for the num we're looking for
When the number of iterations was small, looking near the original position of the num was a good idea. As the number of iterations increases, the positions are fairly random, so speed of lookup is mostly a factor of how quickly we can scan the whole buffer. Speeds up part 2 of the problem from 2.2s to 0.12s.
Diffstat (limited to '20.c')
-rw-r--r--20.c7
1 files changed, 3 insertions, 4 deletions
diff --git a/20.c b/20.c
index e427217..677c9df 100644
--- a/20.c
+++ b/20.c
@@ -101,10 +101,9 @@ main(int argc, char ** argv)
int newpos;
struct num tmp;
- // Look for the num whose ordinal is i, starting from the
- // position of the last num we looked at.
- while (nums[pos].ord != i)
- pos = modulo(pos + 1, nnums);
+ // Look for the num whose ordinal is i
+ for (pos = 0; nums[pos].ord != i; ++pos)
+ ;
if (nums[pos].val == 0)
continue;