master
Jonathan Hodgson 2 years ago
parent d8448356ef
commit f45f295764
  1. 10
      day6/Makefile
  2. 1
      day6/example.txt
  3. 1
      day6/realinput.txt
  4. 103
      day6/solution-part1.c
  5. 60
      day6/solution-part2.c

@ -0,0 +1,10 @@
solution-part%: solution-part%.c
gcc $< -o $@
test-%: solution-% example.txt
$< example.txt
run-%: solution-% realinput.txt
$< realinput.txt
.PHONY: $(patsubst %,run-%, 1 2) $(patsubst %,test-%, 1 2)

@ -0,0 +1 @@
3,4,3,1,2

@ -0,0 +1 @@
4,1,1,4,1,1,1,1,1,1,1,1,3,4,1,1,1,3,1,3,1,1,1,1,1,1,1,1,1,3,1,3,1,1,1,5,1,2,1,1,5,3,4,2,1,1,4,1,1,5,1,1,5,5,1,1,5,2,1,4,1,2,1,4,5,4,1,1,1,1,3,1,1,1,4,3,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,5,1,1,2,1,1,1,1,1,1,1,2,4,4,1,1,3,1,3,2,4,3,1,1,1,1,1,2,1,1,1,1,2,5,1,1,1,1,2,1,1,1,1,1,1,1,2,1,1,4,1,5,1,3,1,1,1,1,1,5,1,1,1,3,1,2,1,2,1,3,4,5,1,1,1,1,1,1,5,1,1,1,1,1,1,1,1,3,1,1,3,1,1,4,1,1,1,1,1,2,1,1,1,1,3,2,1,1,1,4,2,1,1,1,4,1,1,2,3,1,4,1,5,1,1,1,2,1,5,3,3,3,1,5,3,1,1,1,1,1,1,1,1,4,5,3,1,1,5,1,1,1,4,1,1,5,1,2,3,4,2,1,5,2,1,2,5,1,1,1,1,4,1,2,1,1,1,2,5,1,1,5,1,1,1,3,2,4,1,3,1,1,2,1,5,1,3,4,4,2,2,1,1,1,1,5,1,5,2

@ -0,0 +1,103 @@
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
typedef struct LanternFishListItem LanternFishListItem;
struct LanternFishListItem {
unsigned int daysToSpawn;
LanternFishListItem *next;
};
void printFrom( LanternFishListItem *first ){
printf("%i,", first->daysToSpawn);
if ( first->next != NULL ) printFrom(first->next);
}
unsigned int countFrom( LanternFishListItem *current ){
unsigned int count = 1;
while ( current->next != NULL ){
count++;
current = current->next;
}
return count;
}
int main( int argc, char *argv[] ){
if( argc == 2 ) {
FILE *fp=fopen(argv[1], "r");
LanternFishListItem *first = NULL;
LanternFishListItem *previous = NULL;
unsigned int no;
char ch;
while ( fscanf( fp, "%i%c", &no, &ch ) ){
LanternFishListItem *lf = (LanternFishListItem*)malloc(sizeof(LanternFishListItem));
lf->next = NULL;
lf->daysToSpawn = no;
if ( first == NULL ){
first = lf;
} else {
previous->next = lf;
}
previous = lf;
if ( ch == '\n' ) break;
}
for ( unsigned int i = 0; i < 80; i++ ){
unsigned int toAdd=0;
LanternFishListItem *current = first;
while (current != NULL){
if ( current->daysToSpawn == 0 ){
toAdd++;
current->daysToSpawn = 7;
}
current->daysToSpawn--;
if ( current->next != NULL )
current = current->next;
else
break;
}
previous = current;
//printf("On day %i add %i fish\n", i, toAdd);
for (unsigned int j = 0; j < toAdd; j++){
LanternFishListItem *lf = (LanternFishListItem*)malloc(sizeof(LanternFishListItem));
lf->next = NULL;
lf->daysToSpawn = 8;
previous->next = lf;
previous = lf;
}
//printf("After %3i day: ", i+1);
//printFrom(first);
//printf("\n");
}
printf("There are %i Fish.\n",countFrom(first));
fclose(fp);
return 0;
} else {
printf("You need to provide a file\n");
return 1;
}
}

@ -0,0 +1,60 @@
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
#define POSSIBLE_SPAWN_AGES 9
unsigned long countFish(unsigned long *fishCount){
unsigned long count = 0;
for( unsigned int i = 0; i < POSSIBLE_SPAWN_AGES; i++ ){
count += fishCount[i];
}
return count;
}
int main( int argc, char *argv[] ){
if( argc == 2 ) {
FILE *fp=fopen(argv[1], "r");
unsigned int no;
char ch;
// I realised that the ordering of the fish doesn't matter. What matters is how many of them are of each age.
// This array will contain the number of fish of age 1 in fishCount[1] and so on.
unsigned long fishCount[POSSIBLE_SPAWN_AGES] = {0};
while ( fscanf( fp, "%i%c", &no, &ch ) ){
fishCount[no]++;
if ( ch == '\n' ) break;
}
for ( unsigned int i = 0; i < 256; i++ ){
// "backup" the number of fish of age 0 as the memmove will loose this
unsigned long age0 = fishCount[0];
memmove( &fishCount[0], &fishCount[1], sizeof(unsigned long) * (POSSIBLE_SPAWN_AGES - 1) );
// All the fish that had a spawn_age of 0 will now have a spawn age of 6
fishCount[6] += age0;
// They will also all have a new fish with age of 8
fishCount[8] = age0;
//printf("\nDay %i:\n", i+1);
//for( unsigned int j = 0; j < POSSIBLE_SPAWN_AGES; j++){
// printf("Fish with spawn age %i: %lu\n", j, fishCount[j]);
//}
}
printf("There are %lu Fish.\n",countFish(fishCount));
fclose(fp);
return 0;
} else {
printf("You need to provide a file\n");
return 1;
}
}
Loading…
Cancel
Save