~jbauer/advent-of-code

ref: b1019419e368d387c5182c277c4e8710b035e446 advent-of-code/main1.c -rw-r--r-- 696 bytes
b1019419Jake Bauer Complete Day 1 challenges 7 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <stdio.h>
#include <stdlib.h>

FILE *
openInputFile(void)
{
	FILE *fp = fopen("input.txt","r");
	if (fp == NULL)
	{
		printf("Could not open file: input.txt\n");
		exit(EXIT_FAILURE);
	}
	return fp;
}

int
main(void)
{
	char *line = NULL;
	int increases = 0;
	int currentValue = 0;
	int previousValue = -1;
	size_t len = 0;
	ssize_t read;

	FILE *fp = openInputFile();

	while ((read = getline(&line, &len, fp)) != -1)
	{
		if (previousValue < 0)
			previousValue = atoi(line);

		currentValue = atoi(line);

		if (currentValue > previousValue)
			increases++;

		previousValue = currentValue;
	}

	fclose(fp);
	if (line)
		free(line);
	printf("%d\n", increases);
	exit(EXIT_SUCCESS);
}