-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathknot-again.c
167 lines (129 loc) · 3.27 KB
/
knot-again.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include <limits.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#define ROUNDS 64
#define ROWS 128
#define COLS 128
#define SIZE 256
int elem[SIZE];
#define EXTRA_LENGHTS 5
const int extra[EXTRA_LENGHTS] = { 17, 31, 73, 47, 23 };
#define HASH_LEN 16
uint8_t hash[HASH_LEN];
void reset_array(void) {
for(int i = 0; i < SIZE; ++i) elem[i] = i;
}
void reverse(const int startPos, const int length) {
const int endPos = (startPos + length - 1) % SIZE;
for(int i = 0; i < length/2; ++i) {
int a_p = (startPos+i) % SIZE;
int b_p = (endPos-i);
if(b_p < 0) b_p += SIZE;
int a_v = elem[a_p];
int b_v = elem[b_p];
elem[a_p] = b_v;
elem[b_p] = a_v;
}
}
void calc_hash(char *const buffer, const size_t buflen) {
reset_array();
int pos = 0;
int skip = 0;
for(int round = 0; round < ROUNDS; ++round) {
for(int b = 0; b < buflen; ++b) {
int length = buffer[b];
// visualise(pos, length);
reverse(pos, length);
pos = (pos + length + skip) % SIZE;
skip += 1;
}
for(int e = 0; e < EXTRA_LENGHTS; ++e) {
int length = extra[e];
// visualise(pos, length);
reverse(pos, length);
pos = (pos + length + skip) % SIZE;
skip += 1;
}
}
for(int group = 0; group < 16; ++group) {
uint8_t grphash = 0;
for(int member = 0; member < 16; ++member) {
grphash ^= elem[group*16 + member];
}
hash[group] = grphash;
}
}
void print_hash(void) {
for(int group = 0; group < 16; ++group) {
printf("%02x", hash[group]);
}
putc('\n', stdout);
}
int count_bits(void) {
int count = 0;
for(int h = 0; h < HASH_LEN; ++h) {
for(int b = 0; b < 8; ++b) {
if(hash[h] & (1 << b)) ++count;
}
}
return count;
}
uint16_t disk[ROWS * COLS];
#define DSKOFF(x, y) ( ( (y) * COLS ) + (x) )
int mark_neighbours(const int x, const int y, const int reg) {
int offset = DSKOFF(x, y);
if(disk[offset] != 1) return 0;
disk[offset] = reg;
// printf("marking neighbours of %d:%d\n", x, y);
if(x > 0) mark_neighbours(x-1, y, reg);
if(x < COLS-1) mark_neighbours(x+1, y, reg);
if(y > 0) mark_neighbours(x, y-1, reg);
if(y < ROWS-1) mark_neighbours(x, y+1, reg);
return 1;
}
int calc_regions(void) {
const int start_reg = 2;
int reg = start_reg;
for(int y = 0; y < ROWS; ++y) {
for(int x = 0; x < COLS; ++x) {
reg += mark_neighbours(x, y, reg);
}
}
return reg - start_reg;
}
int main(void) {
char input[128];
if(fgets(input, sizeof(input), stdin) == NULL) {
input[0] = '\0';
} else {
int in_last = strlen(input)-1;
if(input[in_last] == '\n') input[in_last] = '\0';
}
printf("input: \"%s\" (%lu)\n", input, strlen(input));
int total_bits = 0;
for(int row = 0; row < 128; ++row) {
char buffer[128];
int buflen = snprintf(buffer, sizeof(buffer), "%s-%d", input, row);
calc_hash(buffer, buflen);
for(int h = 0; h < HASH_LEN; ++h) {
uint8_t hex = hash[h];
for(int b = 0; b < 8; ++b) {
int offset = DSKOFF(h*8 + b, row);
disk[offset] = !!(hex & (1 << (7-b)));
total_bits += disk[offset];
}
}
}
for(int y = 0; y < 8; ++y) {
for(int x = 0; x < 8; ++x) {
int offset = DSKOFF(x, y);
putc(disk[offset] ? '#' : '.', stdout);
}
putc('\n', stdout);
}
printf("total bits: %d\n", total_bits);
printf("total regions: %d\n", calc_regions());
return 0;
}