Skip to content

Instantly share code, notes, and snippets.

@kimagure44
Created December 5, 2020 07:35
Show Gist options
  • Save kimagure44/2a55799b86147f62ef9c2f60c8751f6e to your computer and use it in GitHub Desktop.
Save kimagure44/2a55799b86147f62ef9c2f60c8751f6e to your computer and use it in GitHub Desktop.
Advent of code (2020) - CODE 5 - 2
(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();
const orderSeats = result.sort((a, b) => a > b ? -1 : 1);
let seatOK = 0;
orderSeats.forEach((seat, index, arr) => {
if (arr[index] && arr[index + 1] > 0) {
const check = seat - 1 === arr[index + 1];
if (!check) {
seatOK = seat - 1;
console.log(`Asiento: ${seat} / Siguiente asiento: ${arr[index + 1]} = ${seat - 1} === ${arr[index + 1]} => ${seat - 1 === arr[index + 1]}`);
}
}
});
console.log(`Asiento: ${seatOK} / 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