6
\$\begingroup\$

You should a program in language X, which will only output its source code (i.e function as a quine) when it is not a monday. On monday it should output some string with a levehnstein distance of 4 from the source code. i.e if the code is aaaa on sunday it should output aaaa and on monday it could output ....aaaa or bbbb or cccc or ..aaaa.. etc


Read about Levehnstein difference here https://en.wikipedia.org/wiki/Levenshtein_distance
Rules
  • Your solution must be testable
  • No cheating quines, and any other standard loopholes allowed
  • Should your language "X" lack access to the date it may assume its a random date at the cost of 35 bytes
  • This is code golf and I will accept the shortest solution (in bytes) as winner
  • \$\endgroup\$
    2
    • 2
      \$\begingroup\$ This is yet another generalized quine challenge... \$\endgroup\$
      – user45941
      Jul 2, 2016 at 5:17
    • \$\begingroup\$ Yes I was worried that it would be closed as such, but if the moderators would have wanted to close it as duplicate they would have by now. \$\endgroup\$ Jul 2, 2016 at 12:50

    10 Answers 10

    8
    \$\begingroup\$

    Javascript - 47 45 43 42 34 32 29 bytes

    8 bytes saved thanks to @Downgoat.

    5 bytes saved thanks to @Dendrobium.

    f=_=>(Date()[0]!="M"&&"f=")+f
    
    \$\endgroup\$
    15
    • \$\begingroup\$ !Date().search`Mon` is shorter way to check if it is monday \$\endgroup\$
      – Downgoat
      Jul 1, 2016 at 22:05
    • \$\begingroup\$ @Downgoat cool, didn't know. how does ` work btw? \$\endgroup\$
      – Maltysen
      Jul 1, 2016 at 22:05
    • \$\begingroup\$ Date()[0]=='M' is even shorter \$\endgroup\$
      – Downgoat
      Jul 1, 2016 at 22:07
    • 2
      \$\begingroup\$ Huh, f=_=>(Date()[0]!="M"&&"f=")+f for 29 bytes. When its Monday, the function returns false_=>(Date()[0]!="F"&&"f=")+f with the difference between = and alse being 4. \$\endgroup\$
      – Dendrobium
      Jul 1, 2016 at 23:53
    • 1
      \$\begingroup\$ If the challange was about fridays, you could check it with Date()[0]<"G", saving one byte, but as far as I can see, you can’t make use of Unicode ordering here… \$\endgroup\$ Jul 2, 2016 at 11:49
    7
    \$\begingroup\$

    Jolf, 19 13 9 bytes

    ?wfb+q~2q
    

    Try it online.

    Saved quite a few bytes thanks to Conor.

    ?wfb+q~2q
    
    ?wfb+q~2q
    ?          if
     wfb       the current weekday - 1 (which is 0 on Monday) is truthy
        +q~2   print the program's source code with 1000 appended to it
               else
            q  print the program's source code
    
    \$\endgroup\$
    12
    • 1
      \$\begingroup\$ It's only 1 byte longer than CJam so I wouldn't say this is that long. \$\endgroup\$
      – Downgoat
      Jul 1, 2016 at 22:25
    • \$\begingroup\$ @Downgoat It should be at minimum 2 bytes shorter but there's a bug \$\endgroup\$ Jul 1, 2016 at 22:35
    • \$\begingroup\$ For the condition, try ='MFf3. For the extra four characters, try *4S (four newlines) \$\endgroup\$ Jul 1, 2016 at 23:34
    • \$\begingroup\$ Ah. Good thing it's a Monday and not a Tuesday. \$\endgroup\$ Jul 1, 2016 at 23:40
    • 4
      \$\begingroup\$ Does using a quine built-in not count as a cheating quine any more? \$\endgroup\$ Jul 2, 2016 at 6:56
    5
    \$\begingroup\$

    CJam, 18 17 bytes

    {"_~"et7=({\}|}_~
    

    Test it here.

    On Mondays, it prints:

    _~{"_~"et7=({\}|}
    

    Dennis found a 16-byte solution which will break in the year 10,000 though:

    {"_~"et_7=1=<}_~
    

    On Mondays, this prints

    {"_~"et_7=1=<}_~2016
    

    ... or whatever the current year is.

    \$\endgroup\$
    2
    • \$\begingroup\$ {et_7=1=<"_~"}_~ works for the next ~8000 years. \$\endgroup\$
      – Dennis
      Jul 1, 2016 at 22:35
    • \$\begingroup\$ @Dennis haha, that's a neat idea. \$\endgroup\$ Jul 1, 2016 at 22:37
    3
    \$\begingroup\$

    Mathematica, 75 bytes

    StringJoin[If[DayName[] == Monday, "0001", ""], ToString[#0, InputForm]] & 
    

    Anonymous function, takes no inputs and returns the output. Replace Monday with Friday (or whatever the current day is) to test.

    \$\endgroup\$
    3
    • 1
      \$\begingroup\$ i see lots of spaces \$\endgroup\$
      – Maltysen
      Jul 1, 2016 at 22:18
    • 1
      \$\begingroup\$ @Maltysen A byproduct of the quine mechanism \$\endgroup\$ Jul 1, 2016 at 22:18
    • \$\begingroup\$ oh, lol ------------ \$\endgroup\$
      – Maltysen
      Jul 1, 2016 at 22:19
    3
    \$\begingroup\$

    Vitsy + bash, 29 26 bytes

    '&"etad","M"-)[Y4\a]?rd3*Z
    
    '                            Take a string of everything until hitting another '.
                                 This will wrap around as it finds none.
     &                           Create a new stack and push the current stack over.
      "etad"                     Push the string "date" to the stack.
            ,                    Execute the current stack through shell.
             "M"-                Subtract the character "M" from the first item of the
                                 current stack.
                 )[    ]         If it is equal to zero, do the stuff in brackets.
                   Y             Remove the current stack.
                    4\a          Push the return character to the stack four times.
                        ?        Rotate over a stack (if there is none, stay put).
                         r       Reverse the stack.
                          d3*    Push the character ' to the stack.
                             Z   Print everything in the current stack.
    

    This cannot be tried online as it accesses shell, however, feel free to test this with the downloadable interpreter found here.

    Note that this is a variant of the standard quine, 'rd3*Z.

    \$\endgroup\$
    3
    \$\begingroup\$

    Batch, 128 bytes

    @set/am=%date:~3,2%,a=(14-m)/12,y=%date:~6%-a,d=(%date:~0,2%+y+y/4-y/100+y/400+(m+47)*18/7+a*3)%%7
    @if %d%==1 echo @:
    @type %0
    

    Since Windows XP, date doesn't give you the day of the week, so I must directly calculate it from the date. The leap year adjustment wants March-based years so a is used as an adjusting factor. (m+47)*18/7 is quicker than using a month-based lookup table.

    Note that you need to invoke the file using its full name (e.g. monday.bat). The echo @: counts as a distance of 4 because of the extra CR/LF; I used @: because the result is still a syntactically valid batch script.

    \$\endgroup\$
    1
    • \$\begingroup\$ Date format is related to system setting \$\endgroup\$
      – l4m2
      May 13, 2020 at 15:38
    2
    \$\begingroup\$

    Java "Only" 2,897 bytes

    Take a look at this monstrosity. Can someone help me test this on a monday, I cant figure out how to trick Java into thinking its monday.

    import java.util.Calendar;public class Quine{public static void main(String[] args){char[] s={83,121,115,116,101,109,46,111,117,116,46,112,114,105,110,116,40,34,105,109,112,111,114,116,32,106,97,118,97,46,117,116,105,108,46,67,97,108,101,110,100,97,114,59,112,117,98,108,105,99,32,99,108,97,115,115,32,81,117,105,110,101,123,112,117,98,108,105,99,32,115,116,97,116,105,99,32,118,111,105,100,32,109,97,105,110,40,83,116,114,105,110,103,91,93,32,97,114,103,115,41,123,34,41,59,83,121,115,116,101,109,46,111,117,116,46,112,114,105,110,116,40,34,99,104,97,114,91,93,32,115,61,123,34,41,59,102,111,114,40,105,110,116,32,105,61,48,59,105,60,115,46,108,101,110,103,116,104,45,49,59,105,43,43,41,123,83,121,115,116,101,109,46,111,117,116,46,112,114,105,110,116,40,40,105,110,116,41,115,91,105,93,43,34,44,34,41,59,125,83,121,115,116,101,109,46,111,117,116,46,112,114,105,110,116,40,40,105,110,116,41,115,91,115,46,108,101,110,103,116,104,45,49,93,43,34,125,59,34,41,59,102,111,114,40,99,104,97,114,32,99,58,32,115,41,123,83,121,115,116,101,109,46,111,117,116,46,112,114,105,110,116,40,99,41,59,125,105,102,40,67,97,108,101,110,100,97,114,46,103,101,116,73,110,115,116,97,110,99,101,40,41,46,103,101,116,40,55,41,61,61,50,41,123,83,121,115,116,101,109,46,111,117,116,46,112,114,105,110,116,40,34,109,111,110,100,34,41,59,125,125,125,83,121,115,116,101,109,46,111,117,116,46,112,114,105,110,116,40,34,105,109,112,111,114,116,32,106,97,118,97,46,117,116,105,108,46,67,97,108,101,110,100,97,114,59,112,117,98,108,105,99,32,99,108,97,115,115,32,81,117,105,110,101,123,112,117,98,108,105,99,32,115,116,97,116,105,99,32,118,111,105,100,32,109,97,105,110,40,83,116,114,105,110,103,91,93,32,97,114,103,115,41,123,34,41,59,83,121,115,116,101,109,46,111,117,116,46,112,114,105,110,116,40,34,99,104,97,114,91,93,32,115,61,123,34,41,59,102,111,114,40,105,110,116,32,105,61,48,59,105,60,115,46,108,101,110,103,116,104,45,49,59,105,43,43,41,123,83,121,115,116,101,109,46,111,117,116,46,112,114,105,110,116,40,40,105,110,116,41,115,91,105,93,43,34,44,34,41,59,125,83,121,115,116,101,109,46,111,117,116,46,112,114,105,110,116,40,40,105,110,116,41,115,91,115,46,108,101,110,103,116,104,45,49,93,43,34,125,59,34,41,59,102,111,114,40,99,104,97,114,32,99,58,32,115,41,123,83,121,115,116,101,109,46,111,117,116,46,112,114,105,110,116,40,99,41,59,125,105,102,40,67,97,108,101,110,100,97,114,46,103,101,116,73,110,115,116,97,110,99,101,40,41,46,103,101,116,40,55,41,61,61,50,41,123,83,121,115,116,101,109,46,111,117,116,46,112,114,105,110,116,40,34,109,111,110,100,34,41,59,125,125,125};System.out.print("import java.util.Calendar;public class Quine{public static void main(String[] args){");System.out.print("char[] s={");for(int i=0;i<s.length-1;i++){System.out.print((int)s[i]+",");}System.out.print((int)s[s.length-1]+"};");for(char c: s){System.out.print(c);}if(Calendar.getInstance().get(7)==2){System.out.print("mond");}}}
    
    \$\endgroup\$
    5
    • 2
      \$\begingroup\$ What on earth is that char array? \$\endgroup\$ Jul 2, 2016 at 3:20
    • 1
      \$\begingroup\$ Wtf 2,897 bytes \$\endgroup\$
      – Insane
      Jul 2, 2016 at 3:45
    • 1
      \$\begingroup\$ @Insane This is the only quine framework i have come up with myself. I could rob a better quine framework from someone, but still I find my solution hilarious. \$\endgroup\$ Jul 2, 2016 at 12:54
    • 1
      \$\begingroup\$ Okay now that looks really strange, I'm sure that can be golfed. At least by stripping some white space and renaming args \$\endgroup\$
      – Frozn
      Jul 2, 2016 at 15:52
    • \$\begingroup\$ Yeah it could be golfed... for one I shouldn't use a char[] to store the code! Also I call the class Quine... Its golfed enough \$\endgroup\$ Jul 2, 2016 at 18:42
    2
    \$\begingroup\$

    GolfScript, 21 20 + 35 = 56 55 bytes (or 22 + 0 = 22 20)

    With randomizing, no input expected (following the rules completely):

    {7rand 0=2*)".~"*}.~
    

    Alternatively, date as input, in format like Javascript's new Date().toString() (bending the rules a little):

    {\.,%"M"=2*)".~"*}.~
    

    Each solution looks like this:

    {somecode2*)".~"*}.~
    
    {                }    create a block
                      .   duplicate it
                       ~  execute one copy
     somecode             push 1 if monday or 0 otherwise
             2*           multiply by 2 (2 if monday, 0 otherwise)
               )          add 1 (3 if monday, 1 otherwise)
                ".~"      the tail...
                    *     ...copied 1 or 3 times and concatenated together
    

    Now the stack looks like this:

    .~  (or .~.~.~)
    {somecode2*)".~"*}
    

    ... and is printed bottom to top.

    The somecode part for random:

    7rand 0=
    
    7rand          select a random number from 0 to 6, let 0 be monday
          0=       check if equal to 0 (1 if monday, 0 otherwise)
    

    The somecode part for input:

    \.,%"M"=
    \             we have the input first and the block on top, so we need to swap them
     .            duplicate the input
      ,           replace the second copy with its length
       %          get every (length)-th character starting from 0, effectively fetching first (M on mondays, something else otherwise)
        "M"=      check if equal to M (1 on mondays, 0 otherwise)
    
    \$\endgroup\$
    2
    • \$\begingroup\$ Good Answer! Welcome to Programming Puzzles and code Golf! While technically quines (and quine variants) should take no input, I do like both solutions to be there and they both seem well thought out! \$\endgroup\$ Jul 2, 2016 at 18:44
    • \$\begingroup\$ @RohanJhunjhunwala Yes, they shouldn't take any input, I use it as an altervative for GolfScript's non-existent date builtin. \$\endgroup\$
      – Maya
      Jul 3, 2016 at 15:02
    1
    \$\begingroup\$

    Java, 334 characters

    import java.util.Calendar;interface Q{static void main(String[]a){String p="import java.util.Calendar;interface Q{static void main(String[]a){String p=%c%s%1$c;System.out.printf(p,34,p);if(Calendar.getInstance().get(7)==2)System.out.print(0<1);}}";System.out.printf(p,34,p);if(Calendar.getInstance().get(7)==2)System.out.print(0<1);}}
    

    Based on @RohanJhunjhunwala 's solution, just using the quine method from over here. The difference of four comes by appending true if it's monday. I think it should be possible to shorten that a bit.

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

    Reng v.5, noncompeting, 56 bytes

    Version 5 brings to you object oriented programming!

    "68¹65¹84¹69¹4&A3¹A1¹B6¹68¹97¹C1¹6Ó`1e4*{Xo}2[r*]rao! ;~
    

    This is a standard quine. Simply, "<code>rao! ;~ executes <code>, then outputs the programming, then exits.

    ¹ is Manhattan addition, e.g., digit concatenation. This gives the char codes of DATE, then 4& retrieves the date from the date module. (Equivalently, "DATE"4&.) This pushes a date object. (While it looks like it should be easily retrievable from the display in the stack, this is just how it prints in the stack display. It is unable to be manipulated.)

    The next series provides another pseudo string, translating to getDay. Then, we get the property getDay of the date object on the stack using . We then execute it with `. This provides JS's getDay. When it's 1, it's monday. We check for equality with 1 using 1e, then multiply by four. {Xo} pushes a code block that prints (o) char 33 (X), or !. 2[r creates a new stack with the top two items in reverse order. Then, we repeat (*) the code block 4 times (when monday) or 0 times (any other day). On monday, it prints 4 !s. Otherwise, it prints nothing. Then the string captured by wraparound is printed, and we have the rest of our source.

    Output on monday:

    !!!!68¹65¹84¹69¹4&A3¹A1¹B6¹68¹97¹C1¹6Ó`1e4*{Xo}2[r*]rao! ;~
    

    On other days:

    68¹65¹84¹69¹4&A3¹A1¹B6¹68¹97¹C1¹6Ó`1e4*{Xo}2[r*]rao! ;~
    
    \$\endgroup\$

    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.