Add 2020 day 5

This commit is contained in:
Alice Gaudon 2020-12-05 13:39:12 +01:00
parent 91ec1be623
commit c4a4227c47
1 changed files with 24 additions and 0 deletions

24
2020/day5/day5.js Executable file
View File

@ -0,0 +1,24 @@
#!/bin/node
const fs = require('fs');
fs.readFile('input.txt', (err, data) => {
const seats = data.toString()
.split('\n')
.filter(l => l.length > 0)
.map(s => s.replaceAll(/[BR]/g, '1')
.replaceAll(/[FL]/g, '0'))
.map(s => parseInt(s, 2));
console.log('Highest seat:', seats.reduce((a, v) => v > a ? v : a, -1));
seats.sort();
for (let i = 0; i < seats.length; i++) {
const seat = seats[i];
const nextSeat = seats[i + 1];
if (nextSeat - seat === 2) {
console.log('Your seat:', seat + 1);
break;
}
}
});