2020 day 5 and 6
This commit is contained in:
parent
70f1c47d12
commit
f3e12ea387
6 changed files with 2215 additions and 0 deletions
39
2020/05/part1.js
Normal file
39
2020/05/part1.js
Normal file
|
@ -0,0 +1,39 @@
|
|||
const fs = require('fs');
|
||||
|
||||
const data = fs.readFileSync('input.txt', 'utf-8');
|
||||
const lines = data.split('\n');
|
||||
|
||||
let max = 0
|
||||
// who really needs semicolons anyway
|
||||
for (let i = 0; i < lines.length; i++) {
|
||||
let line = lines[i]
|
||||
let rowCode = line.substring(0, 7).replace(/F/g, "0").replace(/B/g, "1")
|
||||
let colCode = line.substring(7).replace(/L/g, "0").replace(/R/g, "1")
|
||||
|
||||
// another excuse to flex that recursive muscle
|
||||
function getIndexFromBSP (BSP, min, max) {
|
||||
if (BSP.length != 1) {
|
||||
[min, max] = getIndexFromBSP (BSP.substring(0, BSP.length-1), min, max)
|
||||
}
|
||||
|
||||
switch (BSP[BSP.length-1]) {
|
||||
case '0':
|
||||
max = Math.floor((min + max) / 2)
|
||||
break
|
||||
case '1':
|
||||
min = Math.ceil((min + max) / 2)
|
||||
break
|
||||
default:
|
||||
console.log("something went wrong")
|
||||
}
|
||||
|
||||
return [min, max]
|
||||
}
|
||||
let row = getIndexFromBSP(rowCode, 0, 127)[0]
|
||||
let col = getIndexFromBSP(colCode, 0, 7)[0]
|
||||
|
||||
let seatID = (row * 8) + col
|
||||
|
||||
max = seatID > max ? seatID : max
|
||||
}
|
||||
console.log(max)
|
39
2020/05/part2.js
Normal file
39
2020/05/part2.js
Normal file
|
@ -0,0 +1,39 @@
|
|||
const fs = require('fs');
|
||||
|
||||
const data = fs.readFileSync('input.txt', 'utf-8');
|
||||
const lines = data.split('\n');
|
||||
|
||||
let seats = []
|
||||
// who really needs semicolons anyway
|
||||
for (let i = 0; i < lines.length; i++) {
|
||||
let line = lines[i]
|
||||
let rowCode = line.substring(0, 7).replace(/F/g, "0").replace(/B/g, "1")
|
||||
let colCode = line.substring(7).replace(/L/g, "0").replace(/R/g, "1")
|
||||
|
||||
// another excuse to flex that recursive muscle
|
||||
function getIndexFromBSP (BSP, min, max) {
|
||||
if (BSP.length != 1) {
|
||||
[min, max] = getIndexFromBSP (BSP.substring(0, BSP.length-1), min, max)
|
||||
}
|
||||
|
||||
switch (BSP[BSP.length-1]) {
|
||||
case '0':
|
||||
max = Math.floor((min + max) / 2)
|
||||
break
|
||||
case '1':
|
||||
min = Math.ceil((min + max) / 2)
|
||||
break
|
||||
default:
|
||||
console.log("something went wrong")
|
||||
}
|
||||
|
||||
return [min, max]
|
||||
}
|
||||
let row = getIndexFromBSP(rowCode, 0, 127)[0]
|
||||
let col = getIndexFromBSP(colCode, 0, 7)[0]
|
||||
|
||||
seats [row] = seats[row] || []
|
||||
seats [row] [col] = (row*8)+col
|
||||
}
|
||||
// i just scrolled thru and found the one empty seat
|
||||
console.log(seats.filter(row => row.length>7))
|
Loading…
Add table
Add a link
Reference in a new issue