summaryrefslogtreecommitdiff
path: root/03.c
blob: d783b79e131547dac1bc1dc67ff25a5fb41f1c16 (plain)
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
#include <stdlib.h>
#include <stdio.h>
#include <err.h>
#include "input.h"

#define INPUT "input/03.txt"
#define INPUT_SIZE 12

void part1(struct input_str* input) {
	int oneCount[INPUT_SIZE];
	// TODO is that necessary ?
	for(int k = 0; k < INPUT_SIZE; k++) {
		oneCount[k] = 0;
	}

	for(size_t i = 0; i < input->line_count; i++) {
		char* line = input->lines[i];
		for(int j = 0; j < INPUT_SIZE; j++) {
			if(line[j] == '1')
				oneCount[j]++;
		}
	}

	// null terminated string	
	char binary[INPUT_SIZE + 1];
	for(int h = 0; h < INPUT_SIZE; h++) {
		binary[h] = oneCount[h] > input->line_count / 2.0f ? '1' : '0';
	}
	binary[INPUT_SIZE] = '\0';
	char* endp = NULL;
	unsigned long gamma = strtoul(binary, &endp, 2);
	unsigned long epsilon = gamma ^ 0xFFF;
	if(*endp != '\0')
		err(1, "erreur de parsing du gammaat");

	printf("%ld\n", gamma * epsilon);
}

int main() {
	// read input data
	struct input_str input;
	input_str_read(&input, INPUT);

	// do stuff
	part1(&input);

	// cleanup & exit
	input_str_free(&input);
	return 0;
}