Day 1
This commit is contained in:
parent
dff4b9cc14
commit
6be1f14410
6 changed files with 4630 additions and 0 deletions
10
2022/day1/Makefile
Normal file
10
2022/day1/Makefile
Normal file
|
@ -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)
|
14
2022/day1/example.txt
Normal file
14
2022/day1/example.txt
Normal file
|
@ -0,0 +1,14 @@
|
|||
1000
|
||||
2000
|
||||
3000
|
||||
|
||||
4000
|
||||
|
||||
5000
|
||||
6000
|
||||
|
||||
7000
|
||||
8000
|
||||
9000
|
||||
|
||||
10000
|
2248
2022/day1/realinput.txt
Normal file
2248
2022/day1/realinput.txt
Normal file
File diff suppressed because it is too large
Load diff
36
2022/day1/solution-part1.c
Normal file
36
2022/day1/solution-part1.c
Normal file
|
@ -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;
|
||||
}
|
||||
}
|
67
2022/day1/solution-part2.c
Normal file
67
2022/day1/solution-part2.c
Normal file
|
@ -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;
|
||||
}
|
||||
}
|
2255
2022/day1/solution.bash
Executable file
2255
2022/day1/solution.bash
Executable file
File diff suppressed because it is too large
Load diff
Loading…
Add table
Add a link
Reference in a new issue