Interpret the Pig series

13

Given an input of a Pig, SickPig, DeadPig, QuinePig, or DeafPig program, choose one of those "languages" randomly and interpret the input as that "language."

First, randomly choose between one of the five members of the "Pig series":

  • Pig

    If the choice was Pig, mimic the reference interpreter by doing the following:

    • Find the first occurrence of the word PIG in the input (case-sensitive).

    • If the word PIG does not appear in the input, output the message File must contain the string 'PIG'. and exit.

    • Otherwise, split the input string on the first occurrence of PIG. Output the text after the first occurrence of PIG to a file with a filename of the text before PIG.

      PIG may be contained in the text to be output (so, an input of fooPIGbarPIGbaz should output barPIGbaz to a file called foo).

    Note that the reference interpreter takes input via a command line argument that specifies a filename to read from. However, your submission may take input in any of the standard methods accepted on PPCG.

  • SickPig

    If the choice was SickPig, follow the same instructions as Pig. However, instead of writing the text after PIG to the file, choose randomly from the following list

    GRUNT
    MOAN
    OINK
    BURP
    GROAN
    WHINE
    

    and output that to the file instead. This random choice must be independent of the previous choice (so, an output of GRUNT should have a 1/5 * 1/6 = 1/30 chance overall).

  • DeadPig

    DeadPig is like SickPig, but it always outputs the following string instead of randomly choosing a string:

    Your pig has unfortunately died. Please try again.
    
  • QuinePig

    QuinePig is like Pig, but instead of writing the text after PIG to the file, it instead writes the entire input to the file (so, an input of fooPIGbarPIGbaz should output fooPIGbarPIGbaz to a file called foo).

  • DeafPig

    If the choice was DeafPig, do nothing. (The pig is deaf... what do you expect?)

Miscellaneous rules:

  • "Random" means each choice should be roughly equally likely (so, choosing Pig 90% of the time and the other variants only 2.5% of the time is invalid).

  • You may assume that the requested filenames will always be valid for your file system (but they may contain spaces, etc.).

  • For all the variants of Pig, your code may optionally output a single trailing newline to the file as well.

  • Since this is , the shortest code in bytes will win.

Doorknob

Posted 2016-02-04T02:02:18.500

Reputation: 68 138

2+1 what do you expect? – Dennis – 2016-02-04T02:48:33.153

Does the random choice have to be perfectly uniform or is modding 32768 acceptable? – Dennis – 2016-02-04T02:56:59.797

@Dennis Perfect uniformity is not necessary, but each choice must have a roughly equal probability (so mod a big number is okay). – Doorknob – 2016-02-04T02:58:37.787

Woah... I wrote that Pig interpreter... – LegionMammal978 – 2016-02-05T01:13:19.097

Answers

1

Pyth - 157 bytes

Will be doing string compression.

?}J"PIG"z?=GO[jJtKczJOc"GRUNT MOAN OINK BURP GROAN WHINE"d"Your pig has unfortunately died. Please try again."z0).wGhK.q"File must contain the string 'PIG'."

Doesn't work online cuz of file I/O, but try it outputting [content, filename] to stdio here.

Maltysen

Posted 2016-02-04T02:02:18.500

Reputation: 25 023

6

Bash, 251 246 bytes

r=$RANDOM
((r%5<4))||exit
[[ $1 =~ PIG ]]||(echo "File must contain the string 'PIG'.";exit)
s=(GRUNT MOAN OINK BURP GROAN WHINE)
m=("${1#*PIG}" ${s[r%6]}
"Your pig has unfortunately died. Please try again." "$1")
echo -n "${m[r%5]}">"${1%%PIG*}"

This would be a lot shorter if deaf pigs could at least read...

Dennis

Posted 2016-02-04T02:02:18.500

Reputation: 196 637

4

Python 2, 296 286 278 bytes

def g(p):
 import random;f=random.randint;r=f(0,4);i=p.find("PIG")
 if r:
    if i+1:open(p[:i],"w").write([0,p[i+3:],["GRUNT","MOAN","OINK","BURP","GROAN","WHINE"][f(0,5)],"Your pig has unfortunately died. Please try again.",p][r])
    else:print"File must contain the string 'PIG'."

The last two lines start with a tab, instead of the rendered 4 spaces.

Takes input program as function argument.

Denker

Posted 2016-02-04T02:02:18.500

Reputation: 6 639

Hello, when I count the bytes in your submission, I get 317. How are you counting the bytes? – Ogaday – 2016-02-04T12:11:06.157

1@Ogaday The four spaces preceding the last two lines are actually tab characters. (Stack Exchange replaces tabs with spaces within code blocks, though.) – Doorknob – 2016-02-04T12:26:00.493

@Doorknob Ah, ok! That would be it. I wondered why the spaces hadn't been golfed out. – Ogaday – 2016-02-04T12:30:24.773

There should be a period after 'PIG'. – LegionMammal978 – 2016-02-05T01:16:10.923

@LegionMammal978 Thanks for the hint, complety missed that. – Denker – 2016-02-05T07:43:50.507

2

Batch, 409 406 405 bytes

@echo off
set/ar=%random%%%5
if 0==%r% exit/b
set p=x%1
set q=%p:*PIG=%
if %q%==%p% echo File must contain the string 'PIG'.&exit/b
set p=%1
call set p=%%p:PIG%q%=%%
goto %r%
:1
echo %q%>%p%
exit/b
:2
for %%a in (GRUNT.0 MOAN.1 OINK.2 BURP.3 GROAN.4 WHINE.5)do if %%~xa==.%time:~6,1% echo %%~na
exit/b
:3
echo Your pig has unfortunately died. Please try again.>%p%
exit/b
:4
echo %1>%p%

Sadly %p:*PIG=% fails if p is blank, thus the x%1 hack. call set is a nice way to avoid enabledelayedexpansion that I found on Stack Overflow; while the %%~xa==. was a flash of inspiration on my part.

Edit: Saved 3 bytes thanks to @CᴏɴᴏʀO'Bʀɪᴇɴ. Saved 1 byte thanks to @EʀɪᴋᴛʜᴇGᴏʟғᴇʀ.

Neil

Posted 2016-02-04T02:02:18.500

Reputation: 95 035

Why @echo on? Isn't that implicitly done? Perhaps you meant @echo off? – Conor O'Brien – 2016-02-04T12:27:30.543

Also, for generating a random number, SET/A r=%RANDOM%%%5 is shorter. – Conor O'Brien – 2016-02-04T12:28:51.787

@CᴏɴᴏʀO'Bʀɪᴇɴ Ugh, I meant @echo off but I had been debugging... also thanks for the %RANDOM% tip, I hadn't heard of that one. – Neil – 2016-02-04T13:11:03.783

@CᴏɴᴏʀO'Bʀɪᴇɴ Sadly the %RANDOM% version requires me to use an extra set /a which ends up being 10 bytes longer. – Neil – 2016-02-04T13:15:56.527

But you're already using set/a...? – Conor O'Brien – 2016-02-04T13:16:04.283

Not the second time. – Neil – 2016-02-04T13:16:33.193