I have a colleague at work that works from home every Tuesday and Thursday. At around 8:00 AM he sends us a message with the following text:
Hello today I'm working from home
In order to relieve him from the burden of doing this every day he stays at home, we would like to automate this task for him.
The challenge
Write in as few bytes as possible a piece of code that:
- Receives the current time: your code may receive values for the current year, month (1-12), day of month (1-31), hour (0-23) and minute (0-59) and the day of the week (you can choose whether this number starts from 0 or 1, and if 0/1 means Sunday, Monday or any other day); alternatively you may receive a structure such as
Date
,DateTime
,Calendar
or any other time-related structure, if your language allows it. You can also receive a string with the date inyyyyMMddHHmm
if you want, or two separate strings for date and time, and then an integer with the day of week. Feel free. - Returns two consistent truthy and falsey values, indicating if the message must be sent to the work chat or not.
Rules
- This piece of code is assumed to be invoked periodically. The exact periodicity is irrelevant, nonetheless.
- The truthy value must be returned if the day of week is Tuesday or Thursday and the time is 8:00 AM with an error margin of 10 minutes (from 7:50 to 8:10 inclusive).
- The truthy value must be sent only if it is the first time the code is invoked between those hours for the specified day. We don't want the bot to send the same message several times in a row. The way you manage this restriction will be entirely up to you.
- Your code may be an independent program executed repeatedly or it may be part of a bigger code that is always running. Your choice.
- You may assume that there will be no reboots between executions of the code.
- You may assume that the date will always be correct.
- Explanations about your code and specifically about the method used to achieve persistence are encouraged.
Examples
(Week starts on Monday: 1, the following invokations will be made in succession)
2018,08,27,08,00,1 = falsey (not Tuesday or Thursday)
2018,08,28,07,45,2 = falsey (out of hours)
2018,08,28,07,55,2 = truthy (first time invoked this day at the proper hours)
2018,08,28,08,05,2 = falsey (second time invoked this day at the proper hours)
2018,08,28,08,15,2 = falsey (out of hours)
2018,08,29,08,00,3 = falsey (not Tuesday or Thursday)
2018,08,29,18,00,3 = falsey (not Tuesday or Thursday)
2018,08,30,07,49,4 = falsey (out of hours)
2018,08,30,07,50,4 = truthy (first time invoked this day at the proper hours)
2018,08,30,07,50,4 = falsey (second time invoked this day at the proper hours)
2018,08,30,08,10,4 = falsey (third time invoked this day at the proper hours)
2018,08,30,08,11,4 = falsey (out of hours)
2018,09,04,08,10,2 = truthy (first time invoked this day at the proper hours)
This is code-golf, so may the shortest code for each language win!
YYYYmmdd
and the time as wellHHss
? \$\endgroup\$