Skip to content

Instantly share code, notes, and snippets.

@maksverver
Created December 14, 2018 13:13
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 maksverver/aeda195b76b904aeb1ef98b071bfb4fa to your computer and use it in GitHub Desktop.
Save maksverver/aeda195b76b904aeb1ef98b071bfb4fa to your computer and use it in GitHub Desktop.
Advent of Code 2018 day 14
#include <stdio.h>
#include <stdlib.h>
static int *scores;
static size_t size;
static size_t capacity;
static int current;
void AddScore(int x) {
if (size == capacity) {
capacity = capacity == 0 ? 16 : 2*capacity;
scores = realloc(scores, sizeof(int)*capacity);
}
scores[size++] = x;
current = (10*current + x)%1000000;
}
void Solve(int target) {
AddScore(3);
AddScore(7);
for (int i = 0, j = 1;;) {
int x = scores[i] + scores[j];
if (x < 10) {
AddScore(x);
if (current == target) break;
} else {
AddScore(x/10);
if (current == target) break;
AddScore(x - x/10*10);
if (current == target) break;
}
i = (i + 1 + scores[i]);
while (i >= size) i -= size;
j = (j + 1 + scores[j]);
while (j >= size) j -= size;
}
}
int main() {
int target = 0;
if (scanf("%d", &target) != 1) {
return 1;
}
Solve(target);
printf("%d\n", size - 6);
return 0;
}
@veldsla
Copy link

veldsla commented Dec 14, 2018

Regel 27 / 29. Je weet dat de scores niet boven de 9 kunnen uitkomen, dus x/10 is altijd 1 en x/10*10 is altijd 10 als x>=10

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment