Paste: aoc9 FAST

Author: jon
Mode: c
Date: Sun, 9 Dec 2018 23:19:39
Plain Text |
//CFLAGS="-O3 -std=c11"

#include "stdlib.h"
#include "stdio.h"
#include "time.h"

typedef struct node {
	int val;
	struct node* prev;
	struct node* next;
} node_t ;

int count = 0;
node_t pool[20000000];

int main(int argc, char** argv) {
	int marbles;
	int players;
	unsigned int scores[1000] = { 0 };
	sscanf(argv[1], "%d", &players);
	sscanf(argv[2], "%d", &marbles);

	struct timespec ts;
	timespec_get(&ts, TIME_UTC);

	node_t* curnode = &pool[count];
	count++;
	curnode->val = 0;
	curnode->next = curnode->prev = curnode;
	int i=0;
	int player = 0;
	int marble = 0;
	int next23 = 23;
	for (i=0;i<marbles; i++ ) {
		marble++;
		int playerplus1 = player + 1;
		player = playerplus1 == players ? 0 : playerplus1 ;
		if (marble==next23) {
			next23+=23;

			node_t* next = curnode->prev->prev->prev->prev->prev; 
			curnode = next->prev;
			node_t* prev = curnode->prev; 
			int othernode = prev->val;
			prev->next = next;
			next->prev = prev;
			scores[player] += (marble + othernode);
		} else {
			node_t* node = curnode->next;
			curnode = &pool[count];
			count++;
			node_t* tmp = node->next;
			curnode->val=i;

			node->next = curnode;
			curnode->prev = node;

			curnode->next = tmp;
			tmp->prev = curnode;

		}
	}

	unsigned int max = 0;
	for (i=0; i<players; i++) {
		//printf("%d %u\n",i, scores[i]);
		max = scores[i] > max ? scores[i] : max;
	}


{
	struct timespec ts2;
	timespec_get(&ts2, TIME_UTC);
	long long nanos = (ts2.tv_sec - ts.tv_sec)*1000000000 + ts2.tv_nsec - ts.tv_nsec;
	printf("%lldms, %lldmicros, %lld nanos\n",nanos/1000000,nanos/1000,nanos);
}


	printf("%u\n", max);
}

Annotation: FAST and CORRECT :)

Author: jon
Mode: c
Date: Mon, 10 Dec 2018 11:33:57
Plain Text |
//CFLAGS="-O3 -std=c11"

#include "stdlib.h"
#include "stdio.h"
#include "time.h"

typedef struct node {
	int val;
	struct node* prev;
	struct node* next;
} node_t ;

int count = 0;
node_t pool[20000000];

int main(int argc, char** argv) {
	int marbles;
	int players;
	unsigned int scores[1000] = { 0 };
	sscanf(argv[1], "%d", &players);
	sscanf(argv[2], "%d", &marbles);

	struct timespec ts;
	timespec_get(&ts, TIME_UTC);

	node_t* curnode = &pool[count];
	count++;
	curnode->val = 0;
	curnode->next = curnode->prev = curnode;
	int i=0;
	int player = 0;
	int marble = 0;
	int next23 = 23;
	for (i=0;i<marbles; i++ ) {
		marble++;
		int playerplus1 = player + 1;
		player = playerplus1 == players ? 0 : playerplus1 ;
		if (marble==next23) {
			next23+=23;

			curnode = curnode->prev->prev->prev->prev->prev->prev;
			node_t* prev = curnode->prev; 
			int othernode = prev->val;
            prev = prev->prev;
			prev->next = curnode;
			curnode->prev = prev;
			scores[player] += (marble + othernode);
		} else {
			node_t* node = curnode->next;
			curnode = &pool[count];
			count++;
			node_t* tmp = node->next;
			curnode->val=marble;

			node->next = curnode;
			curnode->prev = node;

			curnode->next = tmp;
			tmp->prev = curnode;

		}
	}

	unsigned int max = 0;
	for (i=0; i<players; i++) {
		//printf("%d %u\n",i, scores[i]);
		max = scores[i] > max ? scores[i] : max;
	}


{
	struct timespec ts2;
	timespec_get(&ts2, TIME_UTC);
	long long nanos = (ts2.tv_sec - ts.tv_sec)*1000000000 + ts2.tv_nsec - ts.tv_nsec;
	printf("%lldms, %lldmicros, %lld nanos\n",nanos/1000000,nanos/1000,nanos);
}


	printf("%u\n", max);
}

New Annotation

Summary:
Author:
Mode:
Body: