2020 day 13

This commit is contained in:
tristan 2020-12-13 14:53:10 +00:00
parent 7f73e72ec2
commit b46bf4115d
9 changed files with 53 additions and 12 deletions

21
2020/13/part1.js Normal file
View file

@ -0,0 +1,21 @@
// this was fairly simple; just keep increasing the wait until you find a bus that leaves. there is probably a more efficient solution but this works
const fs = require('fs');
const input = fs.readFileSync('input.txt', 'utf-8').split('\n');
const time = Number(input[0]);
const busses = input[1].split(',').filter(v=>v!='x').map(n => Number(n));
let isBus = false;
let timeDif = 0;
let busID;
for (; !isBus; timeDif++) {
for(bus of busses) {
if ((time + timeDif) % bus == 0){
busID = bus;
isBus = true;
break;
}
}
}
// the for loop does an extra increment at the end oops
timeDif--
console.log(timeDif*busID)