48
\$\begingroup\$

Your Programs:

You'll write two programs (both in the same language). The storage program takes a string from STDIN and stores it somewhere persistent (see below) and then exits without error. The retrieval program takes no input, retrieves the string that was stored, and prints it to STDOUT.

Objective test of Persistence:

You should be able to run the storage program on your local machine, then power-cycle your local machine, then call the retrieval program on your local machine. You can stash the string however you want (even on the web) as long as you pass this reboot test.

Test Cases:

Storage then retrieval:

echo foo | Store
Retrieve
foo

Repeated Stores should overwrite (like a set() method):

echo foo | Store
echo bar | Store
Retrieve
bar

Repeated Retrieval is non-destructive (like a get() method):

echo foo | Store
Retrieve
foo
Retrieve
foo

Retrieval before any invocation of Storage:

You don't need to worry about this. Your retrieval program can assume that the storage program has been run at some point in the past.

Input/Output flexibility.

People have asked me to expand this from strict STDIN/STDOUT to the standard IO rules. I can't because it would introduce too many loopholes. Some standard IO options already have the input stored in a persistent way, eg "programs may take input from a file". I'd like to be more flexible than just strict STDIN and STDOUT, but without opening the floodgates.

From the standard IO rules thread I'm cherry-picking the ones that don't break the challenge:

If you use an alternate it must be user-interactive. The user shouldn't have to do any other work besides piping their input to your program, typing it into a prompt your program provides, or typing input as a command-line-arg of your program. The user shouldn't have to do anything other than running your retrieve program to see the output displayed on screen or sent to STDOUT or STDERR.

Allowed assumptions:

  • Your two programs will be run in the same directory
  • Your programs have read-write permissions for that directory
  • Files you create will survive the reboot (not in a temp dir)
  • One trailing newline that wasn't part of the string is allowed. No other trailing whitespace

This is code-golf, and your score is the sum of bytes from both programs.

\$\endgroup\$
  • 9
    \$\begingroup\$ so just echo $@>x and cat x is valid? \$\endgroup\$ – Adám Jul 5 '18 at 23:30
  • 1
    \$\begingroup\$ Your spec doesn't mention the ability to store multiple times, but you do have an example case that does so. May we assume a clean environment for each invocation of Store? \$\endgroup\$ – Adám Jul 5 '18 at 23:40
  • 4
    \$\begingroup\$ @EriktheOutgolfer Why would it not be valid? Even if there is a loophole discussed on meta that doesn't prevent a challenge from specifically allowing it. \$\endgroup\$ – dylnan Jul 5 '18 at 23:45
  • 6
    \$\begingroup\$ Web restrictions are usually about pulling stuff from the web as a loophole to get out of doing the work of creating that stuff. In this case, if it's on the web it's because you did the work of putting it there. \$\endgroup\$ – Jared K Jul 6 '18 at 0:18
  • 3
    \$\begingroup\$ Somebody needs to write an answer for a computer with magnetic-core memory. \$\endgroup\$ – Mark Jul 6 '18 at 23:30

40 Answers 40

1
\$\begingroup\$

Sed, 2+2 bytes:

Stores to file f:

wf

Reads from file f:

rf

Try a demo online!

\$\endgroup\$
0
\$\begingroup\$

SmileBASIC (v3+), 23 + 14 = 37 bytes

Simple filesystem-based answer. Works in all known SmileBASIC environments v3.x or above. Be careful, in case you happen to have an important text file named A!

Store, 23 bytes

LINPUT S$SAVE"TXT:A",S$

Retrieve, 14 bytes

?LOAD("TXT:A")

SmileBASIC (v2), 21 + 16 = 37 bytes

Adjusted to work on old SmileBASIC (Petit Computer/mkII for DSi).

Store, 21 bytes

LINPUT MEM$SAVE"MEM:A

Retrieve, 16 bytes

LOAD"MEM:A
?MEM$

I'm honestly kinda amazed both versions work out to the same total byte count.

\$\endgroup\$
0
\$\begingroup\$

Cgam, 7 bytes

Store, 4 bytes

qSow

Retrieve, 3 bytes

Sog

Unfortunately, there is not yet a web interpreter, all you can do is download the code, build it, and run it yourself...

\$\endgroup\$
0
\$\begingroup\$

Powershell | 26 bytes

Store (store.ps1 | 24 bytes)

"`$s='$input'">>$profile

Retrieve (retrieve.ps1 | 2 bytes)

$s

Calling

Read-Host|.\store.ps1

# close powershell, reboot
# open new Powershell

.\retrieve.ps1
\$\endgroup\$
  • 1
    \$\begingroup\$ The question asks for storing input from STDIN, not the arguments \$\endgroup\$ – Ferrybig Jul 6 '18 at 11:38
  • \$\begingroup\$ @Ferrybig How's that \$\endgroup\$ – SomeShinyMonica Jul 6 '18 at 12:48
  • \$\begingroup\$ Wouldn't Read-Host >t and gc t be sufficient? \$\endgroup\$ – whatever Jul 6 '18 at 13:29
  • \$\begingroup\$ my bad about arguments vs STDIN. I said STDIN in the challenge description but then showed arguments in the example cases. It's fixed now. \$\endgroup\$ – Jared K Jul 6 '18 at 14:05
  • \$\begingroup\$ @JaredK So now that I have revised, this is correct now right? \$\endgroup\$ – SomeShinyMonica Jul 6 '18 at 14:09
0
\$\begingroup\$

Java 8, 156 bytes

Both solutions are lambdas taking empty input and throwing java.io.IOException.

Store (93 bytes)

import java.nio.file.*;

v->Files.copy(System.in,Paths.get("a"),StandardCopyOption.values()[0])

Retrieve (63 bytes)

import java.nio.file.*;

v->Files.copy(Paths.get("a"),System.out)
\$\endgroup\$
0
\$\begingroup\$

NodeJS (REPL) v10.0.0, 57 bytes

Store: fs.writeFileSync("a",process.args)

Read: fs.readFileSync("a")+[]

\$\endgroup\$
0
\$\begingroup\$

Mathematica, 23 bytes

InputString[]>>"a"

Put[], abbreviated >> as an infix operator, receives an expression as its left operand, then writes that expression to the filespec given in its right operand, here the file a. InputString[] interactively (graphically or textually, depending on how the kernel is being accessed) receives a string and returns that expression. This store program is 18 bytes.

<<"a"

Get[], abbreviated << as a prefix operator, reads the file specified by its right argument, evaluating each expression in it, then returns the last such expression. This retrieval program is 5 bytes.

You can use Directory[] to find out where the file is stored. The representation stored in the file is quoted, so is equivalent to an expression representing a string.

\$\endgroup\$
0
\$\begingroup\$

JavaScript (Node.js), 129 bytes

Store, 78 bytes

(p=process).stdin.resume().on('data',t=>require('fs').writeFile('_',t,p.exit))

Retrieve, 51 bytes

console.log(require('fs').readFileSync('_','utf8'))

\$\endgroup\$
0
\$\begingroup\$

Pascal (95+92=187)

Store:

program i;var f:Text; s:string;begin Assign(f,'i');Rewrite(f);Read(s);Write(f,s);Close(f);end.

Retrive:

program o;var f:Text;s:string;begin Assign(f,'i');Reset(f);Read(f,s);Write(s);Close(f);end.

You can omit Close(f); in retrive and shorten it a little bit, but, not in a spirit of pascal!

\$\endgroup\$
0
\$\begingroup\$

Yabasic, 36+38=74 bytes

Two scripts that write to the file t in the current directory. TIO Link provided both the getter and setter in both cases

Setter

Input""s$
Open#1,"t","w"
?#1s$
Close#1

Getter

Open#1,"t","r"
Input#1t$
Close#1
?t$

Try it online!

\$\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.