17
\$\begingroup\$

Inspired by this badly received migrated post.

The head of security at your company is concerned about your PIN generating system after it gave him 12345. He also didn't really appreciate the Spaceballs joke you made at his expense, so you've been delegated to re-write the PIN generator. Since it has to fit on some special hardware, you have to make it as small as possible.

Your Task

  • You take two inputs - # of PINs, and PIN size in digits.
  • Generate the specified number of PINs of the specified size randomly and print it out.
  • All valid PIN's of the size must be possible to be printed out, even if they aren't uniformly probable.
  • However, there are some restrictions on the PINs - here are the invalid ones:

    1. If all the pairs are the same digit: 114422 (Note: this will obviously include all same digit PINs).
    2. Increasingly linear PINs (mod 10): 246802.
    3. All groups of 3 are physical lines on the keypad of 1 2 3;4 5 6;7 8 9;bksp 0 enter;: 147369.
    4. The PIN can be entirely split up into groups from rule 1 and rule 3.


  • This is , so shortest code in bytes wins!
\$\endgroup\$
  • \$\begingroup\$ Does rule one also include a single double pair (like 55123)? \$\endgroup\$ – mınxomaτ Oct 12 '15 at 18:40
  • \$\begingroup\$ @minxomat good point, ammending the rules to include both groups. That one will since the second half is 123, but 55432 would be fine. \$\endgroup\$ – Maltysen Oct 12 '15 at 18:42
  • \$\begingroup\$ Does rule 3 include diagonals? \$\endgroup\$ – Martin Ender Oct 12 '15 at 20:57
  • 6
    \$\begingroup\$ The irony is that this definition of a so called "secure" pin only reduces the number of pins attackers might have to brute force! \$\endgroup\$ – DankMemes Oct 13 '15 at 1:36
  • 1
    \$\begingroup\$ What's the shortest length for rule 2? \$\endgroup\$ – Dennis Oct 13 '15 at 3:44
1
\$\begingroup\$

Pyth, 120 bytes

Arz7VGJ1WJK%"%0*d",HO-^TH1=J|q1l{m%-F_vMcd1T.:K2u|GHmu|GHm?qlk2:k"(.)\\1"0?qlk3}k+++=bcS"123456789"3_Mb.Tb_M.TbZdZ./K0)K

Figured I should add a real implementation then. Generates random numbers until one is found that meets all requirements. Can probably be improved a lot!

Online version

\$\endgroup\$
  • 1
    \$\begingroup\$ I have changed the rules to include the requirement that PINs must be random, and all PINs must be possible. \$\endgroup\$ – Maltysen Oct 12 '15 at 20:42
  • \$\begingroup\$ I thought you might :) \$\endgroup\$ – user46060 Oct 12 '15 at 20:50
0
\$\begingroup\$

Perl 5, 244

Starts with generating random numbers for the given size.
And only prints those that don't meet the restrictions.

Finding a solution for the keypad lines (without hardcoding combinations) was kinda fun.

($k,$l)=@ARGV;$m=10**$l-1;while($n<$k){$_=sprintf("%0".$l."d",int(rand($m)));@N=split//,$_;pop@N;$i=$d=0;while(++$i<@N&&$d<1){$d=$N[$i-1]<=>$N[$i]}$b=$_;$b=~s|\d|@A=split//,$';2*$A[0]-$&-$A[1]==0|eg;if((!m/(\d)\1/)&$b>0&$d>=0){$n++;print$_.$/}}

Test

$ perl gen_pins.pl 10 5
98121
15931
69042
93730
83458
25312
24601
49468
49490
67012
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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