Skip to content
Snippets Groups Projects
Commit 97b24e49 authored by Mark Jeeninga's avatar Mark Jeeninga
Browse files

Day 11 Mark

parent 2fd7fc33
No related branches found
No related tags found
No related merge requests found
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)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment