**This is our solution and implementation to problem #86 on Project Euler.**

Our code is written in TypeScript, a language which is built on-top of JavaScript and transpiles to it. We've included the problem statement, our code (which is commented for greater clarity), our video which outlines our analysis and implementation approach, and the solution + how long it took to calculate it.

**Note:** the code and contents here might be slightly different than what is in the video. We've made some improvements to some of the code since recording.

If you would like to view the original problem and solve it, please visit: Cuboid Route on Project Euler. If you're having trouble solving this problem, or are just curious to see how others have solved it, feel free to take a look, but please put solid effort into solving this before viewing the actual solution to the problem.

### Problem Statement

A spider, S, sits in one corner of a cuboid room, measuring 6 by 5 by 3, and a fly, F, sits in the opposite corner. By travelling on the surfaces of the room the shortest "straight line" distance from S to F is 10 and the path is shown on the diagram.

However, there are up to three "shortest" path candidates for any given cuboid and the shortest route doesn't always have integer length.

It can be shown that there are exactly 2060 distinct cuboids, ignoring rotations, with integer dimensions, up to a maximum size of M by M by M, for which the shortest route has integer length when M = 100. This is the least value of M for which the number of solutions first exceeds two thousand; the number of solutions when M = 99 is 1975.

Find the least value of M such that the number of solutions first exceeds one million.

### Our Solution

Our solution is given in the TypeScript files below. This solution uses more than one code file. Some solutions use utilities which were created and enhanced while working on this and previous Project Euler problems. Some code in the utilities files might not be used in this particular problem.

` ````
import { Fraction } from "../../utils/fraction";
import { AbstractSolution, RunSolution } from "../../utils/solution";
@RunSolution
export class Solution86 extends AbstractSolution {
getProblemName(): string {
return "Cuboid Route";
}
protected solve() {
//return this.doSolve(2_000);
return this.doSolve(1_000_000);
}
/**
* Generate pythagorean triplets to determine integer line lengths
* Triplet values will be used to determine cuboid dimensions
* @param limit
* @returns
*/
private doSolve(limit: number): number {
//keep track of count per dimension
const dimMap = new Map<number, number>();
let isSearching = true;
//generate triplets
for (let m = 2; isSearching; m++) {
const mSq = m ** 2;
for (let n = 1; n < m; n++) {
if ((m + n) % 2 === 1 && Fraction.gcdTwoNums(m, n) === 1) {
const nSq = n ** 2;
const aRoot = mSq - nSq;
const bRoot = 2 * m * n;
const cRoot = mSq + nSq;
for (let k = 1; true; k++) {
const [a, b, c] = [aRoot * k, bRoot * k, cRoot * k].sort((a, b) => a - b);
if (b > limit) {
if (k === 1) {
isSearching = false;
}
break;
}
//process a
dimMap.set(
a,
(dimMap.get(a) || 0) + this.calcSingle(b, a)
);
//procces b
dimMap.set(
b,
(dimMap.get(b) || 0) + this.calcSingle(a, b)
);
}
}
}
}
/**
* Iterate over results
* Keep counting until we reach limit,
* then, return last m value seen
*/
let count = 0;
const mKeys = Array.from(dimMap)
.sort((a, b) => a[0] - b[0]);
let mMax = 0;
for (let i = 0; count <= limit; i++) {
//[m, thisCount]
count += mKeys[i][1];
mMax = mKeys[i][0];
}
return mMax;
}
/**
* Get the count for a single combo of:
* : a + (b-i) + i
* : (a+i) + i + b
*
* single is unaltered
*/
private calcSingle(plus: number, single: number): number {
if (single >= plus) {
return Math.floor(plus/2);
}
const calc = single - Math.floor((plus-1)/2);
return Math.max(0, calc);
}
}
```

### Results

This implementation found the solution in **1746ms**.

If you would like to view the answer, click below to reveal. Please consider reviewing the implementation and trying to code your own solution before viewing the answer.

## View Answer

The answer is

**1818.**

All of our solutions are hosted on GitHub. The code on this page was pulled from the repo and the solution and execution time were calculated based on that code.