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
| improve this question | | | | |
\$\endgroup\$
  • 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 Apr 30 '16 at 2:20
  • 1
    \$\begingroup\$ @Doorknob 1. Yes, and 2. That was a mistake, fixed. \$\endgroup\$ – Majora320 Apr 30 '16 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 Apr 30 '16 at 2:42
  • 1
    \$\begingroup\$ Related. \$\endgroup\$ – Martin Ender Apr 30 '16 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\$ – CalculatorFeline Apr 30 '16 at 18:15

10 Answers 10

7
\$\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.
| improve this answer | | | | |
\$\endgroup\$
  • \$\begingroup\$ I've always wondered what "mold" means... \$\endgroup\$ – Leaky Nun Apr 30 '16 at 4:15
  • 18
    \$\begingroup\$ @KennyLau I think it means it's time to throw out the bread. \$\endgroup\$ – Alex A. Apr 30 '16 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)

| improve this answer | | | | |
\$\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.
| improve this answer | | | | |
\$\endgroup\$
  • 2
    \$\begingroup\$ I guess you're a bit "jelly" of Dennis' answer? \$\endgroup\$ – Dennis van Gils Apr 30 '16 at 17:26
  • \$\begingroup\$ Jelly is evil.. \$\endgroup\$ – CalculatorFeline Apr 30 '16 at 18:54
3
\$\begingroup\$

Pyth, 10 bytes

s@Ljb.z*3l

Try it online!

| improve this answer | | | | |
\$\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.

| improve this answer | | | | |
\$\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
| improve this answer | | | | |
\$\endgroup\$
  • \$\begingroup\$ What's cycle? \$\endgroup\$ – CalculatorFeline Apr 30 '16 at 16:28
  • \$\begingroup\$ @CatsAreFluffy cycle takes a list and repeats it infinitely. Eg cycle "hi" == "hihihihi.. \$\endgroup\$ – Michael Klein Apr 30 '16 at 16:31
  • \$\begingroup\$ Have you considered $? \$\endgroup\$ – CalculatorFeline Apr 30 '16 at 16:36
  • \$\begingroup\$ @CatsAreFluffy I'm not sure where I'd use it, what were you thinking? \$\endgroup\$ – Michael Klein Apr 30 '16 at 18:53
3
\$\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)

| improve this answer | | | | |
\$\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!.

| improve this answer | | | | |
\$\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, γ)
| improve this answer | | | | |
\$\endgroup\$
2
\$\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]
| improve this answer | | | | |
\$\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.