2020 day 5 and 6
This commit is contained in:
parent
70f1c47d12
commit
f3e12ea387
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))
|
2093
2020/06/input.txt
Normal file
2093
2020/06/input.txt
Normal file
File diff suppressed because it is too large
Load diff
21
2020/06/part1.js
Normal file
21
2020/06/part1.js
Normal file
|
@ -0,0 +1,21 @@
|
|||
const fs = require('fs');
|
||||
|
||||
const data = fs.readFileSync('test.txt', 'utf-8');
|
||||
const groups = data.split('\n\n');
|
||||
|
||||
let sum = 0
|
||||
for (let g = 0; g<groups.length; g++){
|
||||
let group = groups[g]
|
||||
// ignore spaces and new lines; we don't care about that
|
||||
group = group.replace(/[ \n]/gm, "")
|
||||
|
||||
// list of seen chars
|
||||
let chars = []
|
||||
for (let i = 0; i < group.length; i++) {
|
||||
// if not already seen, add to list of seen chars
|
||||
if (!chars.find(char => char == group[i])) { chars.push(group[i]) }
|
||||
}
|
||||
// keep a sum for our puzzle answer
|
||||
sum += chars.length
|
||||
}
|
||||
console.log(sum)
|
21
2020/06/part2.js
Normal file
21
2020/06/part2.js
Normal file
|
@ -0,0 +1,21 @@
|
|||
const fs = require('fs');
|
||||
|
||||
const data = fs.readFileSync('input.txt', 'utf-8');
|
||||
const groups = data.split('\n\n');
|
||||
|
||||
let sum = 0
|
||||
for (let g = 0; g<groups.length; g++){
|
||||
// list of people in group
|
||||
let group = groups[g].split('\n')
|
||||
// first person
|
||||
let first = group[0]
|
||||
|
||||
// for each of the first person's answers
|
||||
for (let answer = 0; answer < first.length; answer++) {
|
||||
// if the total amount of that answer in the group is equal to the amount of people in the group, then everyone in the group answered that question
|
||||
if (group.length == [...groups[g].matchAll(first[answer])].length) {
|
||||
sum++
|
||||
}
|
||||
}
|
||||
}
|
||||
console.log(sum)
|
|
@ -45,6 +45,8 @@ if (advent) {
|
|||
2. [:star: :star:](https://adventofcode.com/2020/day/2 "see puzzle")
|
||||
3. [:star: :star:](https://adventofcode.com/2020/day/3 "see puzzle")
|
||||
4. [:star: :star:](https://adventofcode.com/2020/day/4 "see puzzle")
|
||||
5. [:star: :star:](https://adventofcode.com/2020/day/5 "see puzzle")
|
||||
6. [:star: :star:](https://adventofcode.com/2020/day/6 "see puzzle")
|
||||
|
||||
## Languages Used
|
||||
* Python
|
||||
|
|
Loading…
Reference in a new issue