-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp1.ts
65 lines (51 loc) · 1.52 KB
/
p1.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
59
60
61
62
63
64
65
import { readFileSync } from 'fs';
const data = readFileSync('input.txt', 'utf8');
const [depsRaw, topRaws] = data.split('\n\n');
const deps: string[] = depsRaw.split(`\n`).filter(line => line.trim() !== '');
const tops: string[] = topRaws.split(`\n`).filter(line => line.trim() !== '');
const validTops: string[] = [];
// Each supposed topological order
for (const top of tops) {
const orderFiltered = top.split(",");
// J is every number of the topological order
let viable = true;
for (let j = 0; j < orderFiltered.length; j++) {
// iterate over each node to find if has any Dependencies
let stop = false;
for (const dependence of deps) {
const [num1, num2] = dependence.split("|");
if (num2 === orderFiltered[j]) {
// The number has a dependence
let found = false;
let foundIndex: number | null = null;
for (let k = 0; k < orderFiltered.length; k++) {
if (orderFiltered[k] === num1) {
found = true
foundIndex = k;
break
}
}
if (found && foundIndex !== null) {
if (foundIndex < j) {
viable = true;
}
if (foundIndex > j) {
viable = false
break;
}
}
}
}
if (!viable) break;
}
if (viable) {
validTops.push(top)
}
}
let acc = 0;
for (const top of validTops) {
const nums = top.split(",");
const middle = nums[Math.floor(nums.length / 2)];
acc += parseInt(middle)
}
console.log("ValidTops", acc)