Day 5
This commit is contained in:
64
05/part2.js
Normal file
64
05/part2.js
Normal file
@@ -0,0 +1,64 @@
|
||||
const fs = require('fs')
|
||||
const readline = require('readline');
|
||||
|
||||
const readInterface = readline.createInterface({
|
||||
input: fs.createReadStream('input'),
|
||||
console: false
|
||||
});
|
||||
|
||||
// [[x,y], [x,y], ...]
|
||||
let seats = [];
|
||||
|
||||
function makeArray(n) {
|
||||
let ary = new Array(n);
|
||||
for(i=0; i<n; i++) {
|
||||
ary[i] = i;
|
||||
}
|
||||
|
||||
return ary;
|
||||
}
|
||||
|
||||
readInterface.on('line', function(line) {
|
||||
const rowSpec = line.substring(0, 7);
|
||||
const colSpec = line.substring(7, 10);
|
||||
|
||||
let row = rowSpec.split('').reduce(function(range, char) {
|
||||
const half = Math.floor(range.length / 2);
|
||||
|
||||
let r2 = range;
|
||||
switch(char) {
|
||||
case 'F': r2 = range.slice(0, half+1); break;
|
||||
case 'B': r2 = range.slice(half, range.length); break;
|
||||
};
|
||||
|
||||
return r2;
|
||||
}, makeArray(128));
|
||||
|
||||
let col = colSpec.split('').reduce(function(range, char) {
|
||||
const half = Math.floor(range.length / 2);
|
||||
|
||||
let r2 = range;
|
||||
switch(char) {
|
||||
case 'L': r2 = range.slice(0, half+1); break;
|
||||
case 'R': r2 = range.slice(half,range.length); break;
|
||||
};
|
||||
|
||||
return r2;
|
||||
}, makeArray(8));
|
||||
|
||||
seats.push([col[0], row[0]]);
|
||||
});
|
||||
|
||||
readInterface.on('close', function(line) {
|
||||
let seatIDs = new Set( seats.map((seat) => seat[1]*8 + seat[0]).sort((a,b) => a-b ) );
|
||||
|
||||
for (x=0; x<8; x++) {
|
||||
for(y=0; y<128; y++) {
|
||||
let seatID = y*8+x;
|
||||
if (!seatIDs.has(seatID) && seatIDs.has(seatID-1) && seatIDs.has(seatID+1)) {
|
||||
console.log(seatID);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
Reference in New Issue
Block a user