DEV Community

Matt Ellen
Matt Ellen

Posted on

ROT13: awful answers only

New year, new challenges, new awful code.

Today you need to decode a short message:

066085033032076066072032078069082032078074083072089044032079072071032086032089086088082032076066072046

Here are a couple of hints:

  • That's some ASCII encoding
  • Only the letters have been ROT13 encoded.

Remember! The code should be suboptimal. It's just after the holiday season so you need to get your CPU back in shape with some exercise.

Discussion (3)

Collapse
qm3ster profile image
Mihail Malo • Edited on
Buffer.from('066085033032076066072032078069082032078074083072089044032079072071032086032089086088082032076066072046'.match(/.../g).map(x=>parseInt(x,10))).map(x=>x>64?x%26+97:x).toString('ascii')
Enter fullscreen mode Exit fullscreen mode

Allocating intermediate storage?
A regex?
Not specifying the radix to parseInt?
Not checking for punctuation other than which was used? ([-` and (-~) ranges)
Only working in node and not on the web due to using Buffer instead of TextDecoder?
And worst of all, completely misinterpreting the task, writing code that isn't especially slow?
Surely no worse could be done!

Collapse
mellen profile image
Matt Ellen Author

Any language or platform is permissible, so you're fine limiting to node :D

Collapse
mellen profile image
Matt Ellen Author

To set the ball rolling:

(function ()
{
  let input = '066085033032076066072032078069082032078074083072089044032079072071032086032089086088082032076066072046'; 

  function parseInteger(str)
  {
    let num = 0;

    for(let i = 0; i < str.length; i++)
    {
      let pn = parseInt(str[i], 10);
      num += pn * Math.pow(10, (str.length-1)-i);
    }

    return num;
  }

  function decodeASCIIPoint(num)
  {
    let p = document.createElement('p');
    p.innerHTML = `&#${num};`;
    return p.textContent;
  }

  function numbersToString(nums)
  {
    let parts = nums.match(/\d\d\d/g);

    let str = '';

    for(let i = 0; i < parts.length; i++)
    {
      let part = parts[i];
      let num = parseInteger(part);
      str += decodeASCIIPoint(num);
    }

    return str;
  }

  function rot13(str)
  {
    let chars = str.split('');
    let result = '';

    for(let ci = 0; ci < chars.length; ci++)
    {
      switch(chars[ci])
      {
        case 'A':
          result += 'N';
          break;
        case 'B':
          result += 'O';
          break;
        case 'C':
          result += 'P';
          break;
        case 'D':
          result += 'Q';
          break;
        case 'E':
          result += 'R';
          break;
        case 'F':
          result += 'S';
          break;
        case 'G':
          result += 'T';
          break;
        case 'H':
          result += 'U';
          break;
        case 'I':
          result += 'V';
          break;
        case 'J':
          result += 'W';
          break;
        case 'K':
          result += 'X';
          break;
        case 'L':
          result += 'Y';
          break;
        case 'M':
          result += 'Z';
          break;
        case 'N':
          result += 'A';
          break;
        case 'O':
          result += 'B';
          break;
        case 'P':
          result += 'C';
          break;
        case 'Q':
          result += 'D';
          break;
        case 'R':
          result += 'E';
          break;
        case 'S':
          result += 'F';
          break;
        case 'T':
          result += 'G';
          break;
        case 'U':
          result += 'H';
          break;
        case 'V':
          result += 'I';
          break;
        case 'W':
          result += 'J';
          break;
        case 'X':
          result += 'K';
          break;
        case 'Y':
          result += 'L';
          break;
        case 'Z':
          result += 'M';
          break;
        default:
          result += chars[ci];
      }
    }

    return result;
  }

  return rot13(numbersToString(input));
})();
Enter fullscreen mode Exit fullscreen mode