Skip to content

Instantly share code, notes, and snippets.

@randrew

randrew/part2.c Secret

Last active Dec 14, 2020
Embed
What would you like to do?
aoc day 13 part 2, non-working
// Build with cc -std=c99 -o part2 part2.c
//
// Feed it input with stdin. It doesn't matter if your input has an extra
// starting line as in the example input or not. It will be ignored if it
// exists.
#include <stdio.h>
#include <stdlib.h>
typedef signed long long Num;
int main(void)
{
Num input = 0, nids = 0, *ids = malloc(sizeof(Num) * 5000);
int more = 0, c;
get:
c = getchar();
switch (c)
{
case '\n':
more = 0;
goto get;
case EOF:
ids[nids++] = input;
goto ready;
case ',':
ids[nids++] = input;
input = 0;
goto get;
default:
if (!more) nids = input = 0;
if (c >= '0' && c <= '9') input = input * 10 + (Num)(c - '0');
more = 1;
goto get;
}
// Finished reading input, do calculation
ready:;
Num t = 0, d = ids[0];
for (Num i = 1; i < nids; i++)
{
Num id = ids[i];
if (id == 0) continue;
Num m = 0;
for (;; m++)
{
if (id - ((t + m * d) % id) == i) break;
}
printf("%lld\n", t + m * d);
t = t + m * d;
d = d * id;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment