sln_2017_21
Copyright(c) Eric Mertens 2017
LicenseISC
Maintaineremertens@gmail.com
Safe HaskellNone
LanguageHaskell2010

Main

Description

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)
##.##.
#..#..
......
##.##.
#..#..
......
Synopsis

Documentation

>>> let printGrid = mapM_ putStrLn

main :: IO () Source #

Print the number of active grid cells after 5 and 18 iterations. The input file can be overridden via command-line arguments.

type Grid = [[Char]] Source #

start :: Grid Source #

Initial grid value (a game of life glider).

>>> printGrid start
.#.
..#
###

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)
.##
#.#
..#

mapSubSquares :: (Grid -> Grid) -> Grid -> Grid Source #

Apply a function to all of the subsquares of a grid.

makeRules :: [(Grid, Grid)] -> Grid -> Grid Source #

Build the grid update function given the list of rules loaded from the input file.