Copyright | (c) Eric Mertens 2017 |
---|---|
License | ISC |
Maintainer | emertens@gmail.com |
Safe Haskell | None |
Language | Haskell2010 |
http://adventofcode.com/2017/day/21
Day 21 defines a system of rewrite rules on a grid of points that are applied to 2x2 or 3x3 subtiles of the whole grid.
>>>
:set -XQuasiQuotes
>>>
let inputFile = "../.# => ##./#../...\n.#./..#/### => #..#/..../..../#..#\n"
>>>
let rules = makeRules ([format|- ((.|#)+!&/ => (.|#)+!&/%n)*|] inputFile)
>>>
let iterations = iterate (mapSubSquares rules) start
>>>
printGrid (iterations !! 0)
.#. ..# ###
>>>
printGrid (iterations !! 1)
#..# .... .... #..#
>>>
printGrid (iterations !! 2)
##.##. #..#.. ...... ##.##. #..#.. ......
Documentation
>>>
let printGrid = mapM_ putStrLn
Print the number of active grid cells after 5 and 18 iterations. The input file can be overridden via command-line arguments.
countCells :: Grid -> Int Source #
Count the number of cells set in a grid.
>>>
countCells start
5
similarSquares :: Grid -> [Grid] Source #
Generate all of the rotated and flipped versions of a grid.
>>>
printGrid (Data.List.intercalate " " <$> transpose (similarSquares start))
.#. .## ### #.. ### ##. .#. ..# ..# #.# #.. #.# ..# #.# #.. #.# ### ..# .#. ##. .#. #.. ### .##
rotateCCW :: Grid -> Grid Source #
Rotate a grid counter-clockwise.
>>>
printGrid (rotateCCW start)
.## #.# ..#