11
\$\begingroup\$

Introduction

hackertyper.net is a website that simulates "hacking" (as the media portrays it) by outputting complicated code from the crypto section of the Linux kernel onto the screen as you type at a rate of 3 characters per one inputted.

Challenge

Your program/function should either accept a file as a command-line argument, have the file path hard-coded, or accept the text [that would be in the file] as a command-line or function argument, and mimic hackertyper.net by outputting 3 characters onto the screen (STDOUT or equivalent) from the file for each one received through STDIN or equivalent.

Once EOF is reached, the program should start outputting characters from the beginning of the file again (wrap).

Specs

For the purposes of simplicity, you can assume that STDIN has already been set to no buffer and no echo mode, that is not waiting until the user presses enter before passing input to your program and not displaying the characters you type.

The file can contain newlines, while the input will not contain newlines.

For a small example written in C (not golfed) that demonstrates how this works, see this.

Green text and a black background are not required.

Example input and output

The file:

#include <stdio.h>
int main() { }
    

Input:

hello world

Output:

#include <stdio.h>
int main() { }

The file:

hello

Input:

hello world

Output:

hellohellohellohellohellohellohel
\$\endgroup\$
9
  • 1
    \$\begingroup\$ 1. Are functions allowed (that take two strings as an argument and return another string)? 2. Your spec says to output three characters for every input, but the examples only output one for every input. \$\endgroup\$
    – Doorknob
    Commented Apr 30, 2016 at 2:20
  • 1
    \$\begingroup\$ @Doorknob 1. Yes, and 2. That was a mistake, fixed. \$\endgroup\$
    – Majora320
    Commented Apr 30, 2016 at 2:23
  • \$\begingroup\$ Do we have to take the file information in as a file, or can we also receive that information through a function argument or equivalent? If we have to, what will the name of that file be? Will it be in a specific directory away from our script(s), or in the same one as our script(s)? \$\endgroup\$
    – R. Kap
    Commented Apr 30, 2016 at 2:42
  • 1
    \$\begingroup\$ Related. \$\endgroup\$ Commented Apr 30, 2016 at 8:54
  • 3
    \$\begingroup\$ Bonuses be bad. And plus very few people will get the penalty, and calling it a "bonus" is confusing. \$\endgroup\$ Commented Apr 30, 2016 at 18:15

13 Answers 13

8
\$\begingroup\$

Jelly, 4 bytes

ẋ3ṁ@

Try it online!

How it works

ẋ3ṁ@    Main link. Arguments: s (input string), t (file string)

ẋ3      Repeat s three times.
  ṁ@    Mold; reshape t like the previous result.
        This repeats the elements of t over and over until the length matches that
        of s repeated thrice.
\$\endgroup\$
2
  • \$\begingroup\$ I've always wondered what "mold" means... \$\endgroup\$
    – Leaky Nun
    Commented Apr 30, 2016 at 4:15
  • 20
    \$\begingroup\$ @KennyLau I think it means it's time to throw out the bread. \$\endgroup\$
    – Alex A.
    Commented Apr 30, 2016 at 6:44
4
\$\begingroup\$

J, 7 bytes

$~(3*#)

Takes two arguments, the text to be repeated and the user's input text.

Usage

The input text is formatted where , means to join and LF is the newline character.

   f =: $~(3*#)
   ('#include <stdio.h>', LF, 'int main() { }') f 'hello world'
#include <stdio.h>
int main() { }
   'hello' f 'hello world'
hellohellohellohellohellohellohel

Try it online. (tryj.tk)

\$\endgroup\$
4
\$\begingroup\$

Jelly, 9 bytes

⁴L×3
ẋ¢ḣ¢

Try it online!

⁴L×3     Define nilad as ¢:

 L       length of
⁴        second argument
  ×3     tripled


ẋ¢ḣ¢     Main chain:
       
         the first argument (implicit)
ẋ        repeated
 ¢       ¢ many times
  ḣ¢     then get the first ¢ characters of it.
\$\endgroup\$
2
  • 3
    \$\begingroup\$ I guess you're a bit "jelly" of Dennis' answer? \$\endgroup\$ Commented Apr 30, 2016 at 17:26
  • \$\begingroup\$ Jelly is evil.. \$\endgroup\$ Commented Apr 30, 2016 at 18:54
4
\$\begingroup\$

Python 3.5, 77 65 63 bytes:

lambda g,f:''.join((g*len(f))[i:i+3]for i in range(0,len(f)*3,3))

Simple enough. An anonymous function which takes in two arguments, the first one being the "file" (g), and the second being the characters typed in by the user (f). Then this creates a generator containing every three characters in g, which are found by indexing for every i and then i+3 characters in g, where i is in the range of 0=>(length of f)*3. Finally, it returns each object in the generator joined into one big string. You call this function by assigning a variable to it, and then calling the variable wrapped inside a print() expression. So if the function's name was q, it would be executed like print(q(<byte array here>)).

Try it online! (repl.it)

\$\endgroup\$
1
  • \$\begingroup\$ Your code is actually still 65 bytes. Here are 63: lambda g,f:''.join((g*len(f))[3*i:3*i+3]for i in range(len(f))) Also, deadlink. \$\endgroup\$
    – movatica
    Commented Apr 14 at 12:03
3
\$\begingroup\$

Pyth, 10 bytes

[email protected]*3l

Try it online!

\$\endgroup\$
3
\$\begingroup\$

JavaScript (ES6), 40 bytes

(s,t)=>s.repeat(l=t.length*3).slice(0,l)

Where s is the data string and t is the user string. Assumes s is nonempty and repeats it l times to ensure that its length is at least l so that it can return the first l characters, where l is three times the length of t.

\$\endgroup\$
3
\$\begingroup\$

Haskell, 25 bytes

First argument is what's "typed", the second the source to display

(.cycle).take.(3*).length

Or non-pointfree, for (possibly) better readability:

h a=take(3*length a).cycle
\$\endgroup\$
4
  • \$\begingroup\$ What's cycle? \$\endgroup\$ Commented Apr 30, 2016 at 16:28
  • \$\begingroup\$ @CatsAreFluffy cycle takes a list and repeats it infinitely. Eg cycle "hi" == "hihihihi.. \$\endgroup\$ Commented Apr 30, 2016 at 16:31
  • \$\begingroup\$ Have you considered $? \$\endgroup\$ Commented Apr 30, 2016 at 16:36
  • \$\begingroup\$ @CatsAreFluffy I'm not sure where I'd use it, what were you thinking? \$\endgroup\$ Commented Apr 30, 2016 at 18:53
3
\$\begingroup\$

Ruby, 39 bytes

In Ruby, $< reads from the supplied file in the command line arguments instead of $stdin if one is given. (If you forget to supply a file, you get a blank output because it reads everything out of $stdin in $<.read and thus STDIN.read.size will be 0.)

$><<($<.read*s=3*STDIN.read.size)[0,s]
\$\endgroup\$
2
\$\begingroup\$

05AB1E, 7 bytes

Code:

3×g©×®£

Explanation:

3×        # Multiply the input string three times.
  g       # Take the length.
   ©      # Copy that to the register.
    ×     # Multiply by the second input.
     ®    # Retrieve the length from the register.
      £   # Only keep [0:length * 3] from the new string.

Try it online!.

\$\endgroup\$
2
\$\begingroup\$

Jolf, 10 bytes

]*iγl*I30γ

Try it here!

Explanation

]*iγl*I30γ
 *i        repeat string 1
    l*I3    the length of the other string * 3
   γ        γ = (^)
]       0γ  (^).slice(0, γ)
\$\endgroup\$
2
\$\begingroup\$

Vyxal, 3 bytes

LTẎ

Try it Online!

  Ẏ # Slice input to length
 T  # 3 *
L   # Length of other input
\$\endgroup\$
2
\$\begingroup\$

Uiua, 4 bytes

↯×3△

Try it!

Reshape the input to 3 multiplied by the shape of the other input.

\$\endgroup\$
1
\$\begingroup\$

This got bumped and piqued my curiosity. I decided to take on the harder version of the challenge, reading in a character at a time.

Zsh, 45 bytes

read -k1&&printf %s ${1::3}&&$0 ${1:3}${1::3}

Recursive call with the text rotated by three characters.

No TIO link, it won't work there because no terminal.

To run this,

  • either save it as a #!zsh executable script, or wrap function declaration such as hacker(){ ... }.
  • Call it with stty -echo; hacker 'text to repeat'; stty echo to properly disable echo.
  • Don't run it for too long, there is no tail call optimization in this version

A proper version with builtin noecho and tail call optimization is 51 bytes:

read -sk1&&printf %s ${1::3}&&exec $0 ${1:3}${1::3}
\$\endgroup\$

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.