1
\$\begingroup\$

I was doing the bandit wargame from overthewire.org and because I am a script kiddie, I googled the answer for level 24.

password="UoMYTrfrBFHyQXmg6gzctqAwOmw1IohZ"
for i in {0000..9999} 
//looping to try any possible pin code
do
echo $password' '$i >> wordlist.txt 
//save password and possible pin code into wordlist 
done

I saw the bash script, and thought, "Well I couldn't have done that because I'm not good at bash." but then I realized that this problem could be done in other languages.

The challenge is simple.

  1. Pipe the password in from stdin.
  2. Append a space in-between the password and the pin or output it separately
  3. Output all PIN combinations. Make sure that there are 4 digits outputted.
  4. BONUS flush the output after each line. This is done automatically with std::endl.

I went ahead and did it in C++

#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
        string key; //1
        getline(cin, key); //I heard that std::getline is
        //only standard for linux, so for practical
        //purposes use cin.getline(char*,size)
        for(int i = 0; i < 10000; i++){   //3
                cout << key << setfill('0') << ' ' //2
                << setw(4) << i << endl;  //4
        }
        return 0;
}

Obviously it can do better than 301 bytes, but you get the gist.

Also any language is acceptable as long as it can produce the same formatted strings separated by new lines or line breaks.

\$\endgroup\$
  • 8
    \$\begingroup\$ I would suggest removing rule 4. It doesn't add anything to the challenge, and anyway only makes sense for languages which have any notion of buffering/flushing output. \$\endgroup\$ – Sanchises Aug 6 at 11:17
  • \$\begingroup\$ @Sanchises Actually it "makes sense" for every programs that prints to stdout on Linux, but I agree with the point. \$\endgroup\$ – user202729 Aug 6 at 12:06
  • 1
    \$\begingroup\$ What does "or output it separately" mean in practice? (May we use multiple spaces? May we use any other consistent string of characters? May we use the password itself? May we output the password 10000 times and then the pins? ...) \$\endgroup\$ – Jonathan Allan Aug 6 at 18:43
  • 1
    \$\begingroup\$ Also for the output it separately thing, I said that because you could just do key += " " and not have to worry about it, but I chose to output it separately. \$\endgroup\$ – Leif Messinger LOAF Aug 6 at 21:27
  • 2
    \$\begingroup\$ That doesn't answer @JonathanAllan's question of what "output it separately" means here. So, for that reason, I'm VTCing as unclear. \$\endgroup\$ – Shaggy Aug 6 at 21:52
2
\$\begingroup\$

05AB1E, 9 7 bytes

žh4。»

Try it online.

Explanation:

žh       # Push builtin string "0123456789"
  4ã     # Get the cartesian product of 4: ["0000","0001",...,"9998","9999"]
    €    # Map each string to:
     ‚   #  Pair it with the (implicit) input-string
      »  # Join each inner list by spaces, and then all strings by newlines
         # (after which the result is output implicitly)
| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

Charcoal, 15 bytes

E×χφ⁺⁺θ ⭆◧Iι⁴Σλ

Try it online! Link is to verbose version of code. Test case uses shortened password to avoid TIO output limit. Explanation:

 ×χφ            Multiply predefined variables 10 with 1000
E               Map over implicit range
           ι    Current index
          I     Cast to string
         ◧  ⁴   Left-pad to width literal `4`
        ⭆    Σλ Convert non-digits to zeros
    ⁺⁺θ         Concatenate with the password and a space
                Implicitly output each entry on its own line
| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

Pyth, 12 bytes

V^jkUT4++QdN

Try it online!

Explanation

V^jkUT4++QdN
V             : For n in
 ^    4       : repeated cartesian product 4 times of
  jkUT        : "0123456789"
       ++QdN  : output input + ' ' + n
| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Octave, 33 bytes

@(a)fprintf([a,' %04u\n'],0:9999)

Try it online!

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Ruby, 32 bytes

10000.times{|i|puts$_+" %04i"%i}

Suggested by Dingus. Try it online!

->a{(0..9999).to_a.map(&:to_s).map{|x|a+" "+x.rjust(4,"0")}.join("\n")};

Try it online!

| improve this answer | |
\$\endgroup\$
  • 1
    \$\begingroup\$ 32 bytes with -n flag. Generally speaking you should try to avoid to_a and to_s. For instance, (0..9999).to_a can just be [*0..9999]. \$\endgroup\$ – Dingus Aug 6 at 11:58
  • \$\begingroup\$ Thanks for the advice. \$\endgroup\$ – Razetime Aug 6 at 11:59
1
\$\begingroup\$

Japt -R, 11 bytes

Takes input as a single element array. If we can use comma as the delimiter instead of space, the last 3 characters can be removed.

ïL²o ùT4)m¸

Try it

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Jelly, (8?) 10 bytes

8 if we may output delimited by multiple spaces (replace K€Y with G).

ØDṗ4ɠ,ⱮK€Y

Try it online!

How?

ØDṗ4ɠ,ⱮK€Y - Main Link: no arguments
ØD         - digit characters = "0123456789"
   4       - four
  ṗ        - Cartesian power -> list of all pins
    ɠ      - read a line from STDIN
      Ɱ    - map (across the pins) applying:
     ,     -   pair
       K€  - join each with a space character
         Y - join with newlines
           - implicit print
| improve this answer | |
\$\endgroup\$

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