r/adventofcode • u/fnordargle • 4h ago
Upping the Ante [2026 Day 1] Huge inputs
Trying to think of algorithms that can solve this for huge inputs, or where many of the assumptions about the input do not hold.
By huge I mean the inputs are large enough that you don't even have enough memory to store counters for every number in the input, let along read the entire input file into memory.
It's like the definition of "Big Data":
* It's not when you can't fit the entire dataset into memory, it's when you can't even fit the index into memory
I like the think about problems like this with increasing levels of restrictions.
With a file with only ~2000 lines we don't have to think about complexity that much, but if the file contained 4,000,000,000 lines we could have the situation where we have three numbers that only appeared once, three numbers that only appeared 3 times, and then 1,999,999,994 numbers that appear twice.
Another assumption that some solutions make is that the frequency count of the numbers that appear in the top and bottom 3 must themselves be relatively unique as 4 numbers that all appear 4 times cannot all be in the top or bottom 3 in a deterministic manner.
To counter this it's trivial to craft an input that has a huge number of lines where even the frequency counts are unique. e.g. Consider the input formed of numbers `1` to `n` where every number `x` appears `x` times. Every item has a unique frequency count but the answer is still deterministic. (And it is similarly trivial to swap 1, 2, 3, `n-2`, `n-1`, `n` with 6 other numbers in the range so the answer isn't obvious up front.)
a) Let's pretend that memory is constrained such that we don't even have enough memory to store counters for all of the unique elements in the input, but we are free to read/write to intermediate local files and that we have enough space to store a bit more than another copy of the original input file.
The standard approach here is to split the input file into `n` smaller files, making sure that any occurrence of a number always appears in the same file. So for number `x` we write it to file `x mod n` every time we encounter it in the input. We choose `n` such that we *can* process `n` individual counters in memory in one go.
Once we've split the input into `n` sub files we process each one of those in turn in memory using the standard `O(n)` algorithm and keep track of the top 3 and bottom 3 item counts we've seen as we go.
b) Now let's imagine that we have no extra local storage, can we still do this?
Yes, but it's going to be a lot slower. Instead of splitting the input into `n` smaller files we just iterate over the main input file `n` times only processing lines that have `x mod n = a` as we loop `a` from `0` to `n-1`. This has the same effect as above but there's a lot of redundant reading involved as the vast majority of inputs read in each loop will be ignored.
c) Final restriction is what if the input is presented to us as a stream that we can't rewind or replay, and we can't write to any local storage, and we can't keep track of all of the possible values in memory. Possible?
I can't think of anything that guarantees getting the right answer without making some assumptions about the input.
2
u/coriolinus 2h ago
Ultimately we want some kind of map counter. But if we are at the point where there's so much data that we can't even keep a map because there are too many keys, then it should be possible to design a custom counter which, once it grows big enough, starts evicting entries from the middle. We sacrifice accuracy with a algorithm, but it should hopefully be fine for cases with a normally randomized distribution.
6
u/XLNBot 4h ago
There are big data algorithms that make this more efficient, but they give up accuracy and they fail if you give them a specific input.
The only way to do this with 100% accuracy regardless of the input (that I know of) is storing a hashmap of n key - count pairs (where n is the number of distinct elements).
You can then pick the three most (or least) common elements with a single iteration over all the pairs
1
u/TheThiefMaster 2h ago
You can do it with only enough memory for four numbers and counts - but it would need a crazy number of passes through the file, keeping track of the three highest known and doing a pass per single number to count it!
More reasonably, you could cap your storage at the first million numbers and their counts, pass through the file accumulating the count of the first million numbers, then discard all but the top three and repeat for the next million. The larger you can push the temp storage, the more numbers you can count per pass.
The actual implementations you've seen just assume you can have enough cache for a single pass count of all numbers.
Alternatively, you can sort the file, and then pass through it counting adjacent duplicates and keeping track of the 3 highest. This can be done reasonably linearly using a merge sort so wouldn't be too bad on the disk.
1
u/T1lted4lif3 1h ago
I was literally thinking about this, what I feel like you can do is do a stream sketch, so for most common and most rare you don’t need to store everything in the middle but estimate what are at the edges,
Maybe could do the trick? Not a stream expert though
3
u/DelightfulCodeWeasel 2h ago
I was trying to do some searching on a very similar theme earlier, to see if there were any algorithms that could find the most frequent element(s) with a single pass through the list and without storing data proportional to the range of numbers in the input, and I ended up finding this paper which compares the internal size of the memory for an algorithm versus the number of possible sequence prefixes as part of a proof.
My extremely naive understanding of the argument goes something like this:
Imagine I'm going to give you a list of 2^31+1 x 32-bit numbers. You don't know this up front, but every single number in the first 2^31 is unique, and the 2^31+1th number is a duplicate of one of the numbers in the first 2^31. Using a 256Mb bit array we can keep track of which numbers we've already seen, and then you finally pick up the repeat on that very last number.
If you use less than 256Mb* to keep track of the numbers you've seen, then it means that there must be two or more distinct sequences of those first 2^31 numbers which map to the same internal memory state of the algorithm. Since there are two distinct sequences that the algorithm can't tell apart, it can't give you a guarantee that it's picked up the single repeat correctly.
I've almost certainly butchered parts of the argument, and I'm sure there are going to be shortcuts to encoding sequences that have certain properties that mean you can do it in less, but I think the overall gist is still that you need memory that's at least proportional to (some factor/power/log of) the size of the alphabet and the input stream - you're not going to be able to find an algorithm that works in a small memory footprint which can deal with arbitrarily large sequences and alphabets.
[*] Or some number of bits that can encode 2^32 choose 2^31, unsure which would be smaller...