Last active
January 17, 2022 09:12
-
-
Save amahdy/21d40d6cbf5f918ab517cc6dc9165078 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Code to run in the browser console of `go/techcon-lightningtalks` */ | |
let loadData = function() { | |
// Array holding the data | |
let data = Array(); | |
// Keep track of `H2` elements | |
let index = 1; | |
// Keep track of pillars | |
let pillar = 1; | |
// Make sure not to overflow the `H2` elements | |
let length = document.getElementsByTagName("h2").length; | |
// Loop over pillars | |
while(document.getElementsByTagName("h2")[index].innerText.startsWith(pillar)) { | |
// Create a new pillar array entry, index 1 | |
data[pillar] = Array(); | |
// Keep track of topics | |
let topic = 1; | |
// Scan element and all its siblings for a given pillar | |
let elem; | |
while(elem = document.getElementsByTagName("h2")[++index].nextElementSibling) { | |
// Create a new topic array entry, index 1 | |
data[pillar][topic] = Array(); | |
// Keep track of talks | |
let talk = 1; | |
// Scan text under a talk | |
while(elem && elem.innerText.startsWith(talk)) { | |
// Create a new talk entry, this is the actual data we are looking for | |
// We care about title and text body only | |
// Thus skipping the second element sibling | |
// Assume that always a talk has 3 sibling lines | |
data[pillar][topic][talk] = Array(); | |
// Line 0 | |
data[pillar][topic][talk][0] = elem.innerText.substr(3); | |
// Line 2 (mapped into 1 to match the cipher) | |
elem = elem.nextElementSibling.nextElementSibling; | |
data[pillar][topic][talk][1] = elem.innerText; | |
// Move to next talk | |
elem = elem.nextElementSibling; | |
talk++; | |
} | |
// Move to next topic | |
topic++; | |
// Make sure not to overflow the `H2` index | |
if(length-index==1) { | |
break; | |
} | |
} | |
// Move to next pillar | |
pillar++; | |
} | |
return data; | |
} | |
let decipher = function(cipher) { | |
// FIXME: Can load the data once per page instead of multiple times per operation | |
let data = loadData(); | |
let s =""; | |
for(let i=0; i<cipher.length; i++) { | |
let x = cipher[i].split(":"); | |
// Rearrange the cipher elements into correct order | |
// And then extract char from `data` array | |
s += data[x[0]][x[1]][x[3]][x[2]].charAt(x[4]-1); | |
} | |
return s; | |
} | |
/* === end of code === */ | |
/* Decipher any text, example usage: */ | |
console.log( | |
"Solution is:\n", | |
decipher( | |
['6:1:1:3:44', '9:2:0:2:105', '1:2:1:3:353', '5:5:1:2:75', '1:5:1:3:165', '5:4:1:1:129', '5:3:0:4:74', '1:5:0:3:22', '1:5:1:3:212', '4:1:1:4:217', '2:5:1:1:64', '2:2:1:2:125', '1:4:1:2:206', '7:2:1:2:27', '5:5:1:1:120', '8:4:1:2:45', '5:5:0:2:69', '1:5:0:1:51', '3:5:1:2:265', '7:2:1:1:172', '7:2:0:2:5', '1:5:0:1:17', '10:1:0:1:35', '3:1:1:4:21', '1:4:1:3:80', '1:1:0:1:7', '8:4:0:1:7', '1:2:0:2:58'] | |
) | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment