diff --git a/day 11/day_11_mark.js b/day 11/day_11_mark.js new file mode 100644 index 0000000000000000000000000000000000000000..0570760814bded2a1f8ad1e9c330d0299241cdd8 --- /dev/null +++ b/day 11/day_11_mark.js @@ -0,0 +1,94 @@ +range = x => [...Array(x).keys()] + +stones = input.split(' ').map(Number) + +blink = () => { + newStones = stones + stones.map((x,i) => { + if(x == 0){ + newStones[i] = 1 + } else { + word = x.toString() + if(word.length % 2 == 0){ + newStones[i] = Number(word.substring(0, word.length/2)) + ' ' + Number(word.substring(word.length/2)) + } else { + newStones[i] = x*2024 + } + } + }) + stones = newStones.join(' ').split(' ') + //console.log(stones.join(' ')) +} + +range(25).map(blink) + +output1 = stones.length + +//// + +evolution = {} +evolution[0] = [1] + +revealEvolution = stone => { + if(!Object.keys(evolution).includes(stone.toString())){ + word = stone.toString() + if(word.length % 2 == 0){ + evolution[stone.toString()] = [Number(word.substring(0, word.length/2)), Number(word.substring(word.length/2))] + } else { + evolution[stone.toString()] = [stone * 2024] + } + } + return evolution[stone.toString()] +} + +evolutionStep = (stone, step) => { + if(step > 0 && !evolution[stone.toString()]){ + revealEvolution(stone).map(x => evolutionStep(x, step-1)) + } +} + +input.split(' ').map(Number).map(stone => evolutionStep(stone, 75)) + +x0 = {} + +input.split(' ').map(Number).map(stone => x0[stone] = 1) + +iteration = (x, step) => { + if(step > 0){ + x1 = {} + Object.keys(x).map(k => revealEvolution(k.toString()).map(y => x1[y] ? x1[y] += x[k] : x1[y] = x[k])) + return iteration(x1, step-1) + } else { + return x + } +} + +x1 = iteration(x0, 75) + +output1 = Object.keys(x1).reduce((t,k) => t + x1[k],0) + + + + + + + + + + + + + + + + + + + + + + + + + +