-
-
Save maksverver/97cc8a86cbe61c1ed2465384a2329c69 to your computer and use it in GitHub Desktop.
Advent of Code 2018 day 9 in C
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
int Solve(int players, int marbles) { | |
int *scores = calloc(players, sizeof(int)); | |
int *prev = calloc(marbles, sizeof(int)); | |
int *next = calloc(marbles, sizeof(int)); | |
int i = 0; | |
for (int j = 1; j < marbles; ++j) { | |
if (j%23 != 0) { | |
i = next[i]; | |
int k = next[i]; | |
next[i] = j; | |
prev[j] = i; | |
next[j] = k; | |
prev[k] = j; | |
i = j; | |
} else { | |
for (int n = 0; n < 7; ++n) { | |
i = prev[i]; | |
} | |
int player = (j - 1)%players; | |
scores[player] += i + j; | |
int h = prev[i]; | |
i = next[i]; | |
next[h] = i; | |
prev[i] = h; | |
} | |
} | |
int max_score = 0; | |
for (int i = 0; i < players; ++i) { | |
if (scores[i] > max_score) { | |
max_score = scores[i]; | |
} | |
} | |
free(scores); | |
free(prev); | |
free(next); | |
return max_score; | |
} | |
int main() { | |
int players; | |
int last_marble; | |
if (scanf("%d players; last marble is worth %d points", &players, &last_marble) == 2) { | |
printf("%d %d\n", players, last_marble); | |
printf("%d\n", Solve(players, last_marble + 1)); | |
printf("%d\n", Solve(players, (last_marble * 100) + 1)); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment