aoc-2020/jour8/main.c
2021-06-07 20:01:33 +02:00

76 lines
1.4 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int check_old_pc(int pc, int list_pc[])
{
int ret = 0, i = 0;
while (!ret && i <= 1024 && list_pc[i] >= 0)
{
if (list_pc[i] == pc)
ret = 1;
i++;
}
return ret;
}
void init_list_pc(int list_pc[])
{
for (int i = 0; i < 1024; i++)
list_pc[i] = -1;
}
void prem_partie(FILE *ptr)
{
int i = 0, a = 0, pc = 0, nbpc = 0;
int list_pc[1024];
char *buffer = NULL, *ope;
char *all_ope[1024];
size_t size_buffer = 0;
for (int j = 0; j < 1024; j++)
all_ope[j] = (char *) malloc(sizeof(char) * 1024);
init_list_pc(list_pc);
while (!feof(ptr))
{
getline(&buffer, &size_buffer, ptr);
if (*(buffer + strlen(buffer) - 1) == '\n')
*(buffer + strlen(buffer) - 1) = '\0';
strcpy(all_ope[i], buffer);
i++;
}
free(buffer);
while (!check_old_pc(pc, list_pc))
{
list_pc[nbpc] = pc;
nbpc++;
buffer = (char *) calloc(1024, sizeof(char));
strcpy(buffer, all_ope[pc]);
const char *espace = " ";
ope = strtok(buffer, espace);
if (!strcmp(ope, "acc"))
{
ope = strtok(NULL, espace);
a += atoi(ope);
pc++;
}
else if (!strcmp(ope, "jmp"))
{
ope = strtok(NULL, espace);
pc += atoi(ope);
}
else
pc++;
free(buffer);
}
for (int j = 0; j < 1024; j++)
free(all_ope[j]);
printf("Laccumulateur contient la valeur %d\n", a);
}
void deux_partie(FILE *ptr) {}