Published
Edited
Dec 6, 2021
Insert cell
# Advent of code - Day 6
Insert cell
test = "3,4,3,1,2".split(",").map(x => parseInt(x))
Insert cell
function generation_loop(input, cycle){
let curr = Array.from(Array(10)).map((v,i) => input.filter(x => x == i).length)

Array.from(Array(cycle)).forEach((v,i,a) => {
curr[7] += curr[0]
curr[9] = curr[0]
curr.shift()
})

return curr
}
Insert cell
//generation_loop(test, 3)
Insert cell
generation_loop(test, 17)
Insert cell
d3.sum(generation_loop(test, 18))
Insert cell
d3.sum(generation_loop(test, 80))
Insert cell
d3.sum(generation_loop(input, 18))
Insert cell
d3.sum(generation_loop(input, 256))
Insert cell
d3.sum(generation_loop(input, 1000))
Insert cell
d3.sum(generation_loop(input, 10000))
Insert cell
test
Insert cell
input = `
1,3,4,1,5,2,1,1,1,1,5,1,5,1,1,1,1,3,1,1,1,1,1,1,1,2,1,5,1,1,1,1,1,4,4,1,1,4,1,1,2,3,1,5,1,4,1,2,4,1,1,1,1,1,1,1,1,2,5,3,3,5,1,1,1,1,4,1,1,3,1,1,1,2,3,4,1,1,5,1,1,1,1,1,2,1,3,1,3,1,2,5,1,1,1,1,5,1,5,5,1,1,1,1,3,4,4,4,1,5,1,1,4,4,1,1,1,1,3,1,1,1,1,1,1,3,2,1,4,1,1,4,1,5,5,1,2,2,1,5,4,2,1,1,5,1,5,1,3,1,1,1,1,1,4,1,2,1,1,5,1,1,4,1,4,5,3,5,5,1,2,1,1,1,1,1,3,5,1,2,1,2,1,3,1,1,1,1,1,4,5,4,1,3,3,1,1,1,1,1,1,1,1,1,5,1,1,1,5,1,1,4,1,5,2,4,1,1,1,2,1,1,4,4,1,2,1,1,1,1,5,3,1,1,1,1,4,1,4,1,1,1,1,1,1,3,1,1,2,1,1,1,1,1,2,1,1,1,1,1,1,1,2,1,1,1,1,1,1,4,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,1,1,2,5,1,2,1,1,1,1,1,1,1,1,1
`.split(",").map(x => parseInt(x))
Insert cell
Plot.plot({
marks: [
Plot.line(Array.from(Array(50)).map((v,i) => [i, d3.sum(generation_loop(input,i))]), {
x: d => d[0],
y: d => d[1]
})
]
})
Insert cell
generation_loop(input,0)
Insert cell
generation_loop(input,1)
Insert cell
generation_loop(input,3)
Insert cell
generation_loop(input,4)
Insert cell
generation_loop(input,5)
Insert cell
generation_loop(test, 9)
Insert cell
generation_loop(test, 15)
Insert cell
generation_loop(test, 18)
Insert cell
d3.sum(generation_loop(test, 8))
Insert cell
d3.sum(generation_loop(test, 16))
Insert cell
d3.sum(generation_loop(test, 18))
Insert cell
d3.sum(generation_loop(test, 24))
Insert cell
d3.sum(generation_loop(test, 64))
Insert cell
function generation_generalization(input, cycle){
let curr = Array.from(Array(9)).map((v,i) => input.filter(x => x == i).length)
//let curr = generation_loop(input, 2)
let n = (b) => Math.pow(2, Math.floor(b/8))
return n(cycle) * d3.sum(curr) //+ d3.sum(curr.filter((x,i) => i < mod(cycle,7)).map(a => a))
//return d3.map()
}
Insert cell
generation_loop(test, 1)
Insert cell
generation_loop(test, 2)
Insert cell
generation_loop(test, 3)
Insert cell
d3.sum(generation_loop(test, 7))
Insert cell
generation_generalization(test, 7)
Insert cell
generation_generalization(test, 8)
Insert cell
generation_generalization(test, 16)
Insert cell
generation_generalization(test, 18)
Insert cell
generation_generalization(test, 24)
Insert cell
generation_generalization(test, 64)
Insert cell
mod = (x,n) => ((x % n ) + n ) % n
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more