Ajout jour 4

This commit is contained in:
Rick 2020-12-12 15:59:48 +01:00
parent 99543a09c4
commit 7881f8b97a
Signed by: Rick
GPG Key ID: 9570A7DB7CB2F436
5 changed files with 1348 additions and 0 deletions

18
jour4/Makefile Normal file
View File

@ -0,0 +1,18 @@
CC=gcc
CFLAGS= -Wall -g
all: clean main clean
main: main.o utils.o
$(CC) -o $@ $^
main.o: main.c
$(CC) $(CFLAGS) $^ -o $@ -c
utils.o: utils.c
$(CC) $(CFLAGS) $^ -o $@ -c
.PHONY: all clean
clean:
rm -f *.o

1159
jour4/input Normal file

File diff suppressed because it is too large Load Diff

139
jour4/main.c Normal file
View File

@ -0,0 +1,139 @@
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include "utils.h"
#define FILENAME "input"
void premPartie(FILE *ptr);
void deuxPartie(FILE *ptr);
int main()
{
FILE *ptr = fopen(FILENAME, "r");
if (ptr == NULL)
return 1;
//premPartie(ptr);
deuxPartie(ptr);
fclose(ptr);
return 0;
}
void deuxPartie(FILE *ptr)
{
int find = 0, tmp;
int nbData = 0; // est incrémenté si arg bon
char checkLigne[2]; // vérifie 2 lignes plus loin pour détecter ligne vide
char value[10];
char key[3];
char *tmpChar = NULL;
fscanf(ptr, "%s:%s", key, value);
while (!feof(ptr))
{
if (!strncmp(key, "byr", 3))
{
tmp = sub_digit(value);
if (tmp >= 1920 && tmp <= 2002)
nbData++;
}
else if (!strncmp(key, "iyr", 3))
{
tmp = sub_digit(value);
if (tmp >= 2010 && tmp <= 2020)
nbData++;
}
else if (!strncmp(key, "eyr", 3))
{
tmp = sub_digit(value);
if (tmp >= 2020 && tmp <= 2030)
nbData++;
}
else if (!strncmp(key, "hgt", 3))
{
int checkH = sub_digit(value);
if (strstr(value, "cm") != NULL && checkH >= 150 && checkH <= 193)
nbData++;
else if (strstr(value, "in") != NULL && checkH >= 59 && checkH <= 76)
nbData++;
}
else if (!strncmp(key, "ecl", 3))
{
if (check_ecl(value))
nbData++;
}
else if (!strncmp(key, "pid", 3))
{
tmpChar = strpbrk(value, "0123456789");
if (strspn(tmpChar, "0123456789") == 9)
nbData++;
}
else if (!strncmp(key, "hcl", 3))
{
//comme le 1er char est :, on doit tout décaler de 1
if (value[1] == '#' && strlen(value) == 8)
{
int i = 2;
while (isalnum(value[i]) && value[i] != '\0')
i++;
if (i == strlen(value))
nbData++;
}
}
checkLigne[0] = getc(ptr);
checkLigne[1] = getc(ptr);
if (checkLigne[0] == '\n' && (checkLigne[1] == '\n' || checkLigne[1] == EOF))
{
if (nbData == 7)
find++;
nbData = 0;
}
// reviens un caractère en arrière pour éviter de perdre des infos
// quand on check la présence dune ligne vide
fseek(ptr, -1, SEEK_CUR);
fscanf(ptr, "%s:%s", key, value);
}
printf("%d bons passeports trouvés.\n", find);
}
void premPartie(FILE *ptr)
{
int find = 0;
int nbData = 0, cid = 0;
char checkLigne[2]; // vérifie 2 lignes plus loin pour détecter ligne vide
char word[256];
fscanf(ptr, "%s", word);
while (!feof(ptr))
{
nbData++;
if (!strncmp(word, "cid", 3))
cid = 1;
checkLigne[0] = getc(ptr);
checkLigne[1] = getc(ptr);
if (checkLigne[0] == '\n' && (checkLigne[1] == '\n' || checkLigne[1] == EOF))
{
if ((nbData == 7 && !cid) || nbData == 8)
find++;
nbData = 0;
cid = 0;
}
// reviens un caractère en arrière pour éviter de perdre des infos
// quand on check la présence dune ligne vide
fseek(ptr, -1, SEEK_CUR);
fscanf(ptr, "%s", word);
}
printf("%d bons passeports trouvés.\n", find);
}

30
jour4/utils.c Normal file
View File

@ -0,0 +1,30 @@
#include <stdlib.h>
#include <string.h>
#include "utils.h"
int sub_digit(char *str)
{
int ret = 0;
char *beginning = strpbrk(str, "0123456789");
int len = strspn(beginning, "0123456789");
char *tmp = (char *) malloc(sizeof(char) * len);
strncpy(tmp, beginning, len);
ret = atoi(tmp);
free(tmp);
return ret;
}
int check_ecl(char *str)
{
str = str + 1;
if (!strcmp(str, "amb") || !strcmp(str, "blu") || !strcmp(str, "brn")
|| !strcmp(str, "gry") || !strcmp(str, "grn") || !strcmp(str, "hzl")
|| !strcmp(str, "oth"))
return 1;
return 0;
}

2
jour4/utils.h Normal file
View File

@ -0,0 +1,2 @@
int sub_digit(char *str);
int check_ecl(char *str);