Skip to content

Instantly share code, notes, and snippets.

@tom-wagner
Last active February 15, 2019 20:32
Show Gist options
  • Save tom-wagner/1f6d2d8831eed44ede84633ff6cc3b5a to your computer and use it in GitHub Desktop.
Save tom-wagner/1f6d2d8831eed44ede84633ff6cc3b5a to your computer and use it in GitHub Desktop.
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
@tom-wagner
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment