Notebooks 2.0 is here.

Published
Edited
Dec 14, 2018
Insert cell
Insert cell
part2 = {
const after = '890691'
const recipes = [3, 7]
const elves = [0, 1]
while (true) {
// for (let n = 0; n < 3000; n++) {
recipes.push(...String(recipes[elves[0]] + recipes[elves[1]]).split('').map(Number))

for (let i = 0; i < elves.length; i++)
elves[i] = (elves[i] + recipes[elves[i]] + 1) % recipes.length
let found = recipes.slice(recipes.length - (after.length*2)).join('').indexOf(after)
if (recipes.length % 100000 === 0)
yield recipes.length
if (found !== -1) {
yield recipes.length - (after.length*2) + found
return
}
}
}
Insert cell
Insert cell
// part2 = {
// const head = { score: 3, i: 1 }
// let tail = { score: 7, i: 2 }
// head.next = tail

// const search = '890691'.split('').map(Number)
// let found = 0, toSearch = head, lastSearch = null
// let maxFound = 0
// const elves = [head, tail]
// let recipes = 2
// while (found < search.length) {
// // for (let n = 0; n < 20; n++) {
// // new recipes
// const newRecipes = String(elves[0].score + elves[1].score).split('').map(Number)
// for (const score of newRecipes) {
// tail = tail.next = { score, i: tail.i + 1 }
// if (!toSearch)
// toSearch = tail
// }
// recipes += newRecipes.length
// // check search
// while (toSearch && found < search.length) {
// console.log(toSearch, found)
// if (toSearch.score === search[found])
// found++
// else
// found = toSearch.score === search[0] ? 1 : 0
// if (found > maxFound)
// maxFound = found
// lastSearch = toSearch
// toSearch = toSearch.next
// }

// if (found === search.length) {
// yield lastSearch.i - search.length
// return
// }
// for (let i = 0; i < elves.length; i++) {
// const steps = 1 + elves[i].score
// for (let j = 0; j < steps; j++) {
// elves[i] = elves[i].next
// if (!elves[i])
// elves[i] = head
// }
// }

// if (recipes % 100000 == 0)
// yield { recipes, maxFound }
// }
// return { recipes, elves, head, toSearch, found, lastSearch }
// }
Insert cell
part1 = {
const after = 890691, length = 10
const recipes = [3, 7]
const elves = [0, 1]
while (recipes.length < after + length) {
recipes.push(...String(recipes[elves[0]] + recipes[elves[1]]).split('').map(Number))

for (let i = 0; i < elves.length; i++)
elves[i] = (elves[i] + recipes[elves[i]] + 1) % recipes.length
}
return recipes.slice(after, after + length).join('')
}
Insert cell
initial = lines[0].split('').map(Number)
Insert cell
lines = (debug ? test : input).split('\n').filter(_.identity)
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
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