Browse Source

day 2 done

stable
Penguin 8 months ago
parent
commit
7195f89f72
  1. 9
      day2/c/Makefile
  2. BIN
      day2/c/aoc_day2
  3. 113
      day2/c/aoc_day2.c
  4. BIN
      day2/c/aoc_day2.o
  5. 1000
      day2/input.txt

9
day2/c/Makefile

@ -0,0 +1,9 @@
CC=gcc
CFLAGS=-std=gnu99 -O2 -g
PROJECT=aoc_day2
%.o: %.c
$(CC) -c -o $@ $< $(CFLAGS)
$(PROJECT): aoc_day2.o
$(CC) -o aoc_day2 aoc_day2.o

BIN
day2/c/aoc_day2

Binary file not shown.

113
day2/c/aoc_day2.c

@ -0,0 +1,113 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define INPUT_FILE ("../input.txt")
#define ARR_LEN (1000)
typedef enum direction
{
DIR_FORWARD = 0,
DIR_UP = 1,
DIR_DOWN = 2
}direction;
// this is just direction + amount
typedef struct action_t
{
direction dir;
int amount;
}action_t;
int main(int argc, char** argv)
{
int horizontal_pos = 0;
int depth = 0;
int aim = 0;
FILE* fp = fopen(INPUT_FILE, "r");
if(fp == NULL)
{
printf("Failed to open file for reading...\n");
exit(-1);
}
size_t len = 0;
ssize_t rc = 0;
char* line = NULL;
char action[12];
action_t action_bank[ARR_LEN];
memset(action_bank, 0, sizeof(action_t) * ARR_LEN);
int ct = 0;
while((rc = getline(&line, &len, fp)) > 0)
{
memset(action, '\0', 12);
int amount = 0;
sscanf(line, "%s %d", action, &amount);
action_bank[ct].amount = amount;
if(strcmp(action, "forward") == 0)
{
action_bank[ct].dir = DIR_FORWARD;
horizontal_pos += amount;
}
else if(strcmp(action, "up") == 0)
{
action_bank[ct].dir = DIR_UP;
depth -= amount;
}
else if(strcmp(action, "down") == 0)
{
action_bank[ct].dir = DIR_DOWN;
depth += amount;
}
else
{
printf("action didn't match any known actions -- action: %s\n", action);
exit(-1);
}
ct++;
}
printf("Part 1\n");
printf("Horizontal Position: %d\n", horizontal_pos);
printf("Depth: %d\n", depth);
printf("calculated course: %d\n", depth * horizontal_pos);
// part 2
horizontal_pos = 0;
depth = 0;
for(int ind = 0; ind < ARR_LEN; ind++)
{
/* printf(">>>ACTION: %s\tAMOUNT: %d\n", */
/* action_bank[ind].dir == DIR_FORWARD ? "FORWARD" : */
/* action_bank[ind].dir == DIR_UP ? "UP" : */
/* action_bank[ind].dir == DIR_DOWN ? "DOWN" : "UNKNOWN", */
/* action_bank[ind].amount); */
if(action_bank[ind].dir == DIR_FORWARD)
{
horizontal_pos += action_bank[ind].amount;
depth += (aim * action_bank[ind].amount);
}
else if(action_bank[ind].dir == DIR_UP)
{
aim -= action_bank[ind].amount;
}
else if(action_bank[ind].dir == DIR_DOWN)
{
aim += action_bank[ind].amount;
}
else
{
printf("Error, dir unknown\n");
exit(-1);
}
}
printf("Part 2\n");
printf("Horizontal Position: %d\n"
"Depth: %d\n"
"Aim: %d\n", horizontal_pos, depth, aim);
printf("Final Position: %d\n", horizontal_pos * depth);
return 0;
}

BIN
day2/c/aoc_day2.o

Binary file not shown.

1000
day2/input.txt

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