-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path03-1.c
45 lines (39 loc) · 1.01 KB
/
03-1.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
#define _GNU_SOURCE
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#include <assert.h>
#include <stdbool.h>
#include <stdlib.h>
#include <math.h>
int main(void)
{
FILE *fin;
char *val;
int64_t gamma = 0, epsilon = 0, len;
int64_t *counters;
uint64_t bytes;
if ((fin = fopen("03-input.txt", "r")))
{
len = getline(&val, &bytes, fin) - 1;
if ((counters = malloc(sizeof(int64_t) * len)))
{
do
{
for (uint8_t i = 0; val[i] != '\0'; ++i)
counters[i] += (val[i] * 2) - (('0' * 2) + 1);
} while (getline(&val, &bytes, fin) != -1);
for (int8_t i = 0; i < len; ++i)
{
gamma += ((counters[i] > 0) << (len - i - 1));
epsilon += ((counters[i] < 0) << (len - i - 1));
}
printf("%ld\n", gamma * epsilon);
}
fclose(fin);
}
else
{
printf("Can't open the file\n");
}
}