diff options
Diffstat (limited to 'day23')
-rw-r--r-- | day23/data.js | 3 | ||||
-rw-r--r-- | day23/day23-1.js | 33 | ||||
-rw-r--r-- | day23/day23-2.js | 57 |
3 files changed, 93 insertions, 0 deletions
diff --git a/day23/data.js b/day23/data.js new file mode 100644 index 0000000..152cb7e --- /dev/null +++ b/day23/data.js @@ -0,0 +1,3 @@ +const data = '123487596' + +module.exports = data diff --git a/day23/day23-1.js b/day23/day23-1.js new file mode 100644 index 0000000..8427cb4 --- /dev/null +++ b/day23/day23-1.js @@ -0,0 +1,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('')) diff --git a/day23/day23-2.js b/day23/day23-2.js new file mode 100644 index 0000000..536e3c3 --- /dev/null +++ b/day23/day23-2.js @@ -0,0 +1,57 @@ +const data = require('./data') +const numbers = data.split('').map((n) => parseInt(n, 10)) +const min = Math.min(...numbers) +const max = Math.max(...numbers) +const start = numbers[0] +const cupsLength = 1000000 +const cups = {} +let current = numbers[0] + +numbers.forEach((n, i) => { + cups[n] = { + prev: numbers[(i + (numbers.length - 1)) % numbers.length], + next: numbers[(i + 1) % numbers.length], + } +}) + +for (let i = max + 1; i <= cupsLength; i++) { + cups[i] = { + prev: i - 1, + next: i + 1, + } +} + +cups[start].prev = cupsLength +cups[numbers[numbers.length - 1]].next = max + 1 +cups[max + 1].prev = numbers[numbers.length - 1] +cups[cupsLength].next = start + +for (let i = 0; i < 10000000; i++) { + let destination = current - 1 + const first = cups[current].next + const second = cups[first].next + const third = cups[second].next + const fourth = cups[third].next + cups[current].next = fourth + + while ( + destination < min + || destination === first + || destination === second + || destination === third + ) { + destination-- + if (destination < min) { destination = cupsLength } + } + + const afterDestination = cups[destination].next + cups[destination].next = first + cups[third].next = afterDestination + current = cups[current].next +} + +const res1 = cups[1].next +const res2 = cups[res1].next + +console.log('Answer: ', res1 * res2) + |