Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const fs = require("fs");
- data = fs.readFileSync("./metro.in", "utf-8");
- arr = data.split("\r\n").map(i => i.split(" ").map(k => parseInt(k)));
- //n,m,S,E,x,y
- get_to_end = ( lines, s, e) => {
- if (s == e) {
- return [];
- } else {
- cl = lines.filter(line => line[0] == s).sort((a, b) => a.length - b.length)[0];
- return cl != undefined ? [cl].concat(get_to_end(lines, cl[cl.length - 1], e)) : [];
- }
- };
- do_test_cases = (i) => {
- if (i < arr.length) {
- [s, e] = [arr[i][2], arr[i][3]];
- lines = [];
- for (j = i + 1; j <= i + arr[i][1]; j++)
- lines.push(arr[j].slice(1, arr[j].length));
- route = get_to_end(lines, s, e);
- console.log(route.slice(-1)[0].slice(-1)[0] == e ? route.reduce((c, n, i) => c + (i + 1 != route.length - 1 ? 5 : 0) + ((n.length - 2) * 2), ((route[0].length - 2) * 2)) : "impossible");
- do_test_cases(i + arr[i][1] + 1);
- }
- };
- do_test_cases(0);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement