My solutions to the advent of code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

163 lines
3.6 KiB

#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
#define ANSI_COLOR_RED "\x1b[31m"
#define ANSI_COLOR_YELLOW "\x1b[33m"
#define ANSI_COLOR_RESET "\x1b[0m"
#define MAX(a, b) ((a) < (b) ? (b) : (a))
#define MIN(a, b) ((a) < (b) ? (a) : (b))
typedef struct Element Element;
struct Element{
char letter;
Element *next;
};
typedef struct Interaction Interaction;
struct Interaction{
char letter1;
char letter2;
char result;
Interaction *next;
};
Element* newElement(char letter){
Element *new = malloc(sizeof(Element));
new->letter = letter;
new->next = NULL;
return new;
}
Interaction* newInteraction(char letter1, char letter2, char result){
Interaction *new = malloc(sizeof(Interaction));
new->letter1 = letter1;
new->letter2 = letter2;
new->result = result;
new->next = NULL;
return new;
}
void printElements(Element *current){
while ( current != NULL ){
printf("%c ",current->letter);
current = current->next;
}
printf("\n");
}
void printInteractions(Interaction *current){
while ( current != NULL ){
printf("Merging %c and %c will give you %c\n",current->letter1, current->letter2, current->result);
current = current->next;
}
printf("\n");
}
unsigned int countElements(Element *current){
unsigned int count = 0;
while ( current != NULL ){
count++;
current = current->next;
}
return count;
}
Element* interact(char firstCh, char secondCh, Interaction *interaction){
while ( interaction != NULL ){
if ( interaction->letter1 == firstCh && interaction->letter2 == secondCh )
return newElement( interaction->result );
interaction = interaction->next;
}
return NULL;
}
unsigned int calculateScore(Element *current){
unsigned int min = ~0;
unsigned int max = 0;
unsigned int counts[26] = {0};
while ( current != NULL ){
counts[current->letter - 'A']++;
current = current->next;
}
for(unsigned int i = 0; i < 26; i++){
max = MAX( max, counts[i] );
if ( counts[i] != 0 ) min = MIN( min, counts[i] );
printf("%c: %i\n", i+'A' , counts[i]);
}
printf("Max: %i, Min: %i, Difference: %i\n", max, min, max-min);
return 0;
}
void step(Element *element, Interaction *interaction){
while ( element != NULL && element->next != NULL ){
Element *toInsert = interact(element->letter, element->next->letter, interaction);
toInsert->next = element->next;
element->next = toInsert;
element = toInsert->next;
}
}
int main( int argc, char *argv[] ){
if( argc == 2 ) {
FILE *fp=fopen(argv[1], "r");
char ch;
Element *firstEl = NULL;
Element *previousEl = NULL;
while ( fscanf( fp, "%c",&ch )){
if ( ch == '\n' ) break;
printf("%c\n",ch);
if ( firstEl == NULL ){
firstEl = newElement(ch);
previousEl = firstEl;
} else {
previousEl->next = newElement(ch);
previousEl = previousEl->next;
}
}
//Skip over empty lines
while( !feof(fp) && fscanf(fp, "\n\n") ) continue;
char firstCh, secondCh, resultCh;
Interaction *firstInt = NULL;
Interaction *previousInt = NULL;
while( !feof(fp) && fscanf(fp, "%c%c -> %c\n", &firstCh, &secondCh, &resultCh) ){
if ( firstInt == NULL ){
firstInt = newInteraction(firstCh, secondCh, resultCh);
previousInt = firstInt;
} else {
previousInt->next = newInteraction(firstCh, secondCh, resultCh);
previousInt = previousInt->next;
}
}
printElements(firstEl);
for ( unsigned int i = 0; i < 10; i++ ){
printf("\nStep %i:\n", i+1);
step(firstEl, firstInt);
printf("Length %i\n", countElements(firstEl));
}
calculateScore(firstEl);
fclose(fp);
return 0;
} else {
printf("You need to provide a file\n");
return 1;
}
}