Compare commits
2 commits
1c618df84c
...
7ed737b9a2
Author | SHA1 | Date | |
---|---|---|---|
Kegan Myers | 7ed737b9a2 | ||
Kegan Myers | 1a5bb7c360 |
2
data/2020/13.txt
Normal file
2
data/2020/13.txt
Normal 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
|
13
lib/Glib.js
13
lib/Glib.js
|
@ -89,7 +89,7 @@ class Glib {
|
||||||
flatMap(mapFn) {
|
flatMap(mapFn) {
|
||||||
return new Glib(
|
return new Glib(
|
||||||
(function*(iterable) {
|
(function*(iterable) {
|
||||||
let index = 0;
|
let index = 0n;
|
||||||
for (const entry of iterable) {
|
for (const entry of iterable) {
|
||||||
yield* mapFn(entry, index++);
|
yield* mapFn(entry, index++);
|
||||||
}
|
}
|
||||||
|
@ -330,7 +330,7 @@ class Glib {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
reduceScoreK(chooseFn) {
|
reduceScoreK(chooseFn) {
|
||||||
return this.reduce((a, b) => (chooseFn(a[1] > b[1]) ? a : b))[0];
|
return this.reduce((a, b) => (chooseFn(a[1], b[1]) ? a : b))[0];
|
||||||
}
|
}
|
||||||
minScoreK() {
|
minScoreK() {
|
||||||
return this.reduceScoreK((a, b) => a < b);
|
return this.reduceScoreK((a, b) => a < b);
|
||||||
|
@ -338,6 +338,15 @@ class Glib {
|
||||||
maxScoreK() {
|
maxScoreK() {
|
||||||
return this.reduceScoreK((a, b) => a > b);
|
return this.reduceScoreK((a, b) => a > b);
|
||||||
}
|
}
|
||||||
|
reduceScore(chooseFn) {
|
||||||
|
return this.reduce((a, b) => (chooseFn(a[1], b[1]) ? a : b));
|
||||||
|
}
|
||||||
|
minScore() {
|
||||||
|
return this.reduceScore((a, b) => a < b);
|
||||||
|
}
|
||||||
|
maxScoreK() {
|
||||||
|
return this.reduceScore((a, b) => a > b);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = Glib;
|
module.exports = Glib;
|
||||||
|
|
|
@ -16,6 +16,14 @@ Object.defineProperty(Array.prototype, 'set', {
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Object.defineProperty(Array.prototype, 'lengthN', {
|
||||||
|
enumerable: false,
|
||||||
|
configurable: false,
|
||||||
|
get() {
|
||||||
|
return BigInt(this.length);
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
Array.prototype.safeSplice = function safeSplice(...args) {
|
Array.prototype.safeSplice = function safeSplice(...args) {
|
||||||
const spliced = this.slice();
|
const spliced = this.slice();
|
||||||
spliced.splice(...args);
|
spliced.splice(...args);
|
||||||
|
|
|
@ -1,5 +1,13 @@
|
||||||
const crypto = require('crypto');
|
const crypto = require('crypto');
|
||||||
|
|
||||||
|
Object.defineProperty(String.prototype, 'lengthN', {
|
||||||
|
enumerable: false,
|
||||||
|
configurable: false,
|
||||||
|
get() {
|
||||||
|
return BigInt(this.length);
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
Object.defineProperty(String.prototype, 'md5', {
|
Object.defineProperty(String.prototype, 'md5', {
|
||||||
enumerable: false,
|
enumerable: false,
|
||||||
configurable: false,
|
configurable: false,
|
||||||
|
|
|
@ -8,14 +8,14 @@ const VALUE = {
|
||||||
module.exports = {
|
module.exports = {
|
||||||
'1': (input) =>
|
'1': (input) =>
|
||||||
Glib.fromIterable(input)
|
Glib.fromIterable(input)
|
||||||
.filter((char, index) => char === input[(index + 1) % input.length])
|
.filter((char, index) => char === input[(index + 1n) % input.lengthN])
|
||||||
.toInts()
|
.toInts()
|
||||||
.sum(),
|
.sum(),
|
||||||
'2': (input) =>
|
'2': (input) =>
|
||||||
Glib.fromIterable(input)
|
Glib.fromIterable(input)
|
||||||
.filter(
|
.filter(
|
||||||
(char, index) =>
|
(char, index) =>
|
||||||
char === input[(index + input.length / 2) % input.length],
|
char === input[(index + input.lengthN / 2n) % input.lengthN],
|
||||||
)
|
)
|
||||||
.toInts()
|
.toInts()
|
||||||
.sum(),
|
.sum(),
|
||||||
|
|
|
@ -23,7 +23,7 @@ module.exports = {
|
||||||
...Glib.fromLines(input).map((line) =>
|
...Glib.fromLines(input).map((line) =>
|
||||||
twodee
|
twodee
|
||||||
.everyStep(parseLine(line))
|
.everyStep(parseLine(line))
|
||||||
.map((pt, i) => [pt.string, i + 1])
|
.map((pt, i) => [pt.string, i + 1n])
|
||||||
.toMap(),
|
.toMap(),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
|
56
solutions/2020/13.js
Normal file
56
solutions/2020/13.js
Normal 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;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
};
|
|
@ -14,6 +14,7 @@ const results = {
|
||||||
10: [2277, 37024595836928],
|
10: [2277, 37024595836928],
|
||||||
11: [2359, 2131],
|
11: [2359, 2131],
|
||||||
12: [508, 30761],
|
12: [508, 30761],
|
||||||
|
13: [2095, 598411311431841],
|
||||||
},
|
},
|
||||||
2019: {
|
2019: {
|
||||||
1: [3386686, 5077155],
|
1: [3386686, 5077155],
|
||||||
|
|
Loading…
Reference in a new issue