10
\$\begingroup\$

This is a pretty simple challenge, but (I hope) a fun one.

Task

If right now either the month is 4 and the day is 20, the hour (on a 12 hour clock) is 4 and the minute is 20, or the minute is 4 and the second is 20, print this ascii art:

                        .
                       .:.
                       :|:
                      .:|:.
                      ::|::
       :.             ::|::             .:
       :|:.          .::|::.          .:|:
       ::|:.         :::|:::         .:|:;
       `::|:.        :::|:::        .:|::'
        ::|::.       :::|:::       .::|:;
        `::|::.      :::|:::      .::|::'
         :::|::.     :::|:::     .::|::;
         `:::|::.    :::|:::    .::|::;'
`::.      `:::|::.   :::|:::   .::|::;'     .:;'
 `:::..    `;::|::.  :::|:::  .::|:::    ::::;
   `:::::.    ':|::. :::|::: .::|:'   ,::::;'
     `:::::.    ':|:::::|:::::|:'   :::::;'
       `:::::.:::::|::::|::::|::::.,:::;'
          ':::::::::|:::|:::|:::::::;:'
             ':::::::|::|::|:::::::''
                  `::::::::::;'
                 .:;'' ::: ``::.
                      :':':
                        ;

You can have trailing spaces.

Otherwise, print the number of minutes and seconds left until the next such occasion in the format: %d minutes and %d seconds left until your next hit. with the time being minimized.

This is , so shortest code in bytes wins!

P.S. If this challenge is too inappropriate for this site, tell me and I'll change it.

\$\endgroup\$
  • 15
    \$\begingroup\$ Aren't you, like, in the first half of high school? :P \$\endgroup\$ – Arcturus Apr 12 '16 at 2:06
  • \$\begingroup\$ Also, does 4:00 PM (on an otherwise nonprint-y time) count as print-y because of the 4 or nonprint-y because it's also 16:00 hours? \$\endgroup\$ – Arcturus Apr 12 '16 at 2:07
  • 4
    \$\begingroup\$ I'd be more worried about it being a dupe, I feel like two halves of old challenges (the various timing ones and the various kgc ones) doesn't really make this a very interesting challenge, but I'm not really sure what I would close it as a dupe of (or if it's even correct to do that...). \$\endgroup\$ – FryAmTheEggman Apr 12 '16 at 2:08
  • \$\begingroup\$ @ANerd-I printy, I'll edit that in. \$\endgroup\$ – Maltysen Apr 12 '16 at 2:10
  • \$\begingroup\$ I was looking at this image and wondering if some sort of custom run-length encoding would be viable for golfing the output, or if there's a better solution? \$\endgroup\$ – Patrick Roberts Apr 12 '16 at 3:52
6
\$\begingroup\$

Python 2, 371 bytes

This source contains non-printable bytes, so it is presented as a hexdump that can be decoded with xxd -r.

00000000: efbb bf66 726f 6d20 7469 6d65 2069 6d70  ...from time imp
00000010: 6f72 742a 0a74 3d74 696d 6528 290a 693d  ort*.t=time().i=
00000020: 300a 7768 696c 6527 3034 3a32 3027 6e6f  0.while'04:20'no
00000030: 7420 696e 2073 7472 6674 696d 6528 2725  t in strftime('%
00000040: 6d3a 2564 2572 272c 6c6f 6361 6c74 696d  m:%d%r',localtim
00000050: 6528 742b 6929 293a 692b 3d31 0a70 7269  e(t+i)):i+=1.pri
00000060: 6e74 5b22 7801 74cd c701 c430 0844 d1bb  nt["x.t....0.D..
00000070: aaf0 cd99 0286 6654 887a dfa0 f41d 7136  ......fT.z....q6
00000080: 8f61 7829 0b6f 5c72 bdb6 9414 de86 d2eb  .ax).o\r........
00000090: 9894 d4e7 64f7 de39 099a 8ed8 32b5 d34a  ....d..9....2..J
000000a0: e8c9 2a53 9da4 371a b1d0 a3d4 18e8 b212  ..*S..7.........
000000b0: 5a25 a139 158a ac90 4cba 7692 4007 c62e  Z%.9....L.v.@...
000000c0: 81b8 31c4 9682 04e2 6ab8 8f21 3bb3 3ce1  ..1.....j..!;.<.
000000d0: 7582 0163 8524 79a8 c175 cb58 7ce5 45ff  u..c.$y..u.X|.E.
000000e0: b3b7 8cc7 bfbe fbaa 9b95 b068 1837 db90  ...........h.7..
000000f0: a546 b54a 5cb9 5c38 6801 0936 a2a8 a85e  .F.J\.\8h..6...^
00000100: 6ca3 4c3e 8e83 a4ef 1412 12ac 7027 7075  l.L>........p'pu
00000110: 2084 ca61 026b 5c30 286e a1fe 222e 6465   ..a.k\0(n..".de
00000120: 636f 6465 2827 7a69 7027 292c 2725 6420  code('zip'),'%d 
00000130: 6d69 6e75 7465 7320 616e 6420 2564 2073  minutes and %d s
00000140: 6563 6f6e 6473 206c 6566 7420 756e 7469  econds left unti
00000150: 6c20 796f 7572 206e 6578 7420 6869 742e  l your next hit.
00000160: 2725 2869 2f36 302c 6925 3630 295d 5b69  '%(i/60,i%60)][i
00000170: 3e30 5d                                  >0]

Readable part:

from time import*
t=time()
i=0
while'04:20'not in strftime('%m:%d%r',localtime(t+i)):i+=1
print["(ZLIB DATA)".decode('zip'),'%d minutes and %d seconds left until your next hit.'%(i/60,i%60)][i>0]
\$\endgroup\$
  • \$\begingroup\$ Oh whoops, I misread the code. I thought it continually looped and printed. Stupid tiny mobile screen. \$\endgroup\$ – Mego Apr 13 '16 at 4:03
4
\$\begingroup\$

JavaScript (ES6), 537 bytes

alert((f=t=>new Date(Date.now()+t*1e3).toISOString().match`(T16|04)[-:]20`)(i=0)?"À\u0019\u0002û1!°?4\u0002ë14!°>C3\u0002=\u0001Ê34\u0003Ê1\u0002=4\u00011C3\u000114\u0002=C\u0013ð4\u0009\u001fCS\u0002m34\u0001>Ià14s\u0002>C3\u0001=IÐ1CS\u0002n34\u0013À4\u0009\u001c34s\u0002?I3\u0001;I°1C3%ð6I3\u0001:I 1C3ub3\u0001l4\u00134\u0009\u001934S\u0007\u001bS'`\u0011 V34\u0013\u00004\u0009\u001034\u0009:Z\u0002i³\u0001zC3\u00014\u00091Cs8Z'°6\u001b 74K³4\u00079['Ð6\u001b³4J£4\u001a8Y' yó4I4]s\u0002Ê7M34CÓw\u0002«6u\u00021u\u0007`6\u0013\u0002ëss#ÀY".replace(/./g,c=>(l=" .\n:|;`',0123456")[(n=c.charCodeAt())&15]+l[n>>4]).replace(/.\d+/g,c=>c[0].repeat(parseInt(c.slice(1),7)+3)):eval('for(;!f(++i););`${i/60|0} minutes and ${i%60|0} seconds left until your next hit.`'))

Uses the CP-1252 encoding.

Note: All unreadables in this post are escaped with \u00xx so that the Stack Exchange system doesn't automatically remove them. They should be considered to have a size of one byte.

Explanation

Uses a run-length encoding scheme along with packing each character into 4 bits. The main logic of the code is 174 bytes and the leaf string (including decompression) is 364 bytes. Further details of the compression method are found below.

alert((
  f=t=>                                   // f checks for 4:20 in a date
    new Date(Date.now()+t*1e3)            // get the date at now + t seconds
    .toISOString().match`(T16|04)[-:]20`  // find 4:20 in the ISO date string
)(i=0)?

  // Leaf decompression
  "À\u0019\u0002û1!°?4\u0002ë14!°>C3\u0002=\u0001Ê34\u0003Ê1\u0002=4\u00011C3\u000114\u0002=C\u0013ð4\u0009\u001fCS\u0002m34\u0001>Ià14s\u0002>C3\u0001=IÐ1CS\u0002n34\u0013À4\u0009\u001c34s\u0002?I3\u0001;I°1C3%ð6I3\u0001:I 1C3ub3\u0001l4\u00134\u0009\u001934S\u0007\u001bS'`\u0011 V34\u0013\u00004\u0009\u001034\u0009:Z\u0002i³\u0001zC3\u00014\u00091Cs8Z'°6\u001b 74K³4\u00079['Ð6\u001b³4J£4\u001a8Y' yó4I4]s\u0002Ê7M34CÓw\u0002«6u\u00021u\u0007`6\u0013\u0002ëss#ÀY"
  .replace(/./g,c=>                       // unpack each 4-bit character
    (l=" .\n:|;`',0123456")               // l = lookup table of characters
    [(n=c.charCodeAt())&15]+l[n>>4]       // return the two characters in the byte
  )
  .replace(/.\d+/g,c=>                    // run-length decoding
    c[0].repeat(parseInt(c.slice(1),7)+3) // repeat character n + 3 times (base-7)
  )

:eval(`                                   // eval just to allow a for loop here...
  for(;!f(++i););                         // check each second up until a match

  \`${i/60|0} minutes and ${i%60|0} seconds left until your next hit.\`
`))

Compression Algorithms

No Compression, 911 bytes

This is the leaf string with no compression at all.

`                        .
                       .:.
                       :|:
                      .:|:.
                      ::|::
       :.             ::|::             .:
       :|:.          .::|::.          .:|:
       ::|:.         :::|:::         .:|:;
       \`::|:.        :::|:::        .:|::'
        ::|::.       :::|:::       .::|:;
        \`::|::.      :::|:::      .::|::'
         :::|::.     :::|:::     .::|::;
         \`:::|::.    :::|:::    .::|::;'
\`::.      \`:::|::.   :::|:::   .::|::;'     .:;'
 \`:::..    \`;::|::.  :::|:::  .::|:::    ::::;
   \`:::::.    ':|::. :::|::: .::|:'   ,::::;'
     \`:::::.    ':|:::::|:::::|:'   :::::;'
       \`:::::.:::::|::::|::::|::::.,:::;'
          ':::::::::|:::|:::|:::::::;:'
             ':::::::|::|::|:::::::''
                  \`::::::::::;'
                 .:;'' ::: \`\`::.
                      :':':
                        ;`

Run-Length Encoding, 542 bytes

Since there are many consecutive repeated characters, run-length encoding improves the byte count significantly.

` 21.
 20.:.
 20:|:
 19.:|:.
 19::|::
 4:. 10::|:: 10.:
 4:|:. 7.::|::. 7.:|:
 4::|:. 6:0|:0 6.:|:;
 4\`::|:. 5:0|:0 5.:|::'
 5::|::. 4:0|:0 4.::|:;
 5\`::|::. 3:0|:0 3.::|::'
 6:0|::. 2:0|:0 2.::|::;
 6\`:0|::. 1:0|:0 1.::|::;'
\`::. 3\`:0|::. 0:0|:0 0.::|::;' 2.:;'
 \`:0.. 1\`;::|::.  :0|:0  .::|:0 1:1;
 0\`:2. 1':|::. :0|:0 .::|:' 0,:1;'
 2\`:2. 1':|:2|:2|:' 0:2;'
 4\`:2.:2|:1|:1|:1.,:0;'
 7':6|:0|:0|:4;:'
 10':4|::|::|:4''
 15\`:7;'
 14.:;'' :0 \`\`::.
 19:':':
 21;`.replace(/.\d+/g,c=>c[0].repeat(+c.slice(1)+3))

Each character repeated 3 or more times is replaced with the character followed by a decimal number of amount - 3. The encoded string is generated with this code:

uncompressed.replace(/(.)\1{2,}/g,c=>c[0]+(c.length-3))

4-bit Packing + RLE, 364 bytes

There are 9 distinct characters in the original string, which means 4 is the minimum number of bits to represent each. Conveniently, this allows for exactly two character representations to fit into a single byte, enabling a simple (and golf-friendly) decompression algorithm to be used. On top of that, there are 7 characters left over, which allows base-7 run-length encoding to be used before packing. Putting each 4-bit index into the upper and lower nibbles of each compressed character means 2 characters are stored per byte, because the CP-1252 encoding encodes every character under code point 256 as a single byte.

"À\u0019\u0002û1!°?4\u0002ë14!°>C3\u0002=\u0001Ê34\u0003Ê1\u0002=4\u00011C3\u000114\u0002=C\u0013ð4\u0009\u001fCS\u0002m34\u0001>Ià14s\u0002>C3\u0001=IÐ1CS\u0002n34\u0013À4\u0009\u001c34s\u0002?I3\u0001;I°1C3%ð6I3\u0001:I 1C3ub3\u0001l4\u00134\u0009\u001934S\u0007\u001bS'`\u0011 V34\u0013\u00004\u0009\u001034\u0009:Z\u0002i³\u0001zC3\u00014\u00091Cs8Z'°6\u001b 74K³4\u00079['Ð6\u001b³4J£4\u001a8Y' yó4I4]s\u0002Ê7M34CÓw\u0002«6u\u00021u\u0007`6\u0013\u0002ëss#ÀY"
.replace(/./g,c=>(l=" .\n:|;`',0123456")[(n=c.charCodeAt())&15]+l[n>>4])
.replace(/.\d+/g,c=>c[0].repeat(parseInt(c.slice(1),7)+3))

The following code is used to do the run-length encoding and packing:

chars="",max=16;
[...uncompressed].map(c=>~chars.indexOf(c)?0:chars+=c);
base=max-chars.length;
chars+=[...Array(base).keys()].join``;
unpacked=uncompressed.replace(/(.)\1{2,}/g,c=>c[0]+(c.length-3).toString(base));
packed=unpacked.replace(/(.|\n){2}/g,s=> // Note: unpacked length must be even!
  String.fromCharCode(chars.indexOf(s[0])|(chars.indexOf(s[1])<<4)));

Huffman Coding, (Possible Future Improvement)

Certain characters occur much more than others, so a Huffman encoding (variable-length integers representing each character) could save some further bytes. However this will introduce a lot more complexity into the decompression algorithm, so aside from being a lot more work it may also cost more bytes to decompress it than it saves in the encoded string.

\$\endgroup\$
  • \$\begingroup\$ I tried using the same compression as I did for my Easter answer but that takes 421 bytes just to reproduce the string. \$\endgroup\$ – Neil Apr 14 '16 at 11:28
3
\$\begingroup\$

Javascript ES6, 905 bytes

Whew.. The 12 hour clock killed my otherwise short (ahum) solution.
905 bytes to print 898 chars of ascii art yay

_=>(a=(d=Date)(c=new d(),z=O=>Math.abs(new d(a[3]+O)-c)).split` `)[1]=='Apr'&a[2]==20|~a[4].search(/(04|16):20/)?'0,0,2ca2o,0,10e,0,0,1eu9z4,0,22w,1ulajuo,0,bklxc,2zgg,i,120gdfk,8lc,1s1s,15,j0jf9c,7em8,tac,10,190zcw0,tm8w,ksk,hra0ia,mihog0,3aels,wy,iv8e15,b98u80,cvs1s,575,9fm71g,190zcw0,bg7vgg,1t4,148d4lu,121pblx,b9ea68,2b36,iv8hn5,9zleso,15kytz4,98cp,4fwpwk,zir9ud,1h9u3ln,11c6vb4,84kda8,kbuo05,b98u84,14possy,k9su6i,a26hwk,zvft34,kavrpc,1tn1j4,1r3jis,kamgao,a5bhq8,2b2q,beyt6x,j0y810,muhiww,9xl,a5b7s8,14npcwi,1bkfw1s,4ys,14lipw4,kalr6k,0,1aq,kalqzg,0,48m,2j6hqf,zik0zk,0,33mcjk,0,5'.split`,`.map(a=>('0'.repeat(32)+parseInt(a,36).toString(2)).substr(-32)).join``.match(/.{1,3}/g).map(a=>` .:\`|;'
`[parseInt(a,2)]).join``:~~((T=Math.min(z('/4/20'),z(` ${a[1]} ${++a[2]} 0:4:20`),z(' 4:20'),z(' 0:4:20'),z(' 16:20'),z(' 0:16:20'))/1000)/60)+` minutes and ${~~(T%60)} seconds left until your next hit.`

Ungolfed code

Note: The ungolfed code will not execute

function() {
    // (all vars here are actually global)
    var d = Date; // short reference to Date object
    var dateString = Date() // "Tue Apr 12 2016 20:13:00 GMT+0200 (W. Europe Daylight Time)"
    .split` `; // ["Tue", "Apr", "12", ...]
    var currentDate = new Date();
    dateDiff = function(toAppend) {
        return Math.abs( // no dates in the past
            new Date(dateString[3] + toAppend) // new Date('2016' + something)
            - currentDate // substract the current date. 
                          // Thanks JS for the datetime implementation
        );
    }

    if ((dateString[1] == 'Apr' & dateString[2] == 20) |
        ~dateString[4].search(/(04|16):20/)) { // if the time doesn't contain 04:20 or 
                                               // 16:20, this will return -1. ~-1 is 0 
                                               // and thus falsy
        alert(printAscii());
    } else {
        alert(printTimeLeft());
    }
}

function printAscii() {
    var magicString = '0,0,2ca2o,0,10e,0,0,1eu9z4,0,22w,1ulajuo,0,bklxc,2zgg,i,120gdfk,8lc,1s1s,15,j0jf9c,7em8,tac,10,190zcw0,tm8w,ksk,hra0ia,mihog0,3aels,wy,iv8e15,b98u80,cvs1s,575,9fm71g,190zcw0,bg7vgg,1t4,148d4lu,121pblx,b9ea68,2b36,iv8hn5,9zleso,15kytz4,98cp,4fwpwk,zir9ud,1h9u3ln,11c6vb4,84kda8,kbuo05,b98u84,14possy,k9su6i,a26hwk,zvft34,kavrpc,1tn1j4,1r3jis,kamgao,a5bhq8,2b2q,beyt6x,j0y810,muhiww,9xl,a5b7s8,14npcwi,1bkfw1s,4ys,14lipw4,kalr6k,0,1aq,kalqzg,0,48m,2j6hqf,zik0zk,0,33mcjk,0,5' // ;)
    var splitted = magicString.split`,`;
    var binarySequence = splitted.map(function(piece) {
        return ('0'.repeat(32) + // 000000000... this will be padded
            parseInt(a,36) // parseInt('190zcw0',36) -> 000000002722627584
            .toString(2) // -> 10100010010010000000000000000000
        ).substr(-32) // left pad binary sequence
    }).join``; // create one big binary sequence from several smaller
    var groupsOfThree = binarySequence.match(/.{1,3}/g);
    return groupsOfThree.map(function(three) {
        return ` .:\`|;'\n`[parseInt(a,2)]; // magic.. turn 010 into : etc
    }).join``; // create them blaze
}

function printTimeLeft() {
    // this is way longer than it should be.. probably
    var minimumTimeDifference = Math.min(
        dateDiff('/4/20'), // -> Difference between now and 2016/4/20 00:00:00
        dateDiff(' 4:20'), // 2016 04:20:00.. Sadly the 2016 is needed (i think)
        dateDiff(' 0:4:20'), // 2016 00:04:20
        dateDiff(' 16:20'), // Damn 12 hour clock :(
        dateDiff(' 0:16:20'),
        dateDiff(` ${dateString[1]} // 2016 Apr 
                   ${dateString[2]+1} // 2016 Apr 13 (12+1)
                   0:4:20` // Check the first occurrence of 4 minutes and 20 seconds 
                           // the next day
    )
    var timeInSeconds = minimumTimeDifference / 1000;
    return Math.floor(timeInSeconds) + // ~~ works like Math.floor
        ` minutes and ${Math.floor(timeInSeconds%60)} seconds left until your next hit.`;
}

Try it!

f=
_=>(a=(d=Date)(c=new d(),z=O=>Math.abs(new d(a[3]+O)-c)).split` `)[1]=='Apr'&a[2]==20|~a[4].search(/(04|16):20/)?'0,0,2ca2o,0,10e,0,0,1eu9z4,0,22w,1ulajuo,0,bklxc,2zgg,i,120gdfk,8lc,1s1s,15,j0jf9c,7em8,tac,10,190zcw0,tm8w,ksk,hra0ia,mihog0,3aels,wy,iv8e15,b98u80,cvs1s,575,9fm71g,190zcw0,bg7vgg,1t4,148d4lu,121pblx,b9ea68,2b36,iv8hn5,9zleso,15kytz4,98cp,4fwpwk,zir9ud,1h9u3ln,11c6vb4,84kda8,kbuo05,b98u84,14possy,k9su6i,a26hwk,zvft34,kavrpc,1tn1j4,1r3jis,kamgao,a5bhq8,2b2q,beyt6x,j0y810,muhiww,9xl,a5b7s8,14npcwi,1bkfw1s,4ys,14lipw4,kalr6k,0,1aq,kalqzg,0,48m,2j6hqf,zik0zk,0,33mcjk,0,5'.split`,`.map(a=>('0'.repeat(32)+parseInt(a,36).toString(2)).substr(-32)).join``.match(/.{1,3}/g).map(a=>` .:\`|;'
`[parseInt(a,2)]).join``:~~((T=Math.min(z('/4/20'),z(` ${a[1]} ${++a[2]} 0:4:20`),z(' 4:20'),z(' 0:4:20'),z(' 16:20'),z(' 0:16:20'))/1000)/60)+` minutes and ${~~(T%60)} seconds left until your next hit.`

alert(f())

In the following code snippet you can set the date to test the ascii art

day=prompt('Day?', 12);
month=prompt('Month?', 'Apr');
f=
_=>(a=(d=Date)(c=new d(),z=O=>Math.abs(new d(a[3]+O)-c)).split` `)[1]==month&a[2]==day|~a[4].search(/(04|16):20/)?'0,0,2ca2o,0,10e,0,0,1eu9z4,0,22w,1ulajuo,0,bklxc,2zgg,i,120gdfk,8lc,1s1s,15,j0jf9c,7em8,tac,10,190zcw0,tm8w,ksk,hra0ia,mihog0,3aels,wy,iv8e15,b98u80,cvs1s,575,9fm71g,190zcw0,bg7vgg,1t4,148d4lu,121pblx,b9ea68,2b36,iv8hn5,9zleso,15kytz4,98cp,4fwpwk,zir9ud,1h9u3ln,11c6vb4,84kda8,kbuo05,b98u84,14possy,k9su6i,a26hwk,zvft34,kavrpc,1tn1j4,1r3jis,kamgao,a5bhq8,2b2q,beyt6x,j0y810,muhiww,9xl,a5b7s8,14npcwi,1bkfw1s,4ys,14lipw4,kalr6k,0,1aq,kalqzg,0,48m,2j6hqf,zik0zk,0,33mcjk,0,5'.split`,`.map(a=>('0'.repeat(32)+parseInt(a,36).toString(2)).substr(-32)).join``.match(/.{1,3}/g).map(a=>` .:\`|;'
`[parseInt(a,2)]).join``:~~((T=Math.min(z('/4/20'),z(` ${a[1]} ${++a[2]} 0:4:20`),z(' 4:20'),z(' 0:4:20'),z(' 16:20'),z(' 0:16:20'))/1000)/60)+` minutes and ${~~(T%60)} seconds left until your next hit.`

alert(f())

\$\endgroup\$
  • \$\begingroup\$ I think an invalid time like 15:16:20 will be a match on your regex and accidentally display the magic string? \$\endgroup\$ – Value Ink Apr 12 '16 at 19:33
  • \$\begingroup\$ @KevinLau Dang, you're right. Will fix that tomorrow.. \$\endgroup\$ – Bassdrop Cumberwubwubwub Apr 12 '16 at 19:49
  • \$\begingroup\$ I don't think () are necessary for the date constructor: c=new d(), ... can become just c=new d, ... \$\endgroup\$ – Cyoce Apr 13 '16 at 2:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.