JSFiddle - React, Tailwind, and code Playground
Skiing Problem
by Laxmikant Dange
JavaScript
/// <reference path="./typings/underscore/underscore.d.ts" />
class Skiing {
availablePaths = [];
mapSize = {
x: 4,
y: 4
};
currentPosition = {
x: 0,
y: 0
}
constructor(private data?: any[]) {
}
start() {
for (let y = 0; y < this.mapSize.y; y++) {
for (let x = 0; x < this.mapSize.x; x++) {
let p = [];
p.push(this.data[y][x]);
this.explore(x, y, p);
}
}
let _sortedPath = this.availablePaths.sort((a: any, b: any) => {
let _a = a.reduce((q, w) => {
return q + w;
});
let _b = b.reduce((e, r) => {
return e + r;
});
if (_a > _b)
return -1;
if (_a < _b)
return 1;
return 0;
});
// Filter longest path
if (_sortedPath.length > 1) {
let _maxWeight = _sortedPath.map(a => {
return a.length;
}).sort((_a, _b) => _a - _b).reverse()[0];
_sortedPath = _sortedPath.filter(a => a.length === _maxWeight);
}
// Filter steepest path
if (_sortedPath.length > 1) {
let _maxStep = _sortedPath.map(a => {
return (a[0] - a[a.length - 1]);
})[0];
_sortedPath = _sortedPath.filter(a => (a[0] - a[a.length - 1]) === _maxStep);
}
_sortedPath = _sortedPath[0];
console.log("Longest path:", _sortedPath, "Drop:", (_sortedPath[0] + _sortedPath[_sortedPath.length - 1]));
}
explore(x: number, y: number, path: any[]) {
if (y == this.mapSize.y - 1) {
this.availablePaths.push(path);
}
// Right Node
if (x < (this.mapSize.x - 1)) {
if (this.data[y][x + 1] < this.data[y][x]) {
// console.log("Increasing X");
let p = path.concat();
...