Skip to content

Instantly share code, notes, and snippets.

@Friss
Created December 7, 2020 06:10
Show Gist options
  • Save Friss/c0e7496f1daa1fe10938f06e0068e386 to your computer and use it in GitHub Desktop.
Save Friss/c0e7496f1daa1fe10938f06e0068e386 to your computer and use it in GitHub Desktop.
const fs = require('fs').promises;
(async () => {
console.log(`Reading input from ${__dirname}/${process.argv[2]}.txt`);
const inputData = await fs.readFile(`${__dirname}/${process.argv[2]}.txt`);
const inputs = inputData
.toString()
.split('\n')
.filter((n) => n);
const seats = [];
inputs.forEach((seat) => {
const range = {
minRow: 0,
maxRow: 127,
minCol: 0,
maxCol: 7,
id: 1,
};
const steps = seat.split('');
// F, L means to take the lower half
// B, R means to take the upper half,
steps.forEach((l) => {
if (l === 'F') {
range.maxRow = Math.floor(
range.maxRow - (range.maxRow - range.minRow + 1) / 2
);
} else if (l === 'B') {
range.minRow = Math.floor(
range.minRow + (range.maxRow - range.minRow + 1) / 2
);
} else if (l === 'R') {
range.minCol = Math.floor(
range.minCol + (range.maxCol - range.minCol + 1) / 2
);
} else {
range.maxCol = Math.floor(
range.maxCol - (range.maxCol - range.minCol + 1) / 2
);
}
});
range.id = 8 * range.minRow + range.maxCol;
// Parse row/col with binary
// parseInt(seat.substr(0, 7).replaceAll('F', '0').replaceAll('B', '1'), 2);
// parseInt(seat.substr(7, 10).replaceAll('R', '1').replaceAll('L', '0'), 2);
seats.push(range);
});
console.log(
'max seat id',
seats.reduce((max, { id }) => {
return max < id ? id : max;
}, -Infinity)
);
let lastId = 99;
seats
.map(({ id }) => id)
.sort((a, b) => a - b)
.forEach((id) => {
if (id - lastId === 2) {
console.log('your seat', lastId + 1);
}
lastId = id;
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment