Skip to content

Instantly share code, notes, and snippets.

@unlox775
Created December 25, 2019 05:08
Show Gist options
  • Save unlox775/683ce409b51eb736546bfd25c01685ce to your computer and use it in GitHub Desktop.
Save unlox775/683ce409b51eb736546bfd25c01685ce to your computer and use it in GitHub Desktop.
Ethan code treasure hunt 2019
<?php
$str = "00 Ethan Buchanan 99";
// The CIPHER...
// 1. Look at the ascii value ord() of the first digit (e.g. "a" = 97)
// 2. Find the char in the string that corresponds to that number (i.e. the 97th char)
// 3. If the string is shorter than that number, wrap around to the first number using MOD()
// 4. Move that char to be the first char
// 5. Repeat
//
// Note this would not work in these cases:
// - it not scramble any chars positions after the highest ascii value in the string
// - If the string has any NULL (0-ascii), it would error, as we are getting 1-based Nth char
// - If the string has any 1-ascii chars, it would stop scrambling
// - the prev 2 rules apply if the MOD ends up at 1 or 0
//
// Your mission, is to write a reverse-CIPHER. You will know when you are done, when the
// first 2 chars are "00", and the last 2 are "99".
echo "String Length:". strlen($str). "\n\n";
$i = 0;
echo 'Start: '. str_replace("\n",'\n',$str)."\n";
while ($i < 100000) {
$char = substr($str,0,1); // the first char of the string
$n = ord($char); // ascii value
$str = moveCharToFirst($str,$n);
echo "Iter: ". sprintf("%08d",$i) ." first char was ". sprintf("%03d",$n) .', "'. str_replace("\n",'%',$char) .'" -- '. str_replace("\n",'\n',$str)."\n";
$i++;
}
function moveCharToFirst($str, $n) {
$n = $n % (strlen($str) +1); // modulus if the char they want to move is > than the string length
// Let us know if
if ( $n == 0 || $n == 1 ) { throw new Exception("FATAL: had a char that was NULL or exactly MOD = the length of the string"); }
$new_str =
substr($str, $n-1,1) // the char we are moving
. substr($str, 0, $n-1); // the first part
if ( $n < strlen($str) ) {
$new_str .= substr($str, $n); // the last part
}
return $new_str;
}
Output:
String Length:20
Start: 00 Ethan Buchanan 99
Iter: 00000000 first char was 048, "0" -- h00 Etan Buchanan 99
Iter: 00000001 first char was 104, "h" -- 9h00 Etan Buchanan 9
Iter: 00000002 first char was 057, "9" -- a9h00 Etan Buchnan 9
Iter: 00000003 first char was 097, "a" -- ua9h00 Etan Bchnan 9
Iter: 00000004 first char was 117, "u" -- ua9h00 EtanBchnan 9
Iter: 00000005 first char was 032, " " -- a ua9h00 EtnBchnan 9
Iter: 00000006 first char was 097, "a" -- Ba ua9h00 Etnchnan 9
...
Iter: 00099990 first char was 104, "h" -- nhn90aEa 0hacuBt 9 n
Iter: 00099991 first char was 110, "n" -- 0nhn9aEa 0hacuBt 9 n
Iter: 00099992 first char was 048, "0" -- a0nhn9Ea 0hacuBt 9 n
Iter: 00099993 first char was 097, "a" -- ca0nhn9Ea 0hauBt 9 n
Iter: 00099994 first char was 099, "c" -- Bca0nhn9Ea 0haut 9 n
Iter: 00099995 first char was 066, "B" -- aBc0nhn9Ea 0haut 9 n
Iter: 00099996 first char was 097, "a" -- haBc0nhn9Ea 0aut 9 n
Iter: 00099997 first char was 104, "h" -- nhaBc0nhn9Ea 0aut 9
Iter: 00099998 first char was 110, "n" -- cnhaB0nhn9Ea 0aut 9
Iter: 00099999 first char was 099, "c" -- acnhaB0nhn9Ea 0ut 9
"t\nhI,eg.aie dhttsopbp90 tctaes fet dfouti0"
"g uec eaolnr\nar e rs r,i z ssff idlpunu"
"a ts dtaa nsophwh\n}i!htlinbaeoemeee Iipt{9"
Concatenate these 3 strings together.
This is the value you must reverse-cipher.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment