Skip to content

Commit 66ef494

Browse files
committed
Completed 'AngryFrogs' puzzle
1 parent 768b6a2 commit 66ef494

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed

Playground/Puzzles/AngryFrogs.test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { assertEquals } from '../../test_deps.ts';
2+
3+
import maxDistance from './AngryFrogs.ts';
4+
5+
6+
// RUN: deno test Playground/Puzzles/AngryFrogs.test.ts
7+
8+
//---------------------------------------------------------------------
9+
// ---------- UNIT TESTS ----------
10+
//---------------------------------------------------------------------
11+
12+
Deno.test({
13+
name: "[1,2,3,4,5] -> 4",
14+
fn() {
15+
let lilypadHeights = [1,2,3,4,5];
16+
assertEquals(maxDistance(lilypadHeights), 4);
17+
}
18+
});
19+
20+
21+
22+
Deno.test({
23+
name: "[5,7,3,1,2,3,4,6,2] -> 6",
24+
fn() {
25+
let lilypadHeights = [5,7,3,1,2,3,4,6,2];
26+
assertEquals(maxDistance(lilypadHeights), 6);
27+
}
28+
});

Playground/Puzzles/AngryFrogs.ts

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
export default function maxDistance(lilypadHeights: number[]) {
2+
Solution.initializeLilypadForest(lilypadHeights);
3+
return Solution.findMaxDistance();
4+
}
5+
6+
class Solution {
7+
8+
private static lilypadForest: number[];
9+
10+
static initializeLilypadForest(lilypadHeights: number[]) {
11+
Solution.lilypadForest = lilypadHeights;
12+
}
13+
14+
static findMaxDistance(): number {
15+
16+
let maxDistance = 0;
17+
18+
let distanceTraveledLeft, distanceTraveledRight, distanceTraveled;
19+
20+
for(let i=0; i < Solution.lilypadForest.length; ++i) {
21+
// console.log("Lilypad", i);
22+
distanceTraveledRight = Solution._travelRightFrom(i);
23+
distanceTraveledLeft = Solution._travelLeftFrom(i);
24+
25+
distanceTraveled = distanceTraveledLeft + distanceTraveledRight;
26+
27+
// console.log(" Distance Traveled:", distanceTraveled)
28+
29+
if(distanceTraveled > maxDistance)
30+
maxDistance = distanceTraveled;
31+
32+
// console.log(" Max Distance:", maxDistance)
33+
}
34+
35+
return maxDistance;
36+
}
37+
38+
private static _travelRightFrom(index: number): number {
39+
for(let i=index; ; ++i) {
40+
if(i+1 == Solution.lilypadForest.length ||
41+
Solution.lilypadForest[i+1] < Solution.lilypadForest[i]) {
42+
// console.log(" Traveled Right: ", i-index);
43+
return i-index;
44+
}
45+
}
46+
}
47+
48+
private static _travelLeftFrom(index: number): number {
49+
for(let i=index; ; --i) {
50+
if(i-1 < 0 || Solution.lilypadForest[i-1] < Solution.lilypadForest[i]) {
51+
// console.log(" Traveled Left: ", index-i);
52+
return index-i;
53+
}
54+
}
55+
}
56+
}
57+
58+
//---------------------------------------------------------------------
59+
// ---------- MAIN PROGRAM ----------
60+
//---------------------------------------------------------------------
61+
if (import.meta.main) {
62+
63+
let forest1 = [5,7,3,1,2,3,4,6,2];
64+
console.log("Forest 1: ", maxDistance(forest1));
65+
66+
// RUN: deno run Playground/Puzzles/AngryFrogs.ts
67+
}

0 commit comments

Comments
 (0)