Created
January 3, 2018 03:29
-
-
Save devNoiseConsulting/8d4c74d8beed559638a2924d170d4847 to your computer and use it in GitHub Desktop.
Fractal Art - Advent of Code - 20171221
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let gridRules = require('./gridRules'); | |
| let transposeArray = function(array) { | |
| return array[0].map((col, i) => array.map(row => row[i])); | |
| }; | |
| let permuations = function(square) { | |
| let perms = []; | |
| perms.push(square.slice()); | |
| perms.push(transposeArray(square.slice())); | |
| square = square.slice().map(v => v.slice().reverse()); | |
| perms.push(square.slice()); | |
| perms.push(transposeArray(square.slice())); | |
| square = square.reverse(); | |
| perms.push(square.slice()); | |
| perms.push(transposeArray(square.slice())); | |
| square = square.slice().map(v => v.slice().reverse()); | |
| perms.push(square.slice()); | |
| perms.push(transposeArray(square.slice())); | |
| perms = perms | |
| .map(p => p.map(l => l.join('')).join('/')) | |
| .filter((v, i, arr) => i == arr.indexOf(v)); | |
| //perms.sort(); | |
| return perms; | |
| }; | |
| let getSubSquare = function(square, x, y, chunk) { | |
| x *= chunk; | |
| y *= chunk; | |
| return square.slice(x, x + chunk).map(v => v.slice(y, y + chunk)); | |
| }; | |
| let getSubSquares = function(square, chunk) { | |
| let size = square.length / chunk; | |
| let subSquares = new Array(size).fill(0).map(v => new Array(size).slice()); | |
| for (let i = 0; i < size; i++) { | |
| for (let j = 0; j < size; j++) { | |
| subSquares[i][j] = getSubSquare(square, i, j, chunk); | |
| } | |
| } | |
| return subSquares; | |
| }; | |
| let mergeSubSquares = function(subSquares, chunk) { | |
| let size = subSquares.length; | |
| let newSquare = new Array(size * ++chunk); | |
| subSquares.forEach((row, x) => { | |
| row.forEach((cell, y) => { | |
| let newRow = x * chunk; | |
| cell.forEach((section, i) => { | |
| if (newSquare[newRow] == undefined) { | |
| newSquare[newRow] = new Array(); | |
| } | |
| newSquare[newRow] = newSquare[newRow].concat(section); | |
| newRow++; | |
| }); | |
| }); | |
| }); | |
| return newSquare; | |
| }; | |
| let enhancement = function(gridRules, iterations, square) { | |
| for (let i = 0; i < iterations; i++) { | |
| let chunk = square.length % 2 == 0 ? 2 : 3; | |
| let subSquares = getSubSquares(square, chunk).map(row => | |
| row.map(s => | |
| permuations(s).reduce((acc, v, i) => { | |
| if (gridRules.hasOwnProperty(v)) { | |
| acc = gridRules[v]; | |
| } | |
| return acc; | |
| }, s) | |
| ) | |
| ); | |
| square = mergeSubSquares(subSquares, chunk); | |
| } | |
| return square; | |
| }; | |
| let countLights = function(square) { | |
| return square.reduce((acc, v, i) => { | |
| acc += v.filter(p => p == '#').length; | |
| return acc; | |
| }, 0); | |
| }; | |
| let test, iterations, result; | |
| test = '.#./..#/###'.split('/').map(v => v.split('')); | |
| iterations = 5; | |
| result = enhancement(gridRules, iterations, test); | |
| result = countLights(result); | |
| console.log(result); | |
| iterations = 18; | |
| result = enhancement(gridRules, iterations, test); | |
| result = countLights(result); | |
| console.log(result); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment