13
\$\begingroup\$

Introduction

Bob likes to have different passwords for each website he uses. He wants passwords in a special format so that he can remember. Help him by building a password generator which is as short as possible in code length, because he likes keeping stuff short just like his name.

Challenge

Build Bob a password generator which takes in four parameters -

  • Domain address, (ex : stackexchange.com)
  • First name,
  • last name,
  • Birth date and year.

and outputs a string which has undergone the following transformations.

Every password has a template based on the length l of the domain name. If l is prime then the template is of this form -

[birth-date][domain-name][lastname][birth-year][symbol]

else it is of this form -

[symbol][birth-year][firstname][domain-name][birth-date].

The symbol field will have values based on the domain type.

.com    ->  $$$
.org    ->  &&&
.net    ->  %%%
.edu    ->  +++
.gov    ->  @@@
.mil    ->  >>>
default ->  ~~~

Now, he also wants to run a caesar cipher over fields - [firstname][domain-name][lastname].The shift direction is right if l is prime else it is left. The shift value is determined by the value of addition of birth date and year.

After the above process, the text cannot be easily remembered so he wants the ciphered text to be pronounceable. To do this he wants to replace every third consecutive consonant with a vowel in alphabetical order (i.e, aeiou).

Finally, he wants to delete consecutive occurrences of same letters.

Example

Input  - stackexchange.com bob williams 0894

Output - 08qraicvafyecugjigyoq94$$$.

Explanation :

Password template is [birth-date][domain-name][lastname][birth-year][symbol], because the length of domain name stackexchange is a prime. Symbol will be $$$ as domain type is .com.

Let's fill those values in the template - 08stackexchangewilliams94$$$. Now caesar cipher has to be run on fields [firstname][domain-name][lastname] (it's just lastname in this case). Shift direction is right because l is prime and the shift value is 08+94 = 102.

So, 08stackexchangewilliams94$$$ turns into 08qryaicvafylecugjjgykq94$$$. Now every third consecutive consonant is replaced with a vowel - 08qraaicvafyeecugjigyoq94$$$ every consecutive ocuurences of same letter is deleted - 08qraicvafyecugjigyoq94$$$. Don't worry it is pronounceable for Bob.

Additional info

  • Domain address, first name and last name contain only lowercase alphabets.
  • Domain address will always consist of only a second-level and top-level domain, like stackexchange.com and not codegolf.stackexchange.com. Also, domains like .co.uk is not allowed but just .uk or .in is valid.
  • Replacement of vowel in alphabetical order is cyclic. I mean after u, a is used to replace.
  • ASCII range for Caesar cipher is 97 - 122 inclusive.
  • You can take input as described here.
  • Output has to be a string.
  • The format of birth date and year is dd and yy.So 02 is acceptable and 2 is not.

This is code-golf, so the shortest code in bytes wins. Happy Golfing!

\$\endgroup\$
11
  • 4
    \$\begingroup\$ Will domain name always consist of only a second-level and top-level domain, like stackexchange.com, not img.stackexchange.com? What about country domains like bbc.co.uk? \$\endgroup\$
    – briantist
    Dec 12, 2016 at 15:50
  • \$\begingroup\$ Your example call uses 0894 which adds up to 102 but the explanation uses 08+96=106. The ciphered text appears to use the 102 value. Just a slight inconsistency that could be confusing... \$\endgroup\$
    – briantist
    Dec 12, 2016 at 17:00
  • 6
    \$\begingroup\$ Bob is one crazy mofo if he can pronounce that. \$\endgroup\$ Dec 12, 2016 at 21:30
  • 5
    \$\begingroup\$ @carusocomputing Maybe Bob is Welsh? \$\endgroup\$
    – briantist
    Dec 12, 2016 at 21:35
  • 1
    \$\begingroup\$ No I meant you types defualt not default \$\endgroup\$ Dec 14, 2016 at 11:40

1 Answer 1

7
\$\begingroup\$

PowerShell 3+, 480 476 474 453 450 448 bytes

param($d,$f,$l,$b)$s,$t=$d-split'\.'
$m,$y=$b-split'(?<=..)(?=..)'
$v=+$m+$y
$p='1'*$s.Length-match'^(?!(..+)\1+$)..'
$c={-join([char[]]"$input"|%{[char](97..122)[((+$_-97)+((26-$v),$v)[$p])%26]})}
$s=$s|&$c
$f=$f|&$c
$l=$l|&$c
$a=(($a=@{com='$';org='&';net='%';edu='+';gov='@';mil='>'}.$t),'~')[!$a]*3
("`"$(("$a$y$f$s$m","$m$s$l$y$a")[$p]-replace'([^aeiou\d]{3})',"`$('`$1'-replace'[a-z]`$$',('aeiou'[`$i++%5]))")`""|iex)-replace'([a-z])\1+','$1'

Try it online!

Saved 24 26 bytes thanks to TimmyD!

Wow, this was a crazy one. I don't think I can post a full breakdown of this until later or tomorrow.

Note: all line endings are \n (0x10), so that I don't have to use ; and put this all on one line.

Quick overview:

  1. Split the domain into the second level and top level.
  2. Split the birthdate into month and year.
  3. Calculate the shift value.
  4. Determine whether the length of the SLD is prime. Since the segment cannot be longer than 63 characters (see also RFC 1035), I think it was shorter just to hardcode the list of primes :) Using the regex method suggested.
  5. Define a scriptblock (anonymous function essentially) to perform the Caesar cipher.
  6. Apply the cipher to the SLD, first, and last names
  7. Get the symbol with a hashtable and indexing funny business.
  8. Finally, a giant mess where I apply the appropriate template, run a replace for the consonant triplets, replacing each one with a subexpression (code) that will replace that section with the appropriate value, then run the resulting string through Invoke-Expression (iex) to execute the generated emebedded code, then a final replace of consecutive letters with a single letter.
\$\endgroup\$
2
  • \$\begingroup\$ @TimmyD wow I never saw that before, and it's brilliant. Thanks! \$\endgroup\$
    – briantist
    Dec 16, 2016 at 14:53
  • \$\begingroup\$ @TimmyD you know, I originally tried to write it with unary -join and I could have sworn it was joining with spaces and not $nulls, which surprised me, but having just tried it again it worked as I originally expected. Weird (and thanks again)! \$\endgroup\$
    – briantist
    Dec 16, 2016 at 21:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.