Expand description
ยงChocolate Charts
This solution is heavily inspired by Askalskiโs excellent post Breaking the 1 billion recipes per second barrier
The key insight is that after 23 recipes the elves converge into using the same subset of recipes. This subset can be stored compactly in about 20% of the space and read sequentially to allow efficient vector processing.
Tricks used to speed things up:
- Separate writer and reader threads to generate recipes and check them in parallel.
- Vector processing of recipes using techniques similar to SIMD.
Constantsยง
- PREFIX ๐Pre-calculate the first 23 recipes.
Functionsยง
- from_
be_ ๐bytes Convert 8 bytes in big endian order into ausize
. - lsb ๐Convenience function that returns least significant byte.
- prefix_
sum ๐Compute the prefix sum of each byte within ausize
. Leta..h
denote the bytes from most significant to least significant andฮฃx..y
denote the sum fromx
toy
inclusive. - reader ๐Receives batches of recipes from the writer thread, then scans them byte by byte searching for the part two pattern. For simplicity the pattern is always assumed to by six digits.
- unpack ๐Takes two groups of 8 digits each packed into a
usize
as input, then returns the output digits and their respective locations. Ones from sums greater than ten are implicit and not included since recipes has already been pre-filled with ones. - writer ๐Generates recipes then sends them to the reader thread for checking in batches. Processing is broken into alternating โcoldโ and โhotโ loops. An outer enclosing loop checks periodically for the done signal from the reader thread.
Type Aliasesยง
- Input ๐