-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday05.ts
58 lines (49 loc) · 1.87 KB
/
day05.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import * as fs from 'fs';
const parseNumbers = (str: string) => str.split(' ').filter(x => x !== '').map(x => parseInt(x));
const groupNumbers = (numbers: number[], grouping: number): number[][] => Array.from(
{ length: numbers.length / grouping },
(_, i) => numbers.slice(i * grouping, i * grouping + grouping))
const input = fs.readFileSync('day05.txt', { encoding: 'utf8' })
.replaceAll(/\r\n(\d)/g, ' $1')
.split('\r\n')
.filter(x => x !== '')
.map(x => parseNumbers(x.split(':')[1]));
const seeds = input[0];
const almanac = input.slice(1).map(x => groupNumbers(x, 3));
function getSeedLocation(step: number): number {
for (const almanacEntry of almanac) {
for (const [destination, source, length] of almanacEntry) {
if (source <= step && source + length > step) {
step = destination + step - source;
break;
}
}
}
return step;
}
console.log("Part 1", Math.min(...seeds.map(x => getSeedLocation(x))));
const seedRanges = groupNumbers(seeds, 2);
const doWeHaveThatSeed = (seed: number): boolean => seedRanges
.some(([seedStart, length]) => seedStart <= seed && seedStart + length >= seed);
// inversion of getSeedLocation function
function getSeedGivenLocation(step: number): number {
for (const almanacEntry of almanac.slice().reverse()) {
for (const [destination, source, length] of almanacEntry) {
if (destination <= step && destination + length > step) {
step = source + step - destination;
break;
}
}
}
return step;
}
// problem inversed, rather than enumerating on enormous amount of seeds we enumerating on
// ascending locations and checks if we have got seed for that location 🤡
// Tooks ~8 seconds to compute on my pc 🤡🤡🤡
for (let i = 0; i < 1_000_000_000; i++) {
const seed = getSeedGivenLocation(i);
if (doWeHaveThatSeed(seed)) {
console.log("Part 2", i);
break;
}
}