aoc::year2018

Module day14

Source
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 a usize.
  • lsb ๐Ÿ”’
    Convenience function that returns least significant byte.
  • prefix_sum ๐Ÿ”’
    Compute the prefix sum of each byte within a usize. Let a..h denote the bytes from most significant to least significant and ฮฃx..y denote the sum from x to y 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ยง