day21: Day 21: Fractal Art

Description Usage Arguments Details

Description

Fractal Art

Usage

1
2
3
4
5
6
7
matrix_to_string(m)

string_to_matrix(string)

expand_rule(rule_string)

enhance_matrix(m, rules)

Arguments

m

a matrix rules <- c( "../.# => ##./#../...", ".#./..#/### => #..#/..../..../#..#")

rules <- rules lapply(expand_rule) unlist()

seed <- ".#./..#/###" string_to_matrix()

step1 <- enhance_matrix(seed, rules)

matrix_to_string(step1)

step2 <- enhance_matrix(step1, rules) matrix_to_string(step2)

string

a string description of a matrix

rule_string

a string describing a rule

rules

rules for progressive enhancement

Details

Part One

You find a program trying to generate some art. It uses a strange process that involves repeatedly enhancing the detail of an image through a set of rules.

The image consists of a two-dimensional square grid of pixels that are either on (#) or off (.). The program always begins with this pattern:

1
2
3
.#.
..#
###

Because the pattern is both 3 pixels wide and 3 pixels tall, it is said to have a size of 3.

Then, the program repeats the following process:

Because each square of pixels is replaced by a larger one, the image gains pixels and so its size increases.

The artist's book of enhancement rules is nearby (your puzzle input); however, it seems to be missing rules. The artist explains that sometimes, one must rotate or flip the input pattern to find a match. (Never rotate or flip the output pattern, though.) Each pattern is written concisely: rows are listed as single units, ordered top-down, and separated by slashes. For example, the following rules correspond to the adjacent patterns:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
../.#  =  ..
          .#

                .#.
.#./..#/###  =  ..#
                ###

                        #..#
#..#/..../#..#/.##.  =  ....
                        #..#
                        .##.

When searching for a rule to use, rotate and flip the pattern as necessary. For example, all of the following patterns match the same rule:

1
2
3
.#.   .#.   #..   ###
..#   #..   #.#   ..#
###   ###   ##.   .#.

Suppose the book contained the following two rules:

1
2
../.# => ##./#../...
.#./..#/### => #..#/..../..../#..#

As before, the program begins with this pattern:

1
2
3
.#.
..#
###

The size of the grid (3) is not divisible by 2, but it is divisible by 3. It divides evenly into a single square; the square matches the second rule, which produces:

1
2
3
4
#..#
....
....
#..#

The size of this enhanced grid (4) is evenly divisible by 2, so that rule is used. It divides evenly into four squares:

1
2
3
4
5
#.|.#
..|..
--+--
..|..
#.|.#

Each of these squares matches the same rule (../.# =&gt; ##./#../...), three of which require some flipping and rotation to line up with the rule. The output for the rule is the same in all four cases:

1
2
3
4
5
6
7
##.|##.
#..|#..
...|...
---+---
##.|##.
#..|#..
...|...

Finally, the squares are joined into a new grid:

1
2
3
4
5
6
##.##.
#..#..
......
##.##.
#..#..
......

Thus, after 2 iterations, the grid contains 12 pixels that are on.

How many pixels stay on after 5 iterations?

Part Two

How many pixels stay on after 18 iterations?


tjmahr/adventofcode17 documentation built on May 30, 2019, 2:29 p.m.