Last active
February 15, 2019 20:32
-
-
Save tom-wagner/1f6d2d8831eed44ede84633ff6cc3b5a to your computer and use it in GitHub Desktop.
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
| const generateNextIteration = (arr, patterns) => { | |
| return arr.map((val, idx) => { | |
| const currentPattern = arr[idx - 2] + arr[idx - 1] + arr[idx] + arr[idx + 1] + arr[idx + 2]; | |
| return patterns.hasOwnProperty(currentPattern) ? patterns[currentPattern] : '.'; | |
| }); | |
| }; | |
| const pad50 = () => new Array(50).fill('.'); | |
| const addPadding = (arr) => [...pad50(), ...arr, ...pad50()]; | |
| const pottedPlants = (initialState, patterns, iterations = 20) => { | |
| const initialArr = initialState.split(''); | |
| let arr = addPadding(initialArr); | |
| let i = 0; | |
| while (i < iterations) { | |
| arr = generateNextIteration(arr, patterns); | |
| i++; | |
| } | |
| // subtracting 50 because I added 50 dots to the left of the original first index | |
| return arr.reduce((acc, cv, idx) => cv === '#' ? acc + idx - 50 : acc, 0); | |
| }; | |
| const realPatterns = { | |
| '.###.': '#', | |
| '###.#': '#', | |
| '#....': '.', | |
| '..#..': '.', | |
| '##.#.': '.', | |
| '...#.': '.', | |
| '.#...': '#', | |
| '.##..': '.', | |
| '..#.#': '.', | |
| '#..#.': '.', | |
| '....#': '.', | |
| '##..#': '#', | |
| '..##.': '#', | |
| '.##.#': '#', | |
| '.#.#.': '.', | |
| '.....': '.', | |
| '#####': '.', | |
| '.####': '#', | |
| '###..': '.', | |
| '.#..#': '#', | |
| '#.#.#': '#', | |
| '#..##': '#', | |
| '#...#': '#', | |
| '.#.##': '#', | |
| '##.##': '.', | |
| '..###': '.', | |
| '#.###': '.', | |
| '####.': '#', | |
| '#.##.': '#', | |
| '##...': '#', | |
| '#.#..': '.', | |
| '...##': '#', | |
| }; | |
| const realInitialState = '##...#...###.#.#..#...##.###..###....#.#.###.#..#....#..#......##..###.##..#.##..##..#..#.##.####.##'; | |
| pottedPlants(realInitialState, realPatterns); // 3,405 |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Answer for Part 2: 3,350,000,000,000
Expanded the padding to 5,000 in code above, did 1,000 iterations, recognized the pattern and extrapolated in Excel