Skip to content

Instantly share code, notes, and snippets.

@suraneti
Created February 5, 2021 03:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save suraneti/741e5e071c4fdd042d8f8106876104aa to your computer and use it in GitHub Desktop.
Save suraneti/741e5e071c4fdd042d8f8106876104aa to your computer and use it in GitHub Desktop.
Test
// const circleOne = ["A,B", "A,C", "B,C"];
// const circleTwo = ["B,D", "B,E", "D,E"];
// const circleThree = ["F,G"];
// // const input = [
// // "A,B", // *
// // "A,C", // *
// // "B,C", // *
// // "B,D", // *
// // "D,E", // 0
// // "A,B", // 0
// // "B,E", // 0
// // "F,G", // *
// // ];
// const findLongestLoop = (input) => {
// const pair = {};
// for (let i = 0; i < input.length; i++) {
// if (pair[input[i]]) {
// continue;
// }
// // circle one
// if (circleOne.includes(input[i])) {
// pair[input[i]] = true;
// }
// // circle two
// if (circleTwo.includes(input[i])) {
// pair[input[i]] = true;
// }
// // circle three
// if (
// circleThree.includes(first) &&
// circleThree.includes(second) &&
// first !== second
// ) {
// pair[input[i]] = true;
// }
// }
// return Object.keys(pair).length;
// };
// // console.log(findLongestLoop(input));
const treemap = {
A: { B: { D: { E: "" }, E: "", C: "" }, C: "" },
F: { G: "" },
};
const input = [
// "A,B", // *
// "A,C", // *
// "B,C", // *
// "B,D", // *
// "D,E", // 0
// "A,B", // 0
// "B,E", // 0
// "F,G", // *
"A,B",
"B,C",
"D,E",
];
const findLogest = () => {
const pair = {};
let foundBridge = false;
for (let i = 0; i < input.length; i++) {
const split = input[i].split(",");
const first = split[0];
const second = split[1];
if (treemap[first]) {
if (treemap[first].hasOwnProperty(second) && !pair[input[i]]) {
if (second === "B") {
foundBridge = true;
}
pair[input[i]] = true;
// console.log(`${first},${second}`)
}
}
if (
foundBridge &&
first === "B" &&
treemap["A"][first][second] !== undefined &&
!pair[input[i]]
) {
pair[input[i]] = true;
// console.log(`${first},${second}`)
}
}
if (pair["B,C"] && (pair["B,D"] || pair["B,E"])) {
delete pair["B,C"];
}
return pair;
};
const result = findLogest(input);
console.log(result, Object.keys(result).length);
/**
*
* A
* / \
* B -- C F - G
* / \
* D - E
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment