| Bytes | Lang | Time | Link |
|---|---|---|---|
| 4465 | C | 250118T080858Z | Xuesong |
| 201 | AutoHotkey v2 | 241216T121136Z | Xuesong |
| 272 | Go | 190810T174237Z | Purple P |
| 100 | Ruby | 190810T213323Z | Shelvacu |
| 080 | Pyth | 190812T195128Z | Shelvacu |
| 193 | C# Visual C# Interactive Compiler | 190812T031512Z | Gymhgy |
| 116 | JavaScript ES6 | 190811T090445Z | Endenite |
| 216 | Haskell | 190810T225548Z | B. Mehta |
| 106 | Perl 6 | 190810T234132Z | Jo King |
C, 4465 bytes
Nxx are macros to put at the end of the file. So that the end character has not to be a ; or }. When one of the macro is at the end of file, like N95, it's actually a function definition int n(){return 96;}
#include<stdio.h>
#define F(x) int n(){return x;}
#define N F(0)
#define N00 F(1)
#define N01 F(2)
/* N02 ... N98 */
#define N98 F(99)
#define N99 F(100)
int n();char*m="BREAKING NEWS: WORLD ENDS",*s="#include<stdio.h>%c#define F(x) int n(){return x;}%c#define N F(0)%c#define N00 F(1)%c#define N01 F(2)%c#define N02 F(3)%c#define N03 F(4)%c#define N04 F(5)%c#define N05 F(6)%c#define N06 F(7)%c#define N07 F(8)%c#define N08 F(9)%c#define N09 F(10)%c#define N10 F(11)%c#define N11 F(12)%c#define N12 F(13)%c#define N13 F(14)%c#define N14 F(15)%c#define N15 F(16)%c#define N16 F(17)%c#define N17 F(18)%c#define N18 F(19)%c#define N19 F(20)%c#define N20 F(21)%c#define N21 F(22)%c#define N22 F(23)%c#define N23 F(24)%c#define N24 F(25)%c#define N25 F(26)%c#define N26 F(27)%c#define N27 F(28)%c#define N28 F(29)%c#define N29 F(30)%c#define N30 F(31)%c#define N31 F(32)%c#define N32 F(33)%c#define N33 F(34)%c#define N34 F(35)%c#define N35 F(36)%c#define N36 F(37)%c#define N37 F(38)%c#define N38 F(39)%c#define N39 F(40)%c#define N40 F(41)%c#define N41 F(42)%c#define N42 F(43)%c#define N43 F(44)%c#define N44 F(45)%c#define N45 F(46)%c#define N46 F(47)%c#define N47 F(48)%c#define N48 F(49)%c#define N49 F(50)%c#define N50 F(51)%c#define N51 F(52)%c#define N52 F(53)%c#define N53 F(54)%c#define N54 F(55)%c#define N55 F(56)%c#define N56 F(57)%c#define N57 F(58)%c#define N58 F(59)%c#define N59 F(60)%c#define N60 F(61)%c#define N61 F(62)%c#define N62 F(63)%c#define N63 F(64)%c#define N64 F(65)%c#define N65 F(66)%c#define N66 F(67)%c#define N67 F(68)%c#define N68 F(69)%c#define N69 F(70)%c#define N70 F(71)%c#define N71 F(72)%c#define N72 F(73)%c#define N73 F(74)%c#define N74 F(75)%c#define N75 F(76)%c#define N76 F(77)%c#define N77 F(78)%c#define N78 F(79)%c#define N79 F(80)%c#define N80 F(81)%c#define N81 F(82)%c#define N82 F(83)%c#define N83 F(84)%c#define N84 F(85)%c#define N85 F(86)%c#define N86 F(87)%c#define N87 F(88)%c#define N88 F(89)%c#define N89 F(90)%c#define N90 F(91)%c#define N91 F(92)%c#define N92 F(93)%c#define N93 F(94)%c#define N94 F(95)%c#define N95 F(96)%c#define N96 F(97)%c#define N97 F(98)%c#define N98 F(99)%c#define N99 F(100)%cint n();char*m=%cBREAKING NEWS: WORLD ENDS%c,*s=%c%s%c,r=10;int main(){n()<100?printf(s,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,34,34,34,s,34,n()):printf(m);}N%02d",r=10;int main(){n()<100?printf(s,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,34,34,34,s,34,n()):printf(m);}N
Not a competitive answer. But I've been tried for a long time on this challenge with C language. Still cannot find a way to reduce the #define macros.
Explanation
#include<stdio.h>
#define F(x) int n(){return x;}
#define N F(0)
#define N00 F(1)
#define N01 F(2)
/* N02 ... N98 */
#define N98 F(99)
#define N99 F(100)
int n(); // forward declaration
char*m="BREAKING NEWS: WORLD ENDS" // the final output
,*s="...N%02d" // format string for step 1 to step 99 output. %02d will format the number at EOF
,r=10; // the char `\n`, defined for saving bytes
int main(){
n()<100?
printf(s,r,...,n()): // for step 1 to 99, print the quine itself, plus the trailing number
}
N // the actual implementation of function n()
AutoHotkey (v2), 201 bytes
SetTimer(()=>MsgBox(n<199?Format(s:="SetTimer(()=>MsgBox(n<199?Format(s:={:c}{}{1:c},34,s,n<2?0:n-99):'BREAKING NEWS: WORLD ENDS'),-1),n:=1{3:02}",34,s,n<2?0:n-99):'BREAKING NEWS: WORLD ENDS'),-1),n:=1
AHK does not allow to use a variable before initialization. I could not find a way to read a value at script end and was almost giving up. However, enlightened by Endenite's answer, I managed to achieve it with SetTimer.
Go, 382 366 354 340 305 298 272 bytes
Won't win but I had fun with the challenge.
package main
func main(){if n<199{s+="\x60,"
print(s[:135],s,100+(n/100)*(n%100+1))}else{print("BREAKING NEWS: WORLD ENDS")}}
var s,n=`package main
func main(){if n<199{s+="\x60,"
print(s[:135],s,100+(n/100)*(n%100+1))}else{print("BREAKING NEWS: WORLD ENDS")}}
var s,n=`,1
Based on a quine. If the variable n is less than 199, then it adds to the string variable n a grave accent (\x60), Go's character for multiline strings, followed by a comma ,. It continues by printing the first 147 characters of s (to prevent printing the , because that only occurs at the end) then prints s as a whole, and finally prints 100 if n == 1 and n+1 otherwise. The result is that successive runs change the variable n at the end of the program to 100, 101, etc. If the variable n is 199 or more, then it prints breaking news.
Ruby, 158 154 146 128 122 100 bytes
Inspired by this answer.
eval s=%{$><<(D>198?'BREAKING NEWS: WORLD ENDS':'eval s=%%{%s}<<D=%d'%[s[0..-2],D<2?100:D+1])#}<<D=1
EDIT: I was able to remove (s.split(35.chr)[0]+35.chr).inspect and replace it with s[0..-2] (range of every value except the last) and the %{ ... } string syntax I used before. Saved 22 bytes!
Old version:
EDIT: Saved a pair of parens (and corresponding pair in the data section) by realizing that "BREAKING NEWS: WORLD ENDS" is a perfectly valid format string, and ruby ignores any extraneos parameters.
END{$><<(q=S>198?"BREAKING NEWS: WORLD ENDS":%{END{$><<(q=S>198?"BREAKING NEWS: WORLD ENDS":%%{%s})%%[q,S<2?0:S-99]}
S=1%02d})%[q,S<2?0:S-99]}
S=1
When starting this I realized that since the number has to go at the very end of the program, and ruby doesn't allow using variables before they are declared, I would have to make code run after the digits somehow. I could've done something like def a(s) ... end;a 1 which would become ...end;a 100, however using ruby's lesser-known END syntax uses fewer bytes. However, the block inside END is in a different scope, so S must be a global variable or constant.
Explanation:
END{ ... };S=1: Run block of code just before program terminates; Set constantSto1(or100-199in future iterations)$><<( ... ):$>is a shortcut in ruby for stdout, and<<on an IO writes to the IO. The parens are required, otherwise it becomes($><<S)>198...S>198?"BREAKING NEWS: WORLD ENDS":(q=...)%[q,S<2?0:S-99]: If I were to break this out into slightly more reasonable code it would be:if S > 198 "BREAKING NEWS: WORLD ENDS" else q = ... number_to_append = if S < 2 0 else S - 100 + 1 end q % [q, number_to_append] endThe
%operator applied to a string is effectivelyprintf, with the LHS being the format string and the RHS being the arguments.%{ ... (q=%%{%s}) ... S=1%02d}: ruby has an interesting syntax for strings that also allows for pairs of curly brackets to appear within the string without any escaping as long as they are balanced. This is very helpful, as otherwise a similar quine would have to escape the string to put it into itself as a string literal. The two substitutions in the format string are%sfor a regular string and%02dfor a number right-padded to a size of 2 with the character0.
My thoughts on shortening further:
It'd be nice to be able to useEDIT: Constants are global and can be one character!sinstead of$s, but defining ss=$s;or making a functiondef a(s) ...both use more bytes than they save, and I can't think of any other way to do it.- It'd be nice if
Swas always less then100, so that it could be compared using 2-digit numbers instead of 3-digit numbers. However, if I useS=0at the end, the next two digits are interpreted as octal, and8and9are invalid and all is bunk.S=simply isn't valid, and I don't know any other way to make a value valid both before and after appending two digits. Of note,0(and every other integer) is truthy in ruby.
Any thoughts on how to make this shorter, please let me know!
Pyth, 81 80 bytes
.vh,K"?>J198\"BREAKING NEWS: WORLD ENDS\"s[\".vh,K\"N:KN+C92NN\"J\"?<J2*TT+J1"J1
Explanation:
.vh,K" ... "J1
.v Eval pyth code
h Get the first item in list
, Make a list of the next two items
J1 Set J = 1 (J = 100 to 199 in future iterations)
K" ... " Set K to the given string
And the code in the string is (breaking news shortened):
?>J198"BNWE"s[".vh,K"N:KN+C92NN"J"?<J2*TT+J1
? Ternary: If A then B else C
>J198 Test if J > 198
"BNWE" String literal; If ternary was true, return this (which is then implicitly printed)
s[".vh,K"N:KN+C92NN"J"?<J2*TT+J1 ternary else
s concatenate list of strings
[ create list
".vh,K" string literal, list[0]
N N is set to the double-quote character, list[1]
:KN+C92N list[2]
: Regex substitution. In A, replace B with C
K Variable K (set to the string being eval'd)
N N is double-quote
+C92N A backslash followed by a double-quote
+ concat two strings
C92 character with code point 92 (backslash)
N N is double-quote
N N is double-quote, list[3]
"J" String literal, list[4]
?<J2*TT+J1 Inner ternary, list[5]
<J2 If J < 2 ..
*TT return T * T (10 * 10), else
+J1 return J+1
C# (Visual C# Interactive Compiler), 193 bytes
x=>{var a=t>198?"BREAKING NEWS: WORLD ENDS":"x=>{{var a=t>198?{1}BREAKING NEWS: WORLD ENDS{1}:{1}{0}{1};Write(a,a,(char)34,t+(t<2?99:1));}};int t={2}";Write(a,a,(char)34,t+(t<2?99:1));};int t=1
JavaScript (ES6), 116 bytes
setTimeout(s="alert((n-1?++n:n=100)-200?`setTimeout(s=${JSON.stringify(s)}),n=`+n:'BREAKING NEWS: WORLD ENDS')"),n=1
94 bytes, if stringifying functions is allowed
f=_=>((p=`f=${f}`.split`|`)[4]=p[4]-1?-~p[4]:100)-200?p.join`|`:'BREAKING NEWS: WORLD ENDS'||1
Haskell, 240 232 218 216 bytes
p=putStr;h 1=100;h k=k+1;g _=p$"n="++show(h n);main|n>198=p"BREAKING NEWS: WORLD ENDS"|1>0=p<>print<>g$"p=putStr;h 1=100;h k=k+1;g _=p$\"n=\"++show(h n);main|n>198=p\"BREAKING NEWS: WORLD ENDS\"|1>0=p<>print<>g$"
n=1
Perl 6, 122 114 106 bytes
-8 bytes thanks to Shelvacu
END {<print $!-199??"END \{<$_>~~.EVAL};\$!="~($!-1&&$!-99)+100!!"BREAKING NEWS: WORLD ENDS">~~.EVAL};$!=1
Takes the standard quine format and adds the breaking news string as well as a new function call with the appended number. This uses END to execute the function when the program ends.