Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <stdint.h>
- #include <stdbool.h>
- #define filename "input_22_1.txt"
- #define HLEN 64321
- typedef struct {
- uint32_t diffcombo[2000]; // each diffcombo consist of 4 int8_t-s in exact order, which creates a 32-bit code.
- int8_t last[2000];
- } data_t;
- typedef struct {
- uint32_t addr;
- bool pending;
- int8_t pendingvalue;
- int32_t value;
- } object_t;
- data_t data;//[1550];
- object_t hashmap[HLEN];
- uint64_t calcSecret(uint64_t secret)
- {
- uint64_t new;
- new = secret * 64;
- secret = new ^ secret; //mix
- new = secret % 16777216; //prune
- new = new / 32;
- secret = new ^ secret; //mix
- new = secret % 16777216; //prune
- new = new * 2048;
- secret = new ^ secret; //mix
- new = secret % 16777216; //prune
- return new;
- }
- void addLastNrIntoMap(uint32_t address, int8_t value)
- {
- uint16_t index = address % HLEN;
- do {
- if ((hashmap[index].addr == 0) || (hashmap[index].addr == address)) {
- hashmap[index].addr = address;
- if (!hashmap[index].pending) {
- hashmap[index].pending = true;
- hashmap[index].pendingvalue = value;
- }
- return;
- }
- index++;
- index %= HLEN;
- } while (1);
- }
- void transferPendingValues(void)
- {
- for (int i=0;i<HLEN;i++) {
- hashmap[i].value += hashmap[i].pendingvalue;
- hashmap[i].pending = false;
- };
- }
- uint64_t calc2000th(uint64_t secret, data_t * dptr)
- {
- uint64_t new = secret;
- int i =0;
- dptr->last[0] = (int8_t)(-100);
- dptr->diffcombo[0] = 0;
- do {
- new = calcSecret(new);
- i++;
- dptr->last[i] = (int8_t)(new % 10);
- dptr->diffcombo[i] = (dptr->diffcombo[i-1] << 8) + (uint8_t)(dptr->last[i] - dptr->last[i-1]);
- if (i>3)
- addLastNrIntoMap(dptr->diffcombo[i], dptr->last[i]);
- } while (i<2000);
- transferPendingValues();
- return new;
- }
- int main()
- {
- FILE * f;
- uint64_t result1 = 0;
- int num;
- int i=0;
- f = fopen(filename, "r");
- if (f != NULL) {
- char line[160] = {0};
- while (!feof(f)) {
- if ((NULL != fgets(line, sizeof(line), f)) && (strlen(line) > 1)) {
- num = atoi(line);
- result1 += calc2000th(num, &data/*[i]*/);
- i++;
- }
- }
- fclose(f);
- }
- printf("Result 1 (%i numbers): %lld\n\r", i, result1);
- int max=0;
- uint32_t maxdiff=0;
- for (int h=0;h<HLEN;h++) {
- if ((hashmap[h].addr != 0) && (hashmap[h].value > max)) {
- max = hashmap[h].value;
- maxdiff = hashmap[h].addr;
- }
- }
- printf("Result 2: %i\n\r", max);
- printf(" (%i,%i,%i,%i)\n\r", (int8_t)(maxdiff>>24), (int8_t)(maxdiff>>16), (int8_t)(maxdiff>>8), (int8_t)(maxdiff>>0));
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement