Browse Source

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.
Juan José Gutiérrez de Quevedo Pérez 9 months ago
parent
commit
2c8a012400
1 changed files with 31 additions and 0 deletions
  1. 31 0
      15/2.c

+ 31 - 0
15/2.c

@@ -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;
+}