7
\$\begingroup\$

The Rules

  • Each submission must be a full program or a function.
  • The program must take all instances of the string 2015 from the input (1 string), replace it with <s>2015</s>* (with <s> or <strike>). At the end, if there has been at least one 2015 in the string, you must add a newline and this string: *2016. I'm a year early!
  • The program must not write anything to STDERR.
  • The input will not contain <s>2015</s> or <strike>2015</strike>.

    Note that there must be an interpreter so the submission can be tested.

  • Submissions are scored in bytes, in an appropriate (pre-existing) encoding, usually (but not necessarily) UTF-8. Some languages, like Folders, are a bit tricky to score - if in doubt, please ask on Meta.
  • This is , so shortest code wins

Examples

Input:

Hello! The date is 2015-12-24.

Output:

Hello! The date is 2015*-12-24.
*2016. I'm a year early!

Input:

foo bar baz quux

Output:

foo bar baz quux

Input:

12320151203205120151294201511271823

Output:

1232015*120320512015*12942015*11271823
*2016. I'm a year early!

Input:

 Hello. The year is 2015...
2015.5...

Output:

 Hello. The year is 2015*...
2015*.5...
*2016. I'm a year early!

The Snippet

The Stack Snippet at the bottom of this post generates the catalogue from the answers a) as a leaderboard and b) as a list of winners per language.

To make sure that your answer shows up, please start your answer with a headline, using the following Markdown template:

## Language Name, N bytes

where N is the size of your submission. If you improve your score, you can keep old scores in the headline, by striking them through. For instance:

## Ruby, <s>104</s> <s>101</s> 96 bytes

If there you want to include multiple numbers in your header (e.g. because your score is the sum of two files or you want to list interpreter flag penalties separately), make sure that the actual score is the last number in the header:

## Perl, 43 + 2 (-p flag) = 45 bytes

You can also make the language name a link which will then show up in the snippet:

## [><>](http://esolangs.org/wiki/Fish), 121 bytes

var QUESTION_ID=68372,OVERRIDE_USER=45162;function answersUrl(e){return"http://api.stackexchange.com/2.2/questions/"+QUESTION_ID+"/answers?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+ANSWER_FILTER}function commentUrl(e,s){return"http://api.stackexchange.com/2.2/answers/"+s.join(";")+"/comments?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+COMMENT_FILTER}function getAnswers(){jQuery.ajax({url:answersUrl(answer_page++),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){answers.push.apply(answers,e.items),answers_hash=[],answer_ids=[],e.items.forEach(function(e){e.comments=[];var s=+e.share_link.match(/\d+/);answer_ids.push(s),answers_hash[s]=e}),e.has_more||(more_answers=!1),comment_page=1,getComments()}})}function getComments(){jQuery.ajax({url:commentUrl(comment_page++,answer_ids),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){e.items.forEach(function(e){e.owner.user_id===OVERRIDE_USER&&answers_hash[e.post_id].comments.push(e)}),e.has_more?getComments():more_answers?getAnswers():process()}})}function getAuthorName(e){return e.owner.display_name}function process(){var e=[];answers.forEach(function(s){var r=s.body;s.comments.forEach(function(e){OVERRIDE_REG.test(e.body)&&(r="<h1>"+e.body.replace(OVERRIDE_REG,"")+"</h1>")});var a=r.match(SCORE_REG);a&&e.push({user:getAuthorName(s),size:+a[2],language:a[1],link:s.share_link})}),e.sort(function(e,s){var r=e.size,a=s.size;return r-a});var s={},r=1,a=null,n=1;e.forEach(function(e){e.size!=a&&(n=r),a=e.size,++r;var t=jQuery("#answer-template").html();t=t.replace("{{PLACE}}",n+".").replace("{{NAME}}",e.user).replace("{{LANGUAGE}}",e.language).replace("{{SIZE}}",e.size).replace("{{LINK}}",e.link),t=jQuery(t),jQuery("#answers").append(t);var o=e.language;/<a/.test(o)&&(o=jQuery(o).text()),s[o]=s[o]||{lang:e.language,user:e.user,size:e.size,link:e.link}});var t=[];for(var o in s)s.hasOwnProperty(o)&&t.push(s[o]);t.sort(function(e,s){return e.lang>s.lang?1:e.lang<s.lang?-1:0});for(var c=0;c<t.length;++c){var i=jQuery("#language-template").html(),o=t[c];i=i.replace("{{LANGUAGE}}",o.lang).replace("{{NAME}}",o.user).replace("{{SIZE}}",o.size).replace("{{LINK}}",o.link),i=jQuery(i),jQuery("#languages").append(i)}}var ANSWER_FILTER="!t)IWYnsLAZle2tQ3KqrVveCRJfxcRLe",COMMENT_FILTER="!)Q2B_A2kjfAiU78X(md6BoYk",answers=[],answers_hash,answer_ids,answer_page=1,more_answers=!0,comment_page;getAnswers();var SCORE_REG=/<h\d>\s*([^\n,]*[^\s,]),.*?([\d.]+)(?=[^\n\d<>]*(?:<(?:s>[^\n<>]*<\/s>|[^\n<>]+>)[^\n\d<>]*)*<\/h\d>)/,OVERRIDE_REG=/^Override\s*header:\s*/i;
body{text-align:left!important}#answer-list,#language-list{padding:10px;width:290px;float:left}table thead{font-weight:700}table td{padding:5px}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link rel="stylesheet" type="text/css" href="//cdn.sstatic.net/codegolf/all.css?v=83c949450c8b"> <div id="answer-list"> <h2>Leaderboard</h2> <table class="answer-list"> <thead> <tr><td></td><td>Author</td><td>Language</td><td>Size</td></tr></thead> <tbody id="answers"> </tbody> </table> </div><div id="language-list"> <h2>Winners by Language</h2> <table class="language-list"> <thead> <tr><td>Language</td><td>User</td><td>Score</td></tr></thead> <tbody id="languages"> </tbody> </table> </div><table style="display: none"> <tbody id="answer-template"> <tr><td>{{PLACE}}</td><td>{{NAME}}</td><td>{{LANGUAGE}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody> </table> <table style="display: none"> <tbody id="language-template"> <tr><td>{{LANGUAGE}}</td><td>{{NAME}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody> </table>

\$\endgroup\$
16
  • 1
    \$\begingroup\$ @downvoters what is wrong with this challenge? \$\endgroup\$
    – user46167
    Jan 2, 2016 at 1:41
  • 16
    \$\begingroup\$ I find the challenge boring. It's a find-replace followed by a conditional concatenated string. Also, I don't like when functions are disallowed for no particular reason. \$\endgroup\$
    – xnor
    Jan 2, 2016 at 1:45
  • 8
    \$\begingroup\$ Yes, but a challenge can be simple without being straightforward. Replacement or regex is a built-in in most langs. It means that's likely just the way to do it. \$\endgroup\$
    – xnor
    Jan 2, 2016 at 1:49
  • 2
    \$\begingroup\$ Can we have any trailing whitespace? \$\endgroup\$
    – Downgoat
    Jan 2, 2016 at 2:30
  • 4
    \$\begingroup\$ Not everyone agrees with @NBZ on that. And there are certainly more than enough trivial questions. If you post questions which have a negative score in the sandbox, don't be surprised to see them downvoted. \$\endgroup\$ Jan 2, 2016 at 14:57

21 Answers 21

13
\$\begingroup\$

JavaScript ES6, 84 76 75 bytes

s=>(q=s.replace(/2015/g,"<s>$&</s>*"))+(s<q?`
*2016. I'm a year early!`:"")

This is pretty well golfed for JavaScript imo.


This works by taking the input and replacing all instances of 2015 with <s>$&</s>* where $& is the value matched, but in this case, that's always 2015. Then, if the input is different from q, it'll add the *2016... string, otherwise, It'll add a blank string (nothing).

\$\endgroup\$
4
  • \$\begingroup\$ I think you can do (q!=s&&`\n*2016. I'm a year early!`) \$\endgroup\$ Jan 2, 2016 at 2:21
  • \$\begingroup\$ @CᴏɴᴏʀO'Bʀɪᴇɴ That seems to add "false" to the string when the condition is false :( \$\endgroup\$
    – Downgoat
    Jan 2, 2016 at 2:28
  • \$\begingroup\$ Oh, dangit. That's bad :P \$\endgroup\$ Jan 2, 2016 at 2:30
  • 1
    \$\begingroup\$ Very interesting. Never knew about $& or any of the other special replacement patterns. \$\endgroup\$ Jan 2, 2016 at 4:11
5
\$\begingroup\$

Ruby, 69 bytes

$><<gets.gsub(/2015/,'<s>\&</s>*')
puts"*2016. I'm a year early!"if$&
\$\endgroup\$
0
4
\$\begingroup\$

Jolf, 60 bytes

Two parts, again.

oHρi"2015"+pd"s'$&'*"
oH                    set H
  ρi"2015"             to input string, replaced all 2015 with
           pd"s'$&'    $& crossed out. (pd is an apply tag function)
          +        *" append an asterisk

    v-- unprintable
+H|&?=Hi+S"*2016. I\'m a year early!'"
+H|&?=Hi                            '" add ↓ to H if H != i; otherwise add empty string ('")
        +S"*2016. I\'m a year early!'  a newline plus that string
                                       implicit printing

Try it here! Bonus points for actually crossing out the text and conforming to the output examples? :3

\$\endgroup\$
1
  • \$\begingroup\$ Technically against the rules. +1. \$\endgroup\$
    – wizzwizz4
    Jan 2, 2016 at 17:29
3
\$\begingroup\$

Python, 97 bytes

a=raw_input();b=a.replace('2015','<s>2015</s>*');print a;
if a!=b:print"*2016. I'm a year early!"

This takes input from stdin, replaces any instances of 2015 with a strike, and prints the 2016 message if the two strings don't match.

\$\endgroup\$
1
  • \$\begingroup\$ couldn't you use a<b instead of a!=b? \$\endgroup\$
    – Cyoce
    Jan 2, 2016 at 20:22
2
\$\begingroup\$

Mathematica, 98 bytes

a="2015";StringReplace[#,a->"<s>2015</s>*"]<>If[#~StringCount~a>0,"
*2016. I'm a year early!",""]&

Once again, Mathematica's string processing uses many bytes...

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

Pyth, 52 51 50 bytes

I-
:z"2015""<s>2015</s>*"z." ytµbðï«»±é¢þnl¶Vû

Test Suite.

\$\endgroup\$
2
\$\begingroup\$

𝔼𝕊𝕄𝕚𝕟, 49 chars / 93 bytes

a=ïę ḟ*ṁ,`<s>$&</s>*”)+(a≠ï?ɘƂ联Ӏఈ쁬Πā␎䁛␐줄쁰ꖈᲠ䉠耀#:⬯

Try it here (Firefox only).

Really straightforward. a=ïę ḟ*ṁ,'<s>$&</s>*”) nests all instances of 2015 (represented here as the result of 31*65) in strikethrough tags. Then, append the last string ('compressed' here) accordingly.

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

Japt, 51 bytes

Y=Ur2015"<s>$&</s>*")¥U?U:Y+`
*2016. I'm a ye e§!

Try it online!

\$\endgroup\$
2
\$\begingroup\$

TeaScript, 59 bytes

(q=xg2015,"<s>$&</s>*"))+(x<q?`
*2016. I'm a year early!`:u

Try it online

Explanation

(q=        // Assign `q` to...
   xg      // input.replace
     2015  // 2015 with.
     "..." // The string
)) +       // Add to..

(x<q?      // If the input is different from replacement
     "..." // Add string to the end
    :u     // Otherwise empty string
\$\endgroup\$
5
  • \$\begingroup\$ Downvote reason? This answer is perfectly valid, wth? \$\endgroup\$
    – Downgoat
    Jan 2, 2016 at 3:51
  • \$\begingroup\$ They'll always downvote downgoat! \$\endgroup\$
    – wizzwizz4
    Jan 2, 2016 at 17:31
  • \$\begingroup\$ @wizzwizz4 :( Maybe Upgoat is having better luck \$\endgroup\$
    – Downgoat
    Jan 2, 2016 at 17:32
  • \$\begingroup\$ How legal are sockpuppet accounts? \$\endgroup\$
    – wizzwizz4
    Jan 2, 2016 at 17:34
  • \$\begingroup\$ @wizzwizz4 I don't use them for evil, >:) just chatbots so it's fine. \$\endgroup\$
    – Downgoat
    Jan 2, 2016 at 17:36
2
\$\begingroup\$

05AB1E, 42 40 bytes

Ž7ç©åi®®"<s>ÿ</s>*".:®>“*ÿ. I'm€…‚Œ‰€!“»

Try it online!

-2 thanks to Grimy

Explanation

Ž7ç©åi®®"<s>ÿ</s>*".:®>“*ÿ. I'm€…‚Œ‰€!“» Full Program
Ž7ç©                                     Pushes 2015 and copies it to the register
    å                                    Checks if 2015 occurs in the input
     i                                   If yes, do the following steps, else
                                          finish and output the input
      ®®"<s>ÿ</s>*"                      Pushes 2015 and "<s>2015</s>*"
                   .:                    Replaces all instances of 2015 by 
                                          </s>2015</s> in the input
                     ®>“*ÿ. I'm€…‚Œ‰€!“  Push "*2016. I'm a year early!"
                                       » Concatenate both strings with a newline in between
\$\endgroup\$
2
2
\$\begingroup\$

Arturo, 83 79 bytes

$[s][r:replace s"2015""<s>2015</s>*"if r<>s['r++"\n*2016. I'm a year early!"]r]
\$\endgroup\$
6
  • 1
    \$\begingroup\$ @Dr.Kameleon Suggested r<>s[suffix'r"\n*2016 ~> r<>s['r++"\n*2016. \$\endgroup\$ Apr 14, 2021 at 11:51
  • \$\begingroup\$ @Dr.Kameleon Please read codegolf.meta.stackexchange.com/questions/1615/…. \$\endgroup\$ Apr 14, 2021 at 11:53
  • \$\begingroup\$ @JonathanFrech I'm not sure I'm too familiar with CodeGolf posting standards, but given that I am the original author of the language (Arturo), I just found this and decided to post my own - slightly shorter - solution. :) \$\endgroup\$ Apr 14, 2021 at 11:58
  • 1
    \$\begingroup\$ @Dr.Kameleon Nice to see you here and thank you for the golf! Here we usually suggest golfs in comments instead of editing the answer, so that, probably why you were received so harshly ;) \$\endgroup\$
    – xigoi
    Apr 14, 2021 at 13:22
  • \$\begingroup\$ @xigoi Well, I guess I know now lol. In any case, great to see an Arturo example here! Needless to say, I was more than pleasantly suprised! :) \$\endgroup\$ Apr 14, 2021 at 14:19
1
\$\begingroup\$

(f)lex, 104 bytes

File 2015.l:

 int n=0;
%%
"2015" ++n+puts("<s>2015</s>*");
%%
main(){yylex();n&&puts( "*2016. I'm a year early!\n");}

...no trailing newline.

Build:

$ flex -o 2015.c 2015.l
$ gcc 2015.c -o 2015 -lfl

Run:

$ ./2015 <<<'Hello! The date is 2015-12-24.'
Hello! The date is <s>2015</s>*-12-24.
*2016. I'm a year early!
$ ./2015 <<<'foo bar baz quux'
foo bar baz quux
$ ./2015 <<<'12320151203205120151294201511271823'
123<s>2015</s>*12032051<s>2015</s>*1294<s>2015</s>*11271823
*2016. I'm a year early!

I ran this using flex and gcc on Debian8, but I see no reason why it should fail in other environments using lex and/or a different C compiler.

\$\endgroup\$
1
  • \$\begingroup\$ Note it may bail out due to integer overflow. \$\endgroup\$ Aug 1, 2019 at 10:45
1
\$\begingroup\$

C#, 152 bytes

class A{static void Main(string[]a){System.Console.Write(a[0].Replace("2015","<s>2015</s>*")+(a[0].Contains("2015")?"\n*2016. I'm a year early!":""));}}

Basic solution. Takes the string as the first argument.

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

Gema, 57 bytes

2015=<s>$0</s>*@set{a;*2016. I'm a year early\!}
\Z=${a;}

Sample run:

bash-4.3$ gema -f not-2015.gema <<< 'Hello! The date is 2015-12-24.'
Hello! The date is <s>2015</s>*-12-24.
*2016. I'm a year early!

bash-4.3$ gema -f not-2015.gema <<< 'foo bar baz quux'
foo bar baz quux

bash-4.3$ gema -f not-2015.gema <<< '12320151203205120151294201511271823'
123<s>2015</s>*12032051<s>2015</s>*1294<s>2015</s>*11271823
*2016. I'm a year early!
\$\endgroup\$
1
\$\begingroup\$

PowerShell, 81 79 bytes

-2 bytes thanks to mazzy

param($n)($z=$n-replace2015,'<s>$&</s>*')+"
*2016. I'm a year early!"*($z-ne$n)

Try it online!

Just a replace on the input and a conditional append with the 2016 line. You don't need many quotes or spaces in PowerShell either.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ 79 bytes :) \$\endgroup\$
    – mazzy
    May 28, 2019 at 18:10
0
\$\begingroup\$

Stax, 36 bytes

ç┼↕+∙n7↔Qq┼╩↓╣ÇÖú];ⁿX£╞bëi⌐ñ▼Z♀φ6╛(<

Run and debug it

Unpacked, ungolfed, and commented, it looks like this.

_               push all of standard input
2015X           push 2015 and store it in the x register
$/c             split string on "2015", and copy the result
"<s>`x</s>*"*P  join with "<s>2015</s>*" and print
D!C             stop executing if split resulted in more than 1 element
'*p             print "*" without newline
|Xp             increment x register and print without newline
                print unterminated compressed literal ". I'm a year early!"
`Q_RUWZ)O-C!

Run this one

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

Retina, 49 45 64 61 60 54 bytes

s`2015.+
$&¶*2016. I'm a year early!
\`2015
<s>$&</s>*

After numerous attempts I think this is the shortest Retina solution


The first line matches 2015, the second line replaces the previous match (2015) with <s>$&</s>* where $& is the match's value or in this case, 2015.

The \ removes the trailing newline from the output. The ` starts the regex. $ matches the end specifying we're trying to add a string to the end. The last line simply adds the string where becomes a newline. Retina uses the ISO-8859-1 encoding in which the is one byte

Try it online

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

ROOP, 110 bytes

 1I
 CW
 |%|
 V !
H
' "<s>2015</s>*"
2R !
0 #V
1 G #
5V #"\n*2016. I'm a year early!"
'| M #
 -V #

  #W
   O#

This uses the R operator to replace all occurrences of the string 2015 in the input to <s>2015</s>*.Then, there is a comparison -- if the input string and the new one are different, then 1 is returned. Otherwise, 0 is returned. Then, M repeats the string \n*2016. I'm a year early that many times, i.e. if there are no changes, the string is blank, otherwise, the string is \n*2016. I'm a year early. Then, the strings are printed and the program terminates.

Above there is a 1 which is moved to the left and then falls to the operator H, which ends the program.

\$\endgroup\$
4
  • \$\begingroup\$ It should print "*2016. I'm a year early!" only once \$\endgroup\$
    – user46167
    Jan 3, 2016 at 23:27
  • \$\begingroup\$ That's what it does. \$\endgroup\$ Jan 3, 2016 at 23:29
  • \$\begingroup\$ Oh. I misinterpreted the usage of M. \$\endgroup\$
    – user46167
    Jan 3, 2016 at 23:32
  • \$\begingroup\$ Use 1 or 0, sorry, English is not my native language. Feel free to improve the text if necessary. \$\endgroup\$ Jan 3, 2016 at 23:35
0
\$\begingroup\$

Perl 5 -p, 53 bytes

$\="\n*2016. I'm a year early!"if s|2015|<s>$&</s>*|g

Try it online!

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

Python 3.8 (pre-release), 80 bytes

lambda x:(b:=x.replace('2015','<s>2015<s>*'))+"\n*2016. I'm a year early!"*(b>x)

Try it online!

Saved 8 bytes thanks to squid

\$\endgroup\$
3
  • \$\begingroup\$ Unless I've missed a way that this breaks it, 84 bytes \$\endgroup\$
    – squid
    May 28, 2019 at 18:03
  • \$\begingroup\$ 80 bytes \$\endgroup\$
    – squid
    May 28, 2019 at 19:08
  • \$\begingroup\$ @squid Okay, thanks! \$\endgroup\$
    – MilkyWay90
    May 28, 2019 at 22:18
0
\$\begingroup\$

JavaScript: 62 bytes

x=>x.replace('2015','<s>2015</s>')+"\n*2016. I'm a year early!"
\$\endgroup\$
1
  • 3
    \$\begingroup\$ I'm afraid you missed a point: “\n*2016. I'm a year early!” has to be appended only if the input contained “2015” and your code actually made a replacement. So you must not append it to “foo bar baz quux”. \$\endgroup\$
    – manatwork
    Feb 2, 2021 at 12:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.