Day 2
This commit is contained in:
parent
f6a7e1dbfe
commit
96e5cf31fa
7 changed files with 1146 additions and 0 deletions
10
day2/Makefile
Normal file
10
day2/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)
|
6
day2/example.txt
Normal file
6
day2/example.txt
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
forward 5
|
||||||
|
down 5
|
||||||
|
forward 8
|
||||||
|
up 3
|
||||||
|
down 8
|
||||||
|
forward 2
|
1000
day2/realinput.txt
Normal file
1000
day2/realinput.txt
Normal file
File diff suppressed because it is too large
Load diff
41
day2/solution-part1.c
Normal file
41
day2/solution-part1.c
Normal file
|
@ -0,0 +1,41 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
int main( int argc, char *argv[] ){
|
||||||
|
if( argc == 2 ) {
|
||||||
|
|
||||||
|
unsigned int horizontal = 0;
|
||||||
|
unsigned int depth = 0;
|
||||||
|
|
||||||
|
FILE *fp=fopen(argv[1], "r");
|
||||||
|
while (!feof (fp)){
|
||||||
|
char direction[7] = "";
|
||||||
|
int amount = 0;
|
||||||
|
fscanf(fp, "%s %d\n", direction, &amount);
|
||||||
|
|
||||||
|
|
||||||
|
if ( strcmp(direction,"forward") == 0 )
|
||||||
|
horizontal+=amount;
|
||||||
|
else if ( strcmp(direction,"down") == 0 )
|
||||||
|
depth+=amount;
|
||||||
|
else if ( strcmp(direction,"up") == 0 )
|
||||||
|
depth-=amount;
|
||||||
|
else
|
||||||
|
printf("Hopefully we don't get here\n");
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("Horizontal: %d\n", horizontal);
|
||||||
|
printf("Depth: %d\n", depth);
|
||||||
|
printf("Product: %d\n", horizontal * depth);
|
||||||
|
fclose(fp);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
} else {
|
||||||
|
printf("You need to provide a file\n");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
20
day2/solution-part1.sh
Executable file
20
day2/solution-part1.sh
Executable file
|
@ -0,0 +1,20 @@
|
||||||
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
|
file="$1"
|
||||||
|
|
||||||
|
horizontal=0
|
||||||
|
depth=0
|
||||||
|
|
||||||
|
while read line; do
|
||||||
|
direction="$(echo "$line" | cut -d ' ' -f 1)"
|
||||||
|
amount="$(echo "$line" | cut -d ' ' -f 2)"
|
||||||
|
case "$direction" in
|
||||||
|
forward) horizontal=$((horizontal + amount)) ;;
|
||||||
|
down) depth="$((depth + amount))" ;;
|
||||||
|
up) depth="$((depth - amount))" ;;
|
||||||
|
esac
|
||||||
|
done < <(cat "$file")
|
||||||
|
|
||||||
|
echo "Horizontal: $horizontal"
|
||||||
|
echo "Depth: $depth"
|
||||||
|
echo "Product: $(( depth * horizontal ))"
|
43
day2/solution-part2.c
Normal file
43
day2/solution-part2.c
Normal file
|
@ -0,0 +1,43 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
int main( int argc, char *argv[] ){
|
||||||
|
if( argc == 2 ) {
|
||||||
|
|
||||||
|
unsigned int horizontal = 0;
|
||||||
|
unsigned int depth = 0;
|
||||||
|
unsigned int aim = 0;
|
||||||
|
|
||||||
|
FILE *fp=fopen(argv[1], "r");
|
||||||
|
while (!feof (fp)){
|
||||||
|
char direction[7] = "";
|
||||||
|
int amount = 0;
|
||||||
|
fscanf(fp, "%s %d\n", direction, &amount);
|
||||||
|
|
||||||
|
|
||||||
|
if ( strcmp(direction,"forward") == 0 ){
|
||||||
|
horizontal+=amount;
|
||||||
|
depth+=(aim*amount);
|
||||||
|
} else if ( strcmp(direction,"down") == 0 )
|
||||||
|
aim+=amount;
|
||||||
|
else if ( strcmp(direction,"up") == 0 )
|
||||||
|
aim-=amount;
|
||||||
|
else
|
||||||
|
printf("Hopefully we don't get here\n");
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("Horizontal: %d\n", horizontal);
|
||||||
|
printf("Depth: %d\n", depth);
|
||||||
|
printf("Product: %d\n", horizontal * depth);
|
||||||
|
fclose(fp);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
} else {
|
||||||
|
printf("You need to provide a file\n");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
26
day2/solution-part2.sh
Executable file
26
day2/solution-part2.sh
Executable file
|
@ -0,0 +1,26 @@
|
||||||
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
|
file="$1"
|
||||||
|
|
||||||
|
horizontal=0
|
||||||
|
depth=0
|
||||||
|
aim=0
|
||||||
|
|
||||||
|
while read line; do
|
||||||
|
#direction="$(echo "$line" | cut -d ' ' -f 1)"
|
||||||
|
direction="${line%% *}"
|
||||||
|
#amount="$(echo "$line" | cut -d ' ' -f 2)"
|
||||||
|
amount="${line##* }"
|
||||||
|
case "$direction" in
|
||||||
|
forward)
|
||||||
|
horizontal=$((horizontal + amount))
|
||||||
|
depth="$((depth + (aim * amount) ))"
|
||||||
|
;;
|
||||||
|
down) aim="$((aim + amount))" ;;
|
||||||
|
up) aim="$((aim - amount))" ;;
|
||||||
|
esac
|
||||||
|
done < <(cat "$file")
|
||||||
|
|
||||||
|
echo "Horizontal: $horizontal"
|
||||||
|
echo "Depth: $depth"
|
||||||
|
echo "Product: $(( depth * horizontal ))"
|
Loading…
Add table
Add a link
Reference in a new issue