add a C version of the 2 problem

I wanted to check how much faster a C version would be. As it turns out
it is *MUCH* faster, the python version takes ca. 12s, the C version
takes a few milliseconds.
This commit is contained in:
Juan José Gutiérrez de Quevedo Pérez 2020-12-15 11:41:46 +01:00
parent dd2497438d
commit 2c8a012400

31
15/2.c Normal file
View file

@ -0,0 +1,31 @@
#include <stdio.h>
#include <stdint.h>
uint32_t content[] = {2,0,6,12,1,3};
uint32_t spoken[30000000] = {0};
int main(int argc, char *argv[])
{
uint32_t new_spoken = 0;
uint32_t turn = 1;
uint32_t last_turn = 0;
for(int i = 0; i<sizeof(content)/sizeof(uint32_t); i++, turn++)
spoken[content[i]] = turn;
for(;turn <= 30000000; turn++)
{
if(!last_turn)
new_spoken = 0;
else
new_spoken = turn - 1 - last_turn;
last_turn = spoken[new_spoken];
spoken[new_spoken] = turn;
}
printf("%d\n", new_spoken);
return 0;
}