javascript adventofcode.com/2017/day/6

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript adventofcode.com/2017/day/6相关的知识,希望对你有一定的参考价值。

// noprotect

const rows = data().split('\t').map(i => +i)
const regCount = rows.length

const mem = []
let count = 0

while(mem.indexOf(rows.join()) === -1) {
  mem.push(rows.join())
  count += 1
  
  let maxRegCount = Math.max(...rows)
  let regIndex = rows.findIndex(r => r === maxRegCount)
  
  rows[regIndex] = 0
  
  while (maxRegCount > 0) {
    regIndex = (++regIndex) % regCount
    rows[regIndex] += 1
    maxRegCount -= 1
  }
}

console.log('count:', count)

const firstIndex = mem.indexOf(rows.join())
console.log('cycles:', mem.length - firstIndex)

function data() {
  return (`4	10	4	1	8	4	9	14	5	1	14	15	0	15	3	5`)
}

以上是关于javascript adventofcode.com/2017/day/6的主要内容,如果未能解决你的问题,请参考以下文章

javascript JavaScript isset()等效: - JavaScript

JavaScript 使用JavaScript更改CSS(JavaScript)

JavaScript之基础-1 JavaScript(概述基础语法)

前端基础-JavaScript的基本概述和语法

JavaScript

JavaScript