-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday13b.c
125 lines (98 loc) · 2.24 KB
/
day13b.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
// Licensed under the MIT License.
// Point of Incidence Part 2
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#define DIMENSION 32
typedef unsigned long BitVector;
typedef unsigned long* BitMatrix;
bool bit_vector_is_pow_2(BitVector instance)
{
return instance > 0 && (instance & (instance - 1)) == 0;
}
void bit_matrix_clear(BitMatrix instance)
{
memset(instance, 0, DIMENSION * sizeof(unsigned long));
}
static bool mid(BitMatrix matrix, int index)
{
int left = index;
int right = index + 1;
bool smudged = false;
while (left >= 0 && matrix[right])
{
if (matrix[left] != matrix[right])
{
if (smudged || !bit_vector_is_pow_2(matrix[left] ^ matrix[right]))
{
return false;
}
smudged = true;
}
left--;
right++;
}
return smudged;
}
static int realize(BitMatrix matrix)
{
for (int i = 0; matrix[i + 1]; i++)
{
if (mid(matrix, i))
{
return i + 1;
}
}
return 0;
}
static int realize_xy(BitMatrix x, BitMatrix y)
{
int result = realize(y);
if (result)
{
return 100 * result;
}
return realize(x);
}
int main(void)
{
int i = 0;
long total = 0;
BitVector x[DIMENSION] = { 0 };
BitVector y[DIMENSION] = { 0 };
char buffer[DIMENSION + 2];
clock_t start = clock();
while (fgets(buffer, sizeof buffer, stdin))
{
if (buffer[0] == '\n')
{
i = 0;
total += realize_xy(x, y);
bit_matrix_clear(x);
bit_matrix_clear(y);
continue;
}
int j = 0;
char current;
while ((current = buffer[j]))
{
switch (current)
{
case '#':
y[i] = (y[i] << 1) | 1;
x[j] = (x[j] << 1) | 1;
break;
case '.':
y[i] <<= 1;
x[j] <<= 1;
break;
}
j++;
}
i++;
}
total += realize_xy(x, y);
printf("13b %ld %lf\n", total, (double)(clock() - start) / CLOCKS_PER_SEC);
return 0;
}