Copyright | (c) Eric Mertens 2020 |
---|---|
License | ISC |
Maintainer | emertens@gmail.com |
Safe Haskell | None |
Language | Haskell2010 |
https://adventofcode.com/2020/day/17
This automata implementation uses a few optimizations:
It precomputes the neighborhood map that can be quickly translated to any position without recomputing the map. Unioning one of these for each live cell gives us a Map where each element tells us how many neighbors that cell has. Taking the union of these maps is faster than inserting individual neighbor elements.
This solution only needs to consider elements that have any neighbors at all, and these values are determined by only considering the live elements from the previous generation.
The solution avoids checking if a particular cell in the
previous generation was alive or not unless it's neighbors
is known to be 2
, as this is the only time it matters.
This solution works both with a flexible, n-dimensional list coordinate representation and also with more efficient unpacked tuples of integers. The list version is about 2x slower than the unpacked tuples.
On my MacBook Pro, part 2 of this problem runs in 50ms.