Advent of Code 2018 - clj's Nim Solutions

Day 17: Reservoir Research

For Day 17 we get an underground scan and are required to map the flow of water through sand and clay. The clay blocks and collects the water… as the animation below illustrates.

Puzzle Input

The puzzle input is rendered by looping over the relevant portion of the ASCII based “map” and rendering it into an array of pixels which are saved as a PNG using the stb_image_write library. FFmpeg is then run over the generated PNGs to produce the h264 video. Wrapping a video encoding library seemed like too much work!

Generating nice looking output was the challenge here, not difficult, but still elusive. The original algorithm used to solve the puzzle filled as much vertically or horizontally as possible, but for the animation I wanted one movement in the same direction of flow per frame. The easiest way to do that turned out to be to disqualify newly generated tiles when testing if a tile should change state.

Test Inputs

Below is the test input from the problem description as well as some test inputs I generated to fix various problems with the rendering algorithm, all rendered in glorious ASCII. As evidenced by test_input_0A, not every case is exactly covered, but that isn’t one that comes up in the puzzle input, so ¯\(ツ)

  • test_input_01
  • test_input_0A
  • test_input_0B
  • test_input_0C
  • test_input_0D
  • test_input_0E