r/adventofcode • • 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.

7 Upvotes

9 comments sorted by

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.

1

u/fnordargle 54m ago

The problem with "evicting entries from the middle" is you can only do that at, or near the end of the file, by which time you've done 99.9999% of the work and not saved anything.

Consider this contrived example:

Imagine you had an input where the first 2 million lines contain two instances of the numbers 1 to 1,000,000.

We can be in this state and have just 6 lines left to process and still able to have a unique deterministic solution. All you need is the last 6 lines to have one more occurrence of 3 numbers already in the file (so they become the three most frequently occurring numbers) and then 3 numbers that have not appeared in the file before, and they become the least 3 frequent.

(And don't say you can rely on everything having the same count at this point, as it is easy to create other contrived inputs that don't have this property.)

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/XLNBot 4h ago

This does not require you to store the whole dataset and it can be pretty efficient if you assume that n is low (which is an assumption that you usually can't make)

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/fnordargle 33m ago

Doing it in batches and going over the file again and again is what I describe below section (b).

Sorting the input file requires huge resources and is computationally very expensive. If you want to try for yourself, and have the disk space, write 20GB of random integers to a file and then have something try and sort it. It takes much longer than you'd expect.

Also writing the file out to `n` different chunks and then processing each linearly (without sorting) will obviously be faster than writing the file out to `n` different chunks, sorting each of those, and then combining them (which is what merge sort effectively does) before processing the entire sorted file linearly.

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

1

u/fnordargle 25m ago

Again, you don't know what is at the edges until you've processed the vast majority of it, by which time it's too big to fit into our finite limited memory that we have said is too small to do so..

Take the contrived example of an input consisting of 2 million lines containing two occurrences each of 1 million values.

If we take the idea that it is impossible to hold all of the unique values in memory then what information are you trying to throw away. If you can't hold all of the unique values then you have to throw some away. And what if it was one of those values you've thrown away that appears in one of the next lines to make it one of the most frequent.

Similarly, you can't guarantee to be able to compress the data by storing ranges of consecutive numbers as there's no guarantee that any of the numbers are adjacent at all.

Thinking about it it's pretty simple to prove that it is impossible to solve this for a non-rewindable/rnon-replayable stream with a finite memory constraint and no local storage.

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...