Browse Source

day 1 finished

stable
Penguin 8 months ago
parent
commit
5a9edbcad0
  1. 9
      day1/c/Makefile
  2. BIN
      day1/c/aoc_day1
  3. 64
      day1/c/aoc_day1.c
  4. BIN
      day1/c/aoc_day1.o
  5. 2000
      day1/c/input.txt

9
day1/c/Makefile

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

BIN
day1/c/aoc_day1

Binary file not shown.

64
day1/c/aoc_day1.c

@ -0,0 +1,64 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#define INPUT_FILE ("input.txt")
#define ARR_LEN (2000)
int main(int argc, char** argv)
{
int measurements[ARR_LEN];
memset(measurements, 0, ARR_LEN * sizeof(int));
FILE* fp = fopen(INPUT_FILE, "r");
if(fp == NULL)
{
printf("File failed to open for reading...");
exit(-1);
}
char* line = NULL;
size_t len = 0;
ssize_t rc = 0;
uint16_t ct = 0;
// counter that keeps track of how many times a depth measurement has increased
int increase_count = 0;
if((rc = getline(&line, &len, fp)) < 0)
{
printf("Bad read...\n");
exit(-1);
}
else
{
measurements[ct++] = strtol(line, NULL, 10);
}
while((rc = getline(&line, &len, fp)) >= 0)
{
measurements[ct] = strtol(line, NULL, 10);
if(measurements[ct] - measurements[ct - 1] > 0)
{
increase_count++;
}
ct++;
}
printf("Increase count: %d\n", increase_count);
// part 2
int sum_increase_count = 0;
for(int ind = 2; ind < ARR_LEN - 1; ind++)
{
int sum_one = measurements[ind - 2] +
measurements[ind - 1] +
measurements[ind];
int sum_two = measurements[ind - 1] +
measurements[ind] +
measurements[ind + 1];
sum_increase_count += (sum_two > sum_one);
}
printf("Sum increase count: %d\n", sum_increase_count);
return 0;
}

BIN
day1/c/aoc_day1.o

Binary file not shown.

2000
day1/c/input.txt

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