Last active
December 5, 2022 15:23
-
-
Save BiosBoy/4c56a32ead6365db1c03a29b5ca5e403 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs'); | |
const path = require('path'); | |
const data = fs.readFileSync(path.join(__dirname, './input4.txt'), 'utf8'); | |
const rules = fs.readFileSync(path.join(__dirname, './input4_1.txt'), 'utf8'); | |
const config = rules.split(/\r?\n/).map(item => item.split(' ')).map(item => ({ | |
from: Number(item[3]), | |
count: Number(item[1]), | |
to: Number(item[5]) | |
})); | |
const createColumns = () => { | |
const columns = []; | |
data.split(/\r?\n/).forEach((item) => { | |
const letters = item.split(''); | |
const lettersInCell = 3; | |
const iterationsCount = Math.floor((item.length + 1) / (lettersInCell + 1)); | |
Array.from(Array(iterationsCount).keys()).forEach((index) => { | |
const currentIndex = index * 4; | |
const firstElem = letters[currentIndex]; | |
const secondElem = letters[currentIndex + 1]; | |
const thirdElem = letters[currentIndex + 2]; | |
const stringToInsert = `${firstElem}${secondElem}${thirdElem}`; | |
if (!columns[index]) { | |
columns[index] = []; | |
} | |
columns[index].push(stringToInsert); | |
}) | |
}); | |
return columns.map(elem => elem.filter(item => item !== ' ')); | |
} | |
const game = (partType) => { | |
const columns = createColumns(); | |
// console.log(createColumns(), 'INPUT'); | |
Object.values(config).forEach(level => { | |
const columnFrom = columns[level.from - 1]; | |
const columnTo = columns[level.to - 1]; | |
if (partType == 'first') { | |
Array.from(Array(level.count).keys()).forEach(() => { | |
columnTo.unshift(columnFrom?.splice(0, 1)[0]); | |
}) | |
} else { | |
Array.from(Array(level.count).keys()).forEach(() => { | |
columns[level.to - 1].unshift(columns[level.from - 1]?.splice(0, 1)[0]); | |
}) | |
columns[level.to - 1] = [...columns[level.to - 1].slice(0, level.count).reverse(), ...columns[level.to - 1].slice(level.count, columns[level.to - 1].length)]; | |
} | |
}); | |
let result = ''; | |
columns.forEach(item => { | |
result += item[0]; | |
}) | |
return result.replace(/\[/gi, '').replace(/\]/gi, ''); | |
} | |
console.log(game('first'), game('second'), 'game'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment