Created
December 5, 2020 07:11
-
-
Save kimagure44/1299020d76822d56f0cb16e9f214a0b9 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
(async () => { | |
const result = []; | |
const entries = (await(await fetch('https://adventofcode.com/2020/day/5/input')).text()).split('\n').map(item => item); | |
entries.forEach(item => { | |
let rows = { min: 1, max: 128 }; | |
let columns = { min: 1, max: 8 }; | |
let totalRows = 128; | |
let totalColumns = 8; | |
const card = Array.from(item); | |
card.forEach((letter, iCard) => { | |
if (iCard <= 6) { | |
totalRows = totalRows / 2; | |
rows[letter === 'F' ? 'max' : 'min'] = letter === 'F' ? rows.max - totalRows : rows.min + totalRows; | |
} else { | |
totalColumns = totalColumns / 2; | |
columns[letter === 'L' ? 'max' : 'min'] = letter === 'L' ? columns.max - totalColumns : columns.min + totalColumns; | |
} | |
}); | |
/* | |
Restamos 1 para obtener el valor correcto (0 - 127), | |
ya que las operaciones matemáticas se han realizado de 1 a 128 | |
*/ | |
rows.min--; | |
rows.max--; | |
columns.min--; | |
columns.max--; | |
const id = rows.min * 8 + columns.min; | |
console.table([rows, columns, id]); | |
result.push(id); | |
}); | |
const startTime = performance.now(); | |
const endTime = performance.now(); | |
console.log(`ID más alto: ${Math.max.apply(null, result)}`, `${parseFloat(endTime - startTime).toFixed(4)} ms`); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment