summaryrefslogtreecommitdiffstats
path: root/day23/day23-1.js
blob: 8427cb4654054983d10f684dbd53667462b7eeb5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
const data = require('./data')
let cups = data.split('').map(n => parseInt(n))
let current = cups[0]

const move = () => {
  const i = cups.indexOf(current)
  const picked = cups.slice(i+1,i+4)
  const rest = [...cups.slice(0, i+1), ...cups.slice(i+4)]
  const dest = (nr) => {
    if (picked.includes(nr)) {
      return dest(nr-1)
    } else if (nr < Math.min(...rest)) {
      return dest(Math.max(...cups))
    }
    return nr
  }

  rest.splice(rest.indexOf(dest(current-1)) + 1, 0, ...picked)
  const rest1 = rest.slice(0, rest.indexOf(current))
  const rest2 = rest.slice(rest.indexOf(current))
  cups = [...rest2, ...rest1]
  current = cups[cups.indexOf(current)+1]
}

for(let j=1; j<=100; j++) {
  move()
}

const cups1 = cups.slice(0, cups.indexOf(1))
const cups2 = cups.slice(cups.indexOf(1))
cups = [...cups2, ...cups1]

console.log('Answer: ', cups.slice(1).join(''))