master
Jonathan Hodgson 3 years ago
parent f6a7e1dbfe
commit 96e5cf31fa
  1. 10
      day2/Makefile
  2. 6
      day2/example.txt
  3. 1000
      day2/realinput.txt
  4. 41
      day2/solution-part1.c
  5. 20
      day2/solution-part1.sh
  6. 43
      day2/solution-part2.c
  7. 26
      day2/solution-part2.sh

@ -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,6 @@
forward 5
down 5
forward 8
up 3
down 8
forward 2

File diff suppressed because it is too large Load Diff

@ -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;
}
}

@ -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 ))"

@ -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;
}
}

@ -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…
Cancel
Save