advent-of-code

My solutions to Advent of Code
git clone https://git.sr.ht/~jbauer/advent-of-code
Log | Files | Refs | README | LICENSE

main1.c (2123B)


      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 #include <string.h>
      4 
      5 #define ENTRY_LENGTH 12
      6 
      7 FILE *
      8 openInputFile(void)
      9 {
     10 	FILE *fp = fopen("input.txt","r");
     11 	if (fp == NULL)
     12 	{
     13 		printf("Could not open file: input.txt\n");
     14 		exit(EXIT_FAILURE);
     15 	}
     16 	return fp;
     17 }
     18 
     19 struct list {
     20 	size_t used;
     21 	size_t size;
     22 	int *contents;
     23 };
     24 
     25 int
     26 createList(struct list *list, size_t initialSize)
     27 {
     28 	list->contents = (int *) malloc(initialSize * sizeof(int));
     29 	if (list->contents == NULL)
     30 		return 1;
     31 	list->used = 0;
     32 	list->size = initialSize;
     33 	return 0;
     34 }
     35 
     36 int
     37 insertIntoList(struct list *list, int item)
     38 {
     39 	if (list->used == list->size)
     40 	{
     41 		list->size *= 2;
     42 		list->contents = (int *) realloc(list->contents, list->size * sizeof(int));
     43 		if (list->contents == NULL)
     44 			return 1;
     45 	}
     46 	list->contents[list->used++] = item;
     47 	return 0;
     48 }
     49 
     50 void
     51 freeList(struct list *list)
     52 {
     53 	free(list->contents);
     54 }
     55 
     56 // Just computes most common value from a set of 1 or 0 because nothing more is
     57 // needed for this challenge
     58 int
     59 mostCommonValue(struct list *list)
     60 {
     61 	int numZeros = 0;
     62 	int numOnes = 0;
     63 	for (int i = 0; i < list->used; i++)
     64 	{
     65 		if (list->contents[i] == 0)
     66 			numZeros++;
     67 		else
     68 			numOnes++;
     69 	}
     70 	if (numZeros > numOnes)
     71 		return 0;
     72 	else
     73 		return 1;
     74 }
     75 
     76 int
     77 main(void)
     78 {
     79 	FILE *fp = openInputFile();
     80 
     81 	struct list bits[ENTRY_LENGTH];
     82 	for (int i = 0; i < ENTRY_LENGTH; i ++)
     83 		if (createList(&bits[i], 1) != 0)
     84 			exit(EXIT_FAILURE);
     85 
     86 	char *line = NULL;
     87 	size_t len = 0;
     88 	ssize_t read;
     89 	while ((read = getline(&line, &len, fp)) != -1)
     90 	{
     91 		for (int i = 0; i < ENTRY_LENGTH; i ++)
     92 			insertIntoList(&bits[i], line[i] - '0');
     93 	}
     94 
     95 	int gamma = 0;
     96 
     97 	for (int i = 0; i < ENTRY_LENGTH; i++)
     98 	{
     99 		printf("%d: %d\n", i, mostCommonValue(&bits[i]));
    100 		if (mostCommonValue(&bits[i]))
    101 			gamma = gamma | (1 << (ENTRY_LENGTH - i - 1)) ;
    102 		printf("Gamma is: %x\n", gamma);
    103 	}
    104 
    105 	int epsilon = ~gamma & ((1 << ENTRY_LENGTH) - 1);
    106 
    107 	printf("  Gamma %d\n", gamma);
    108 	printf("Epsilon %d\n", epsilon);
    109 	printf("Product %d\n", gamma * epsilon);
    110 
    111 	fclose(fp);
    112 	if (line)
    113 		free(line);
    114 	for (int i = 0; i < ENTRY_LENGTH; i++)
    115 		freeList(&bits[i]);
    116 	exit(EXIT_SUCCESS);
    117 }