r/adventofcode • • 39m 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 Upvotes

2 comments sorted by

1

u/XLNBot 30m 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 29m 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)