~afk/advent-of-code-2020

7e6efdf14441b8e0e4e8da1982dc33dcd07dd82a — Mario Carballo Zama 2 years ago 1e8ef55 main
06.02: Add solution
4 files changed, 129 insertions(+), 0 deletions(-)

A 06.02/main.c
M bin/06.01
A bin/06.02
A input/06-test.txt
A 06.02/main.c => 06.02/main.c +114 -0
@@ 0,0 1,114 @@
#include <stdio.h>
#define STR_MAX_SIZE 256

int
cpos(char *s, char c)
{
	int i = 0;
	while(s[i] != '\0' && s[i])
		if(s[i++] == c)
			return i - 1;
	return -1;
}

int
slen(char *s)
{
	int n = 0;
	while(s[n] != '\0' && s[++n])
		;
	return n;
}

int
valid(char *s, int size)
{
	int i = 0;
	int j = 0;
	int acc = 0;
	int c = 0;
	int len = slen(s);

	for(i = 0; i < len; ++i) {
		c = 0;
		for(j = i; j < len; ++j) {
			if(s[i] == s[j]) {
				c++;
			}
		}
		if(c == size) {
			acc++;
		}
	}

	return acc;
}

int
loadtxt(FILE *f)
{
	int acc = 0;
	int i = 0;
	int answers = 0;
	int person = 0;

	char line[BUFSIZ];
	char query[BUFSIZ];
	char c = 0;

	if(!f) {
		printf("No file \n");
		return acc;
	}

	while(fgets(line, BUFSIZ, f)) {
		if(line[0] == '\n') {
			answers = valid(query, person);
			printf("[");
			if(answers < 10) {
				printf("0");
			}
			printf("%d] %s\n", answers, query);
			acc += answers;
			answers = 0;
			query[0] = '\0';
			person = 0;
		} else {
			c = line[0];
			i = 0;

			while((c = line[i++]) && c != '\n') {
				query[answers] = c;
				answers++;
				query[answers] = '\0';
			}
			person++;
		}
	}

	/* Add the last query becase we when the file ends we don't check the last line */
	answers = valid(query, person);
	printf("[%d] %s\n", answers, query);
	acc += answers;

	fclose(f);
	return acc;
}

int
main(int argc, char *argv[])
{
	int res = 0;
	if(argc < 1) {
		printf("No input file");
		return 0;
	}

	printf("-- Day 06.01 \n\n");

	res = loadtxt(fopen(argv[1], "r"));

	printf("--\nResult: %d\n--\n", res);

	return (0);
}

M bin/06.01 => bin/06.01 +0 -0
A bin/06.02 => bin/06.02 +0 -0
A input/06-test.txt => input/06-test.txt +15 -0
@@ 0,0 1,15 @@
abc

a
b
c

ab
ac

a
a
a
a

b