Well that was a short one.
Part 1
using DataStructures
function part1(fname)
ids = parse.(Int, readlines(fname))
return prod(first.(sort(collect(counter(ids)), by = last, rev = true)[1:3]))
end
Part 2
function part2(fname)
ids = parse.(Int, readlines(fname))
return prod(first.(sort(collect(counter(ids)), by = last)[1:3]))
end
Something Extra
Let’s figure out how these ids were generated.
The histogram looks close to uniform over three digit numbers.
This can’t be a uniform draw though. It’d be hard to ensure that you have exactly three unique maximizers and minimizers if you just ran it by chance.
But even harder would be getting exactly 500 unique ids! Let’s denote \(C_{i}\) as the number of times the three-digit number \(i\) shows up. Then the unique count is \(U = \sum_{i=1}^{K} \mathbf{1}\{C_i \ge 1\}\) with
\[E[U] = K \left(1 - \left(1 - \frac{1}{K}\right)^N\right)\]
and
\[\operatorname{Var}(U) = K\left(1 - \frac{1}{K}\right)^N + K(K - 1)\left(1 - \frac{2}{K}\right)^N - K^2\left(1 - \frac{1}{K}\right)^{2N}\]
Filling this in we get
| Observed lines (N) | 1978 |
|---|---|
| Observed unique IDs | 500 |
| Expected unique IDs (K = 900) | 800.2 ± 8.0 |
| z-score | -37.4 |
Judging from this it seems like the 500 unique ids were originally uniformly sampled without replacement and the resampled to fill out the number of observed lines.
Let’s look at the distribution over counts now:
| Count k | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
|---|---|---|---|---|---|---|---|
| # of IDs | 2 | 1 | 176 | 163 | 155 | 2 | 1 |
Could this be the result of some uniform draw? It looks roughly symmetric which is actually suspicious at this N count.
Let’s define
\[e_{\ge m}(x) = \sum_{c=m}^{\infty} \frac{x^c}{c!}, \qquad e_{\le m-1}(x) = \sum_{c=0}^{m-1} \frac{x^c}{c!}, \qquad e_{[1,m]}(x) = \sum_{c=1}^{m} \frac{x^c}{c!}\]
and then our calculations just becomes convolutions of the generating functions!
\[P(\text{unique top-}k) = \frac{N!}{K^N} \sum_{m=1}^{N} \binom{K}{k} [x^N]\!\left[\left(e_{\ge m}(x)^k - e_{\ge m+1}(x)^k\right) e_{\le m-1}(x)^{K - k}\right]\]
\[P(\text{unique bottom-}k) = \frac{N!}{K^N} \sum_{m=1}^{N} \binom{K}{k} [x^N]\!\left[\left(e_{[1,m]}(x)^k - e_{[1,m-1]}(x)^k\right) \left(e^x - e_{[1,m]}(x)\right)^{K - k}\right]\]
It’s surprisingly easy to get a unique set of three top counts from the uniform: it’s just over 30%. But getting a unique bottom set is essentially impossible: you get so many \(k=1\) observations due to the asymmetry in the selection distribution at low \(N\).
If we had increased \(N\) much higher you can start to look symmetric and indeed the probability becomes ~16% at N=4000..
| K | N | Expected IDs seen once | P(unique top-3) | P(unique bottom-3) |
|---|---|---|---|---|
| 900 | 1978 | 219.6 | 22.1% | 4.1e-116 |
| 500 | 1978 | 37.8 | 31.1% | 2.6e-15 |
| 500 | 3000 | 7.4 | 35.1% | 3.8% |
| 500 | 4000 | 1.3 | 38.3% | 16.2% |
| 500 | 5000 | 0.2 | 40.9% | 23.8% |
So what must have happened is that topaz randomly selected 500 unique ids without replacement and then we assigned the number of counts randomly with buckets of [1,2], [3,4,5], and [6,7]. This gets you a nice round number of expected lines.
\[E[N] = 494 \cdot 4 + 3 \cdot 1.5 + 3 \cdot 6.5 = 2000\]
Then everything just gets shuffled together.