49
\$\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\$
21
  • 1
    \$\begingroup\$ You can stash the string however you want (even on the web) I'm not pretty sure that this should be valid. \$\endgroup\$ Commented Jul 5, 2018 at 23:26
  • 9
    \$\begingroup\$ so just echo $@>x and cat x is valid? \$\endgroup\$
    – Adám
    Commented Jul 5, 2018 at 23:30
  • 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
    Commented Jul 5, 2018 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
    Commented Jul 6, 2018 at 0:18
  • 3
    \$\begingroup\$ Somebody needs to write an answer for a computer with magnetic-core memory. \$\endgroup\$
    – Mark
    Commented Jul 6, 2018 at 23:30

41 Answers 41

1
2
1
\$\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\$
1
\$\begingroup\$

Batch - 11 Bytes

%12>f
type f

The input is received as a command-line argument and persists (with the error message created upon execution attempt, since CMD attempts to execute the parameter) in f.


Batch - 7 Bytes (non-competitive)

'>%1
dir

There are a plethora of invalid characters in a file name, so this wouldn't work for some strings, but this essentially saves an arbitrary character to the filename given by the parameter. To read it, it just lists all files in the directory, including our string.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Your solution appears to output on STDOUT, which is valid. STDERR is just another option, not required. The quotes around the input don't stop it from competing. The only question is whether they should be counted in your byte count. I think not, since they aren't specific to the question. They seem like standard boilerplate for Batch to take a command line arg, which isn't so different from languages that need standard command line flags. Maybe someone else with Batch knowledge will chime in. For now leave them out of the byte count. Welcome to Code Golf! :) \$\endgroup\$
    – Jared K
    Commented Jul 24, 2018 at 16:41
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\$
7
  • 1
    \$\begingroup\$ The question asks for storing input from STDIN, not the arguments \$\endgroup\$
    – Ferrybig
    Commented Jul 6, 2018 at 11:38
  • \$\begingroup\$ @Ferrybig How's that \$\endgroup\$ Commented Jul 6, 2018 at 12:48
  • \$\begingroup\$ Wouldn't Read-Host >t and gc t be sufficient? \$\endgroup\$
    – whatever
    Commented Jul 6, 2018 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
    Commented Jul 6, 2018 at 14:05
  • \$\begingroup\$ @JaredK So now that I have revised, this is correct now right? \$\endgroup\$ Commented Jul 6, 2018 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\$

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\$
0
\$\begingroup\$

HP‑41C series, 16 B

The alpha‑register (α‑register) can store up to 24 alphabetic characters. The contents of the α‑register are persistent. You can turn the calculator off and on again, they remain there until overwritten.

01♦LBL "S"         5 Bytes    global label requires 4 + (length of string) Bytes
02 CLA             1 Byte     clear contents of α‑register
03 AON             1 Byte     activate alpha‑keyboard
04 PROMPT          1 Byte     display α‑register and stop program execution
05 RTN             1 Byte     `RTN` does not affect local label search

The CLA (clear alpha) command clears the contents of the α‑register. This is necessary in case of empty input. The subsequent PROMPT command does not change the α‑register in case there is no input at all.

The PROMPT displays the contents of the alpha‑register, and stops the program. The user can enter a string now. Once the R/S (run/stop) key is pressed (≘ end‑of‑file indicator), program execution resumes with the command following the PROMPT command.

01♦LBL "R"         5 Bytes    "S" like "store", "R" like "retrieve"
02 AVIEW           1 Byte     select the α‑register as the current display
03 RTN             1 Byte     this is cheaper than an `END` (3 Bytes)

Keep in mind, if in your program further calculations follow, the AVIEW (alpha view) command by itself does not stop program execution and allow the user to read the display. The AVIEW command is therefore usually paired with other commands such as STOP.

Note, if flag 21 (printer enable) is set, but there is no printer or the printer is off, AVIEW does in fact stop program execution (so the user has a chance to manually record display contents). To resume operations press R/S (run/stop).


Note, leading NULL Bytes cannot be stored and retrieved to and from the α‑register. Moreover, another implementation could use text files. Text files permit storing records of up to 254 alphabetic characters each.
See also HP‑49G RPL submission, a different pocket calculator by the Hewlett‐Packard Company.

\$\endgroup\$
1
2

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.