Skip to content

Instantly share code, notes, and snippets.

@randrew

randrew/part2.c Secret

Last active December 14, 2020 01:09
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 randrew/c7fa048f0079fa6058853f076221da7a to your computer and use it in GitHub Desktop.
Save randrew/c7fa048f0079fa6058853f076221da7a to your computer and use it in GitHub Desktop.
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