| Bytes | Lang | Time | Link |
|---|---|---|---|
| 147 | Javascript | 240826T170324Z | ThePlane |
| 146 | Zsh + sed | 230327T000631Z | roblogic |
| 136 | PHP | 161220T015016Z | Titus |
| 328 | PHP | 160404T205441Z | St3an |
| 116 | JavaScript | 140619T122319Z | edc65 |
| 115 | JavaScript ES6 | 160114T175917Z | Mwr247 |
| 053 | CJam | 140619T162350Z | Dennis |
| 103 | J | 140619T163233Z | seequ |
| 115 | Ruby | 140621T155411Z | bjhaid |
| 138 | Python3 | 140620T171448Z | idiot.py |
| nan | 140621T004106Z | Steven L | |
| 060 | Perl | 140619T111515Z | primo |
| 053 | GolfScript | 140619T205632Z | Runer112 |
| 190 | Python 190 Bytes | 140619T221732Z | 0xhughes |
| 184 | Perl | 140619T095517Z | Martin E |
| 140 | Ruby | 140619T181251Z | KChaloux |
Javascript: 151 147 bytes
(With the added ability to change insults!)
changed so that it defaults to "DIE IN A GREASE FIRE"
(p,i="DIE IN A GREASE FIRE",n=0,s=1,r="")=>([...p].map(l=>(l==i[n]&&s?(l!=" "?r+=i[(s=0,n-1)]==" "?`
`:`
`:0,n++):0,/[.?]/.test(l)?s=1:0,r+=l)),r)
readable version:
function secretInsult(passage, insult) {
// initialize variables
let index = 0;
let startOfSentence = true;
let result = "";
// loop over every letter
for(let letter of passage) {
// if it's the current letter, and we are at the start of a sentence
if(letter == insult[index] && startOfSentence) {
// skip spaces
if(letter != " ") {
// add two newlines for the start of new words
if(insult[index-1] == " ") {
result += "\n";
}
result += "\n";
startOfSentence = false;
}
index++;
}
if(/[.?]/.test(letter)) {
// this is the end of a sentence.
// the next letter will be a start of a sentence.
startOfSentence = true;
}
result += letter;
}
return result;
}
Does have more bytes than previous JS answers, but it does have the added functionality of other insults.
Zsh + sed, 167 158 146 bytes
cp a z;b=DIE_IN_A_GREASE_FIRE
for c (${(s::)b}){sed -i "$ s/ $c/ \n$x$c/" a
((#c==95))&&x=\\n||x=;}
$b()<a;`cut -c -1 a|sed 's/^$/_/g'|rs -g0`||<z
The long email one-liner is stored in file a. Using sed -i '$ <blah>' always edits the last line of a. The last line $ is incrementally reduced as we insert \n characters. The variable $x inserts an extra \n when the current letter $c of the insult string $b is an underscore.
Without the extra line breaks and validation code, it's only 64 bytes.
b=DIEINAGREASEFIRE;for c (${(s::)b})sed -i "$ s/ $c/ \n$c/" a
<a
Old versions that don't fully meet the requirements:
106 bytes
115 bytes
146 bytes
153 bytes
167 bytes
PHP, 136 bytes
for($f=" ".join(file(F));$c=DIEINAGREASEFIRE[$i++];){while(($p=strpos($f,$c,$p))&&$f[$p-1]>" ");$p?$f[$p-1]="\n":$i=20;}echo$i<20?$f:"";
if the whole insult can be put; prints the modified string with a leading space or linebreak; empty output if not.
Run with -r.
breakdown
for($f=" ".join(file(F)); // read input from file "F"
$c=DIEINAGREASEFIRE[$i++];) // loop through insult characters:
{
while(($p=strpos($f,$c,$p)) // find next position of $c
&&$f[$p-1]>" "); // ... preceded by a space
$p?$f[$p-1]="\n" // if found, replace the space with a newline
:$i=20; // else break the loop
}
echo$i<20?$f // if modified text has the full insult, print it
:""; // else print nothing
PHP, 328 bytes
Given a file named 'G' containing the raw text to "enmessage"
<?php
$a=implode(file("G"));$b=str_split('DIEINAGREASEFIRE');foreach(array_unique($b) as $c){foreach(str_split($a) as $h=>$d){if($c==$d)$l[$c][]=$h;}}$e=-1;$n=$a;foreach($b as $f=>$c){foreach($l as $j=>$m){if($c==$j){foreach($m as $k=>$h){if($h>$e){$n=substr($n,0,$h)."\n".$c.substr($a,$h+1);$e=$h+2;break 2;}}}}}echo nl2br($n);
Explanation (~ungolfed && commented code) :
<?php
$string=implode(file("G")); // raw text to deal with
$msg=str_split('DIEINAGREASEFIRE'); // hidden message (make it an array)
// 2D array : [letters of the message][corresponding positions in txt]
foreach(array_unique($msg) as $letter) {
foreach (str_split($string) as $pos=>$let) {
if ($letter==$let) $l[$letter][]=$pos; //1 array per seeked letter with its positions in the string
}
}
$currentPos=-1;
$newString=$string;
foreach ($msg as $key=>$letter) { // deal with each letter of the desired message to pass
foreach($l as $cap=>$arrPos) {// search in letters list with their positions
if($letter==$cap) { // array of the current parsed letter of the message
foreach($arrPos as $kk=>$pos) { // see every position
if ($pos>$currentPos) { // ok, use the letter at that position
$newString=substr($newString,0,$pos)."\n".$letter.substr($string,$pos+1); // add line break
$currentPos=$pos+2; // take new characters into account (\n)
break 2; // parse next letter of the message
}
}
}
}
} /* (note that I could have added some other (random) line breaks management, so that
* the message is not TOO obvious... !*/
echo nl2br($newString);
JavaScript 116
Javascript implementation of m-buettner's idea
console.log((RegExp('^('+[...'DIE IN A GREASE FIRE$'].join('.*)(')+')').exec(z=prompt())||[,z]).slice(1).join('\n'))
Test snippet
z="Dear Boss, how are things? It has come to my attention that I received all the blame for the mishap last Friday. Not just the majority of it. Every last bit of it. Is it wrong for me to think that the rest of the team were at least in part responsible? After all, all six of us were involved from the get-go. Not that I think I should stand without blame. Not at all. All I'm saying is this: I do my best. I try hard. I improve constantly. And I am constantly taking responsibility. Generally speaking, I am very okay with taking full responsibility for my actions. But after this spring, it seems I get more than I deserve. Remember the Flakenhauser contract? Everything went down just about as smooth as one could have hoped. Or so it seemed at first. It was just at the very last minute that things fell apart. All of the team agreed that it was more akin to a freak accident than sloppy planning or mismanaged resources. Still, I - alone - took blame for it. Even though I said nothing then, my tolerance level for taking the blame took a serious dent then. From that point on, I have felt it necessary to always try twice as hard, just to escape scrutiny. And still, here we are again. In spite of all my accomplishments. Right where we always seem to end up these days. Every single project. It's becoming unbearable.";
T1.textContent = (RegExp('^('+[...'DIE IN A GREASE FIRE$'].join('.*)(')+')').exec(z)|| [,z]).slice(1).join('\n')
z='From that FIFA point on'
T2.textContent = (RegExp('^('+[...'DIE IN A GREASE FIRE$'].join('.*)(')+')').exec(z)|| [,z]).slice(1).join('\n')
pre { border: 1px solid black }
T1<pre id=T1></pre>
T2 (the FIFA test)<pre id=T2></pre>
JavaScript (ES6), 93 115 bytes
alert(prompt(x=[...'DIEI1NA1G1REASEF1IRE',i=0]).replace(/\b./g,a=>i<20&&a.match(x[i])?(+x[++i]?i++&&`
`:`
`)+a:a))
I used replace to step through the string, with a match for /\b./g so as to only find characters that either followed a space or began the string. Then I checked each character to see if it matched the current index in the array I was looking for, and added a newline before it if it did, and incremented i.
EDIT: Missed the need to create newlines between words. I've done that now, which brings it up to 115.
CJam, 56 53 bytes
q_"DIEIINAAGGREASEFFIRE"{S@:A+S@+#:BA<AB>}%(!B)*\N*@?
How it works
q_ Read all input from STDIN and push a copy.
"DIEIINAAGGREASEFFIRE" Push the characters we're searching for in form of a
string. We'll try to prepend a linefeed to all of them.
Some characters are duplicated to prepend two linefeeds.
{ For every character C in that string:
S@ Push ' ' and rotate the string on the stack on top of it.
:A Save the string (initially the input) in A.
+ Prepend the space to A.
S@+ Construct the string " C".
# Compute the index of " C" in the modified A.
:B Save the index in the variable B.
A< Push the substring of A up to the index.
AB> Push the substring of A after the index.
}%
( Shift the first element of the resulting array.
! Compute the logical NOT. This pushes 1 if and only if the
array's first element is an empty string, which is true
if and only if the input string started with a D.
B) Push the last value of B and increment. If the last match
was successful and, therefore, all matches were successful,
B != -1, so B + 1 != 0.
* Compute the product of the two topmost items on the stack.
\ Swap the resulting Booleanr with the array of substrings.
N* Join the array, separating by linefeeds.
@ Rotate the input string on top of the stack.
? Select the joined array if the Boolean is 1 and the input
string otherwise.
J - 110 103 bytes
Why doesn't J have good functions to handle strings, but only array functions? I'll revise this if I figure out something smart.
Edit: Shortened and fixed output (it had extra spaces before) and checking. I also improved the explanation.
f=:[:>,&.>/@('IEIINAAGGREASEFFIRE'&t=:(}.@[t{.@[(}:@],(((0{I.@E.)({.;LF;}.)])>@{:))])`]@.(0=#@[))@< ::]
Explanation:
I'll denote function usage as
[<left argument>] <function name> <right argument>, e.g.f <text>. I also won't explain every detail because the function is quite long.
@applies the right function to the left function, eg.f@g x == f(g(x))
t=:(}.@[t{.@[(}:@],(((0{I.@E.)({.;LF;}.)])>@{:))])']@.(0=#@[)is a recursive function<delimiters> t <boxed string>that splits the string with every delimiter, in order. Fails if some delimiter is not found.
(((0{I.@E.)({.;LF;}.)])>@{:)splits the string from the left side of the delimiter, adding a linefeed between them.
>@{:gets the last string from a list (the one which hasn't been split yet)
0{I.@E.gets the index to split at, failing if the delimiter doesn't exist.
{.;LF;}.concatenates left side of the split, linefeed and right side of the split
}:@],concatenates results of the earlier splits and the result of the last function (last meaning: look up)
']@.(0=#@[)checks if there's any delimiters left, and calls the function described above (5 lines) if there is. Otherwise returns.
'IEIINAAGGREASEFFIRE'&sets the left argument oftto that string
[:>,&.>/@joins the results of splitting
::]if something fails (I made the search for the split index the weak point), return the original string.
Examples (too long?):
f 'Dear Boss, how are things? It has come to my attention that I received all the blame for the mishap last Friday. Not just the majority of it. Every last bit of it. Is it wrong for me to think that the rest of the team were at least in part responsible? After all, all six of us were involved from the get-go. Not that I think I should stand without blame. Not at all. All I''m saying is this: I do my best. I try hard. I improve constantly. And I am constantly taking responsibility. Generally speaking, I am very okay with taking full responsibility for my actions. But after this spring, it seems I get more than I deserve. Remember the Flakenhauser contract? Everything went down just about as smooth as one could have hoped. Or so it seemed at first. It was just at the very last minute that things fell apart. All of the team agreed that it was more akin to a freak accident than sloppy planning or mismanaged resources. Still, I - alone - took blame for it. Even though I said nothing then, my tolerance lev...
Dear Boss, how are things?
It has come to my attention that I received all the blame for the mishap last Friday. Not just the majority of it.
Every last bit of it.
Is it wrong for me to think that the rest of the team were at least in part responsible? After all, all six of us were involved from the get-go.
Not that I think I should stand without blame. Not at all.
All I'm saying is this: I do my best. I try hard. I improve constantly. And I am constantly taking responsibility.
Generally speaking, I am very okay with taking full responsibility for my actions. But after this spring, it seems I get more than I deserve.
Remember the Flakenhauser contract?
Everything went down just about as smooth as one could have hoped. Or so it seemed at first. It was just at the very last minute that things fell apart.
All of the team agreed that it was more akin to a freak accident than sloppy planning or mismanaged resources.
Still, I - alone - took blame for it.
Even though I said nothing then, my tolerance level for taking the blame took a serious dent then.
From that point on,
I have felt it necessary to always try twice as hard, just to escape scrutiny. And still, here we are again. In spite of all my accomplishments.
Right where we always seem to end up these days.
Every single project. It's becoming unbearable.
f 'Die in a grease fire. It''s fun. Every time.'
Die in a grease fire. It's fun. Every time.
f 'hai'
hai
Ruby 115
n,a,c,i=[4,6,7,13],gets,'',0;a.chars{|x|x=='DIEINAGREASEFIRE'[i]?(i+=1;c+="\n"if n-[i]!=n;c+="\n#{x}"):c+=x};puts c
Ruby 95
a,c,i=gets,"",0;a.chars{|x|x=="DIEINAGREASEFIRE"[i]?(i+=1;c+="\n#{x}"):(c+=x)};puts c
Python3 (166 138)
Golfed:
s='';o=input()
for n in o.split():s+=[n,'\n'+n.title()][n[0]==("DIEINAGREASEFIRE"*len(o))[s.count('\n')]]+' '
print([o,s][s.count('\n')==16])
Ungolfed-ish:
format_s = ''
unformat_s = input()
for n in unformat_s.split():
format_s += [n, '\n' + n.title()][n[0] == ("DIEINAGREASEFIRE"*len(unformat_s))[format_s.count('\n')]] + ' '
print([unformat_s, format_s][format_s.count('\n') == 16])
Although the use of the lambda pleases me somewhat, that mass number of variables used and the somewhat messy development has the opposite effect. Regex may have been a good idea too. Hay ho, at least it works :).
Edit: replaced lambda variable with count builtin, and shortened split statement.
Python3 (165)
def c(m,h,l=[]):
if h:s=m.rindex(h[0]);l=[m[s:]]+l;return c(m[:s],h[1:],l)
return[m]+l
i=input()
try:print('\n'.join(c(i,"ERIF ESAERG A NI EID")))
except:print(i)
Ungolfed
def headings_remaining(headings): return len(headings) > 0
def head(s): return s[0]
def tail(s): return s[1:]
def prepend(l,e): l.insert(0, e)
def reverse(s): return s[::-1]
def chunk(message,headings,output_list=[]):
if headings_remaining(headings):
split_index = message.rindex(head(headings))
message_init = message[:split_index]
message_last = message[split_index:]
prepend(output_list, message_last)
return chunk(message_init, tail(headings), output_list)
else:
prepend(output_list, message)
return output_list
input_message=input()
try:
headings=reverse("DIE IN A GREASE FIRE")
print('\n'.join(chunk(input_message,headings)))
except ValueError: # Couldn't keep splitting chunks because didn't find heading
print(input_message)
Explanation
chunk recursively splits off the end of the message containing the last heading and prepends it to a list.
Caveat: this won't work if there are no spaces at all between two proposed headings in your letter for some reason, but that seems unlikely in a letter to your boss.
Perl - 60 bytes
#!perl -p
$_=(join$/,$_=~('DIE\IN\A\GREASE\FIRE'=~s/./($&.*)/gr))||$_
Counting the shebang as one.
This solution uses the innermost string to build up the following regex:
(D.*)(I.*)(E.*)(\.*)(I.*)(N.*)(\.*)(A.*)(\.*)(G.*)(R.*)(E.*)(A.*)(S.*)(E.*)(\.*)(F.*)(I.*)(R.*)(E.*)
which is similar to the regex used in m.buettner's solution. The generated regex is then matched against the input. In a list context, this will return an array containing each of the match groups, which are joined together by a newline (the reason for the 'match nothing' groups (\.*), is to insert an additional newline). If there is no match, the original string is output instead.
Perl - 73 bytes
#!perl -pl
$s=$_}for(map$s!~s/.*?(?=$_)//?$\='':$&,'DIEI.NA.G.REASEF.IRE$'=~/./g){
Counting the shebang as two.
This splits up the string at the appropriate delimiters, and collects the pieces in an array. If any of them fail to match, the output record separator (which was set to a newline with the -l option) is unset, and so the string is output unmodified.
GolfScript, 53 bytes
Looks like Dennis and I came up with pretty similar things in parallel... But here's my attempt.
Click on the links before each code block to try it online. Unfortunately, the online interpreter won't allow you to run code larger than 1024 characters, so I had to perform some... "compression" of the test input. But it does still work.
..(\;68=(>"IEIINAAGGREASEFFIRE"{1$.@?:^<n@^>}/](]^0<=
. # (Save input)
.(\; # Get first character
68=(> # If 'D', work with input; else, work with one-character string
"IEIINAAGGREASEFFIRE"
{ # For each character
1$.@ # (Duplicate message twice)
?:^ # Find first index of character
<n # Extract part before index and add a newline
@^> # Extract part including and after index as new "message"
}/ # (Close loop)
](]^0<= # If last line was successfully matched, select result; else, select input
A point raised by Dennis: All GolfScript programs print an automatic final newline. Whether or not this should invalidate my solution as-is, I'm not sure. I believe it would cost 4 characters to suppress the final newline by adding "":n somewhere near the end.
Python - 190 Bytes
l,r="IExINxAxGREASExFIRE","\n";k=p=raw_input();y=i=0
for c in l:
if c!="x":
h=len(p);x=p[y:h].find(c)
if x==-1:i+=1
p=p[0:x+y-1]+r+p[x+y:h];y=x+y;r="\n"
else:r=r+r
if i!=0:p=k
Ungolfed
This is my first golf attempt :) Looking to learn some great coding techniques, regardless, I just focused on using find and then some string splicing to find the appropriate characters and format the output.
Variables -
l.r = l is assigned characters we will be using as our guide to create our new formatted paragraph. r is assigned the new line character for us in spacing the new output.
k,p = The input paragraph. K is used to revert to the original as it is not reassigned during execution of the script. I check against x to know when to add a double new line for spacing purposes.
y,i = y is a "cursor" of sorts, keeps track of the last position a character was found so we find through the paragraph correctly for splicing purposes, i is sanity check, if we don't hit all of our characters we revert the paragraph (p variable) to it's original input via the k variable.
h = Input length, which we use in splicing.
x = The position of the current character represented by C, used for splicing as well.
c = Characters in l to iterate and search for.
The following code is reworded and broken out from the original code for readability to as what is happening:
letters,return="IExINxAxGREASExFIRE","\n"
input1=input2=raw_input()
lastpos=sanity=0
for char in letters:
if char != "x":
inputlength=len(input1)
charposition=input1[lastpos:inputlength].find(char)
if charposition==-1:
sanity+=1
input1=input1[0:position+lastpos-1]+return+input1[position+lastpos:inputlength]
lastpos=position+lastpos
return="\n"
else:return=return+return
if sanity!=0:
input1=input2
I would appreciate your feedback! I am looking to learn.
Perl, 184 bytes
Not a spectacular score for Perl, but here is a simple regex solution
$_=<>;s/^(D.*) (I.*) (E.*) (I.*) (N.*) (A.*) (G.*) (R.*) (E.*) (A.*) (S.*) (E.*) (F.*) (I.*) (R.*) E/$1\n$2\n$3\n\n$4\n$5\n\n$6\n\n$7\n$8\n$9\n$10\n$11\n$12\n\n$13\n$14\n$15\nE/;print;
Ruby - 140
a="DIEINAGREASEFIRE";n=[4,6,7,13];i=0;o='';s=ARGV[0]
s.chars.each{|c|
if c==a[i]
i+=1;o+="\n";o+="\n"if n-[i]!=n
end
o<<c
}
puts((i<16)?s:o)
No regexes in this one. It simultaneously walks the characters in the ARGV[0] input, and a string containing characters we need to break on to make our left-column message. Was originally going to stick spaces after the letters that need to have a newline, but found that it was a bit shorter to hardcode the indexes at which to insert the line break.
Once all is said and done, it checks to see that the i index has incremented enough times to have gone through every letter that needed breaking. If it hasn't, we just print out the original string. If it has, we give 'em the formatted one.
n-[i]!=n was a neat trick to save characters when checking whether or not the current index was one that needed an extra line break (as compared to n.include? i). Also saved some characters by using {}s instead of do/end despite being a multiline block, and used a ternary condition on the final puts to save characters when determining which one to output.
Not the shortest, but I thought it would be neat to do without regular expressions.