Skip to content

Instantly share code, notes, and snippets.

@FlorianCassayre
Created December 9, 2018 06:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FlorianCassayre/909c2325866e9f19d5d3090c4fff0ddc to your computer and use it in GitHub Desktop.
Save FlorianCassayre/909c2325866e9f19d5d3090c4fff0ddc to your computer and use it in GitHub Desktop.
#include <stddef.h>
#include <malloc.h>
#include <stdio.h>
#include <time.h>
typedef struct linked_t {
struct linked_t* previous;
struct linked_t* next;
int value;
} linked_t;
void insert_after(linked_t* list, int i) {
linked_t* new = malloc(sizeof(linked_t));
new->previous = list;
new->next = list->next;
new->value = i;
list->next = new;
new->next->previous = new;
}
int remove_current(linked_t* list) {
list->previous->next = list->next;
list->next->previous = list->previous;
int v = list->value;
free(list);
return v;
}
int main(void) {
const clock_t start = clock();
const int players = 412;
const int points = 7164600;
unsigned long* scores = malloc(players * sizeof(long));
for(int i = 0; i < players; i++) {
scores[i] = 0;
}
linked_t* list = malloc(sizeof(linked_t));
list->previous = list;
list->next = list;
list->value = 0;
int player = 0;
for(int i = 1; i <= points; i++) {
if(i % 23 != 0) {
list = list->next;
insert_after(list, i);
list = list->next;
} else {
for(int j = 0; j < 6; j++) {
list = list->previous;
}
int v = remove_current(list->previous);
scores[player] += v + i;
}
player = (player + 1) % players;
}
long max = 0;
for(int i = 0; i < players; i++) {
if(scores[i] > max) {
max = scores[i];
}
}
printf("%lu\n", max);
const clock_t stop = clock();
printf("(result in %f seconds)\n", (stop - start) / (double) CLOCKS_PER_SEC);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment