master
Jonathan Hodgson 2 years ago
parent dff4b9cc14
commit 6be1f14410
  1. 10
      2022/day1/Makefile
  2. 14
      2022/day1/example.txt
  3. 2248
      2022/day1/realinput.txt
  4. 36
      2022/day1/solution-part1.c
  5. 67
      2022/day1/solution-part2.c
  6. 2255
      2022/day1/solution.bash

@ -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,14 @@
1000
2000
3000
4000
5000
6000
7000
8000
9000
10000

File diff suppressed because it is too large Load Diff

@ -0,0 +1,36 @@
#include <stdio.h>
#include <string.h>
int main( int argc, char *argv[] ){
if( argc == 2 ) {
char buffer[10] = {'\0'};
FILE *fp=fopen(argv[1], "r");
unsigned int value = 0, total=0, largest=0;
while (!feof (fp)){
fgets(buffer, 10, fp);
//fscanf(fp, "%d\n", &value);
if (strlen(buffer) == 1){
if ( total > largest ) largest=total;
total=0;
} else {
sscanf(buffer,"%d\n",&value);
total+=value;
}
}
printf("The largest is %d\n", largest);
fclose(fp);
return 0;
} else {
printf("You need to provide a file\n");
return 1;
}
}

@ -0,0 +1,67 @@
#include <stdio.h>
#include <string.h>
void printLargest(int length, unsigned long list[length]){
unsigned int sum = 0;
printf("The largest %d are: ", length);
for (int i = 0; i < length; ++i) {
sum += list[i];
printf("%d", list[i] );
if ( i < length-1 ) printf(", ");
}
printf("\nThe total is: %d\n", sum);
}
void addToList(unsigned int length, unsigned long list[length], unsigned int contender){
for ( int i = 0; i < length; ++i ){
if ( contender > list[i] ){
for (int j = length-1; j>i; --j){
list[j] = list[j-1];
}
list[i] = contender;
break;
}
}
}
int main( int argc, char *argv[] ){
if( argc == 2 ) {
char buffer[10] = {'\0'};
FILE *fp=fopen(argv[1], "r");
unsigned int value = 0, total=0;
unsigned long largest[3]={0,0,0};
while (!feof (fp)){
buffer[0]='\0';
value=0;
fgets(buffer, 10, fp);
printf( " - %s\n", buffer );
if (strlen(buffer) == 1){
printf("Total %d\n", total);
addToList(3,largest,total);
total=0;
} else {
sscanf(buffer,"%d\n",&value);
total+=value;
}
}
printf("Total %d\n", total);
addToList(3,largest,total);
printLargest(3,largest);
//printf("The largest is %d\n", *largest);
fclose(fp);
return 0;
} else {
printf("You need to provide a file\n");
return 1;
}
}

File diff suppressed because it is too large Load Diff
Loading…
Cancel
Save