solve 2020d13

This commit is contained in:
Kegan Myers 2020-12-13 00:52:19 -06:00
parent 1a5bb7c360
commit 7ed737b9a2
3 changed files with 59 additions and 0 deletions

2
data/2020/13.txt Normal file
View file

@ -0,0 +1,2 @@
1005595
41,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,37,x,x,x,x,x,557,x,29,x,x,x,x,x,x,x,x,x,x,13,x,x,x,17,x,x,x,x,x,23,x,x,x,x,x,x,x,419,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,19

56
solutions/2020/13.js Normal file
View file

@ -0,0 +1,56 @@
const { Glib, BNMath: Math, fn } = require('../../lib');
const parse = (input) => {
const [arrivalString, idStrings] = input.split('\n');
return {
arrival: BigInt(arrivalString),
ids: Glib.fromSplit(idStrings, ',').map((s) =>
s === 'x' ? 1n : BigInt(s),
),
};
};
const departureAfter = (time, id) => {
let next = (time / id) * id;
return next < time ? next + id : next;
};
module.exports = {
'1': (input) => {
input = parse(input);
return input.ids
.flatMap((i) => {
if (i === 1n) {
return [];
}
return [[i, departureAfter(input.arrival, i) - input.arrival]];
})
.minScore()
.glib.product();
return id * (time - input.arrival);
},
'2': (input) => {
input = parse(input).ids.array;
let solved = 1n;
let step = input[0];
let start = departureAfter(0n, input[0]);
while (true) {
for (let i = solved; i < input.length; i++) {
const currentBus = input[i];
const currentTime = start + i;
if (departureAfter(currentTime, currentBus) === currentTime) {
solved += 1n;
step *= currentBus;
} else {
break;
}
}
if (solved == input.length) {
return start;
}
start += step;
}
},
};

View file

@ -14,6 +14,7 @@ const results = {
10: [2277, 37024595836928],
11: [2359, 2131],
12: [508, 30761],
13: [2095, 598411311431841],
},
2019: {
1: [3386686, 5077155],