Skip to content

Instantly share code, notes, and snippets.

@geohot
Created December 16, 2019 17:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save geohot/f627e1c3d55c23bd2f111b192e54bca6 to your computer and use it in GitHub Desktop.
Save geohot/f627e1c3d55c23bd2f111b192e54bca6 to your computer and use it in GitHub Desktop.
AoC 16 Part 2, works for all offsets
#include <vector>
int main() {
//char cdat[] = "12345678";
char cdat[] = "59791911701697178620772166487621926539855976237879300869872931303532122404711706813176657053802481833015214226705058704017099411284046473395211022546662450403964137283487707691563442026697656820695854453826690487611172860358286255850668069507687936410599520475680695180527327076479119764897119494161366645257480353063266653306023935874821274026377407051958316291995144593624792755553923648392169597897222058613725620920233283869036501950753970029182181770358827133737490530431859833065926816798051237510954742209939957376506364926219879150524606056996572743773912030397695613203835011524677640044237824961662635530619875905369208905866913334027160178";
//char cdat[] = "03036732577212944063491565474664";
int N = strlen(cdat)*10000;
int *dat = new int[N];
int *cc = new int[N+1];
for (int j = 0; j < N; j+=strlen(cdat)) {
for (int i = 0; i < strlen(cdat); i++) dat[j+i] = cdat[i] - '0';
}
cdat[7] = '\0';
int off = atoi(cdat);
std::vector<std::vector<int> > blks(N);
printf("building\n");
for (int i = 0; i < N; i++) {
int off = i;
blks[i].push_back(off);
while (off < N) {
off += (i+1);
int t = std::min(N, off);
blks[i].push_back(t);
}
blks[i].resize((blks[i].size()/2)*2);
//printf("%d: %d\n", i, blks[i].size());
}
printf("done\n");
for (int phase = 0; phase < 100; phase++) {
printf("%d\n", phase);
cc[0] = 0;
for (int i = 0; i < N; i++) {
cc[i+1] = cc[i] + dat[i];
}
for (int i = 0; i < N; i++) {
int acc = 0;
int pol = 1;
//printf("%d\n", i);
for (int j = 0; j < blks[i].size(); j += 2) {
int t1 = blks[i][j];
int t2 = blks[i][j+1];
//printf("%d %d\n", t1, t2);
acc += (cc[t2]-cc[t1]) * pol;
pol *= -1;
}
// fix
if (acc<0) acc *= -1;
dat[i] = acc%10;
}
}
//for (int i = 0; i < N; i++) printf("%d", dat[i]); printf("\n");
for (int i = off; i < off+8; i++) printf("%d", dat[i]); printf("\n");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment