Day 1
This commit is contained in:
parent
f61a4de3ea
commit
f6a7e1dbfe
7 changed files with 2143 additions and 0 deletions
10
day1/Makefile
Normal file
10
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)
|
10
day1/example.txt
Normal file
10
day1/example.txt
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
199
|
||||||
|
200
|
||||||
|
208
|
||||||
|
210
|
||||||
|
200
|
||||||
|
207
|
||||||
|
240
|
||||||
|
269
|
||||||
|
260
|
||||||
|
263
|
2000
day1/realinput.txt
Normal file
2000
day1/realinput.txt
Normal file
File diff suppressed because it is too large
Load diff
30
day1/solution-part1.c
Normal file
30
day1/solution-part1.c
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
int main( int argc, char *argv[] ){
|
||||||
|
if( argc == 2 ) {
|
||||||
|
|
||||||
|
unsigned int last = ~0;
|
||||||
|
unsigned int increasing = 0;
|
||||||
|
unsigned int value;
|
||||||
|
|
||||||
|
// Read the values into an array
|
||||||
|
FILE *fp=fopen(argv[1], "r");
|
||||||
|
while (!feof (fp)){
|
||||||
|
fscanf(fp, "%d\n", &value);
|
||||||
|
|
||||||
|
printf( "value %d\n", value );
|
||||||
|
if ( value > last )
|
||||||
|
increasing++;
|
||||||
|
last = value;
|
||||||
|
}
|
||||||
|
fclose(fp);
|
||||||
|
|
||||||
|
|
||||||
|
printf( "%d were increasing\n", increasing );
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
} else {
|
||||||
|
printf("You need to provide a file\n");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
17
day1/solution-part1.sh
Executable file
17
day1/solution-part1.sh
Executable file
|
@ -0,0 +1,17 @@
|
||||||
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
|
# Give file as option 1
|
||||||
|
|
||||||
|
file="$1"
|
||||||
|
|
||||||
|
increasing=0
|
||||||
|
last=99999999
|
||||||
|
|
||||||
|
while read line; do
|
||||||
|
if [ "$line" -gt "$last" ]; then
|
||||||
|
increasing="$(( increasing + 1 ))"
|
||||||
|
fi
|
||||||
|
last="$line"
|
||||||
|
done < <(cat "$file")
|
||||||
|
|
||||||
|
echo "$increasing were increasing"
|
54
day1/solution-part2.c
Normal file
54
day1/solution-part2.c
Normal file
|
@ -0,0 +1,54 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
int countLines(char filename[]){
|
||||||
|
FILE *fp;
|
||||||
|
fp=fopen(filename, "r");
|
||||||
|
long int lines =0;
|
||||||
|
|
||||||
|
if ( fp == NULL ) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (EOF != (fscanf(fp, "%*[^\n]"), fscanf(fp,"%*c")))
|
||||||
|
++lines;
|
||||||
|
|
||||||
|
fclose(fp); // closing file
|
||||||
|
return lines;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main( int argc, char *argv[] ){
|
||||||
|
if( argc == 2 ) {
|
||||||
|
// 1 less because file ends with trailing new line
|
||||||
|
int lines=countLines(argv[1]);
|
||||||
|
int values[lines];
|
||||||
|
int line=0;
|
||||||
|
FILE *fp=fopen(argv[1], "r");
|
||||||
|
int increasing = 0;
|
||||||
|
|
||||||
|
// Read the values into an array
|
||||||
|
while (!feof (fp)){
|
||||||
|
fscanf(fp, "%d\n", &values[line]);
|
||||||
|
|
||||||
|
|
||||||
|
if ( (line >= 3) && (values[line] > values[line-3]) ){
|
||||||
|
|
||||||
|
increasing++;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
line++;
|
||||||
|
}
|
||||||
|
fclose(fp); // closing file
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
printf( "Increasing: %d\n", increasing );
|
||||||
|
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
} else {
|
||||||
|
printf("You need to provide a file\n");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
22
day1/solution-part2.sh
Executable file
22
day1/solution-part2.sh
Executable file
|
@ -0,0 +1,22 @@
|
||||||
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
|
# Give file as option 1
|
||||||
|
|
||||||
|
file="$1"
|
||||||
|
increasing=0
|
||||||
|
|
||||||
|
readarray -t "arr" < "$file"
|
||||||
|
|
||||||
|
length="${#arr[@]}"
|
||||||
|
|
||||||
|
for i in $(seq 3 "$length"); do
|
||||||
|
curr="${arr[$i]}"
|
||||||
|
prev3="${arr[$((i - 3))]}"
|
||||||
|
|
||||||
|
if [ -n "$curr" ] && [ "$curr" -gt "$prev3" ]; then
|
||||||
|
increasing="$((increasing + 1))"
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
|
||||||
|
echo "$increasing were increasing"
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue