Published
Edited
Dec 6, 2021
Insert cell
Insert cell
schoolOverYear = {
let start = inputCount.slice()
let res = Array(256)
for (let index = 0; index < 256; ++index) {
res[index] = smartPredictDay(start)
start = res[index].slice()
}
return res
}
Insert cell
viewof daysToDraw = Inputs.range([1, 256], {label: "Days to draw", step: 1})
Insert cell
Plot.plot({
marks: [
Plot.lineY(schoolOverYear.filter( (val, idx) => idx < daysToDraw).map( day => day[0]), {stroke: "red"}),
Plot.lineY(schoolOverYear.filter( (val, idx) => idx < daysToDraw).map( day => day[1]), {stroke: "green"}),
Plot.lineY(schoolOverYear.filter( (val, idx) => idx < daysToDraw).map( day => day[2]), {stroke: "blue"}),
Plot.lineY(schoolOverYear.filter( (val, idx) => idx < daysToDraw).map( day => day[3]), {stroke: "lightblue"}),
Plot.lineY(schoolOverYear.filter( (val, idx) => idx < daysToDraw).map( day => day[4]), {stroke: "yellow"}),
Plot.lineY(schoolOverYear.filter( (val, idx) => idx < daysToDraw).map( day => day[5]), {stroke: "magenta"}),
Plot.lineY(schoolOverYear.filter( (val, idx) => idx < daysToDraw).map( day => day[6]), {stroke: "pink"}),
Plot.lineY(schoolOverYear.filter( (val, idx) => idx < daysToDraw).map( day => day[7]), {stroke: "brown"}),
Plot.lineY(schoolOverYear.filter( (val, idx) => idx < daysToDraw).map( day => day[8]), {stroke: "black"})
]
})
Insert cell
Insert cell
{
let data = input
for (let i = 0; i < 80; i++)
data = predictDay(data)
return data
}
Insert cell
function predictDay(school) {
let spawnNbr = 0
school = school.map( fish => {
if (fish > 0)
return fish - 1
else {
spawnNbr++
return 6
}
})
let newSpawns = Array(spawnNbr).fill(8)
school.push(...newSpawns)
return school
}
Insert cell
Insert cell
schoolAfterOneYear = {
let data = inputCount.slice()
for (let i = 0; i < 256; i++)
data = smartPredictDay(data)
return data
}
Insert cell
schoolAfterOneYear.reduce((accumulator, currentValue, index, array) => {
return accumulator + currentValue
})
Insert cell
function smartPredictDay( school ) {
// Deal with large inputs by counting the number of fish for each "age"
let spawners = school.shift()
school.push(spawners)
school[6] += spawners
return school
}
Insert cell
Insert cell
inputCount = input.reduce((accumulator, currentValue) => {
accumulator[currentValue]++
return accumulator
}, Array(9).fill(0))
Insert cell
input = FileAttachment("06_input.txt").text().then( txt => txt.trim().split(",").map(parseFloat))
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