x86 (32-bit) assembly, 43 bytes (13 bytes code + 30 bytes data)
This routine expects input in ax
as a signed short giving number of days since 1950-01-01, and returns output in al
(clobbering edi
along the way).
.text
.globl eu_members
eu_members:
mov $.Ltbl-1, %edi
.Lloop:
inc %edi
scas (%edi), %ax
jg .Lloop
mov (%edi), %al
ret
.section .rodata
.Ltbl:
.short 2921
.byte 0
.short 8400
.byte 6
.short 11322
.byte 9
.short 13148
.byte 10
.short 16435
.byte 12
.short 19843
.byte 15
.short 20818
.byte 25
.short 23191
.byte 27
.short 25597
.byte 28
.short 32767
.byte 27
Exact opcodes of the code part (in a linked context so that the relocation of .Ltbl doesn't confuse things):
0804930b <eu_members>:
804930b: bf 49 a0 04 08 mov $0x804a049,%edi
8049310: 47 inc %edi
8049311: 66 af scas %es:(%edi),%ax
8049313: 7f fb jg 8049310 <eu_members+0x5>
8049315: 8a 07 mov (%edi),%al
8049317: c3 ret
And here's the test harness I used (note that the assembly code is not PIC safe, so on Debian or Ubuntu systems you will need to pass -no-pie
to gcc):
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <string.h>
unsigned char eu_members_wrap(short days_since_1950_01_01) {
unsigned char res;
__asm__("call eu_members" : "=a"(res) : "0"(days_since_1950_01_01) : "edi", "cc");
return res;
}
int main(int argc, char* argv[]) {
struct tm tm;
time_t epoch, req;
short days_since_epoch;
memset(&tm, 0, sizeof(tm));
tm.tm_year = 50;
tm.tm_mon = 0;
tm.tm_mday = 1;
epoch = mktime(&tm);
memset(&tm, 0, sizeof(tm));
tm.tm_year = atoi(argv[1]) - 1900;
tm.tm_mon = atoi(argv[2]) - 1;
tm.tm_mday = atoi(argv[3]);
req = mktime(&tm);
days_since_epoch = (((unsigned int) (req - epoch)) / (24 * 60 * 60));
/* printf("epoch = %ld, req = %ld\n", epoch, req);
printf("Raw number of days: %hd\n", days_since_epoch); */
printf("EU members: %hhu\n", eu_members_wrap(days_since_epoch));
return 0;
}
1I see the Python answer is using an input as the number of days since
1957-12-31
and the JavaScript answer is taking an input in the formatyyyyMM
without thedd
. Are these both allowed according to the "in any reasonable format"? Both would save bytes in my answer as well. – Kevin Cruijssen – 2020-01-31T12:35:55.087@KevinCruijssen Number of days since some epoch is OK.
yyyyMM
is not, as it does not encode a date. – Robin Ryder – 2020-01-31T12:49:20.68710I feel like in any reasonable format is not objective enough... – Luis Mendo – 2020-01-31T13:19:09.273
1@LuisMendo I think it's OK. It just has to uniquely specify the day. Are you worried there is some possible loophole? – Anush – 2020-01-31T14:31:57.313
3@Anush The problem is how to tell if a format qualifies as "reasonable" or not. For instance, I wouldn't have guessed that Number of days since some epoch (or even months?) does. So a clearer specification is needed – Luis Mendo – 2020-01-31T15:58:32.893
2@LuisMendo I have clarified the input format. – Robin Ryder – 2020-01-31T17:09:40.830
@RobinRyder Thanks. I have retracted my close vote – Luis Mendo – 2020-01-31T18:15:37.887
Note that the new input spec invalidates some answrs. You should notify the authors so they can fix them – Luis Mendo – 2020-01-31T18:16:32.547
@luismendo Thanks. Yes, I plan on notifying them as soon as I get back to a desktop computer. – Robin Ryder – 2020-01-31T18:19:02.303
1Your data is incorrect as the European Union was founded on November 1, 1993 – Rene – 2020-01-31T22:24:00.273
1
Looks like I was off by a year or so :/
– caird coinheringaahing – 2020-01-31T22:53:18.133@Rene I am also considering the EEC as the EU's predecessor. – Robin Ryder – 2020-01-31T23:01:00.487
1@Robin Ryder, then you should refer to the EEC, not to the EU – Rene – 2020-01-31T23:21:40.407
>
1Should that be
-Inf
as the starting point for the first line? – ilkkachu – 2020-02-01T15:01:03.987@ilkkachu You don't need to be able to handle dates Before the Common Era. – Robin Ryder – 2020-02-01T15:04:59.530
First, I thought you missed the addition of the GDR to EEC, but then I realisized that the GDR was added to the Federal Republic of Germany, and still counting as one country. So your numbers are still correct. – Julian Egner – 2020-02-02T21:47:02.820