-
-
Save maksverver/aeda195b76b904aeb1ef98b071bfb4fa to your computer and use it in GitHub Desktop.
Advent of Code 2018 day 14
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> | |
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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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