-
Notifications
You must be signed in to change notification settings - Fork 0
/
generators.c
52 lines (43 loc) · 1.23 KB
/
generators.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#define GEN1_FACTOR 16807ull
#define GEN2_FACTOR 48271ull
#define MODULUS 2147483647ull
#define GEN1_MOD 4ull
#define GEN2_MOD 8ull
int main(int argc, char** argv) {
int mode = 0;
int verbose = 0;
for(int a = 1; a < argc; ++a) {
if(strcmp(argv[a], "--part1") == 0)
mode = 1;
else if(strcmp(argv[a], "--part2") == 0)
mode = 2;
else if(strcmp(argv[a], "--verbose") == 0)
verbose = 1;
else printf("Unknown option \"%s\"\n", argv[a]), exit(EXIT_FAILURE);
}
if(mode == 0) {
printf("You must provide a mode of operation: \"--part1\" or \"--part2\"\n");
exit(EXIT_FAILURE);
}
uint64_t gen1, gen2, rounds;
scanf("%lu %lu %lu", &gen1, &gen2, &rounds);
uint64_t score = 0;
while(rounds --> 0) {
if(mode == 1) {
gen1 = (gen1 * GEN1_FACTOR) % MODULUS;
gen2 = (gen2 * GEN2_FACTOR) % MODULUS;
} else {
do { gen1 = (gen1 * GEN1_FACTOR) % MODULUS; } while(gen1 % GEN1_MOD);
do { gen2 = (gen2 * GEN2_FACTOR) % MODULUS; } while(gen2 % GEN2_MOD);
}
uint16_t low1 = gen1;
uint16_t low2 = gen2;
if(low1 == low2) ++score;
if(verbose) printf("> %20lu:%20lu -> %2x:%2x\n", gen1, gen2, low1, low2);
}
printf("score: %lu\n", score);
}