summaryrefslogtreecommitdiffstats
path: root/2021/day1/depth.c
diff options
context:
space:
mode:
authorYigit Sever2021-12-01 13:53:26 +0300
committerYigit Sever2021-12-01 13:53:26 +0300
commitc83970e459635609f7b4520f4f73db8f70c973f8 (patch)
tree5232ccc1d0e015695e7ce8e694d33985f4c76b21 /2021/day1/depth.c
downloadaoc-c83970e459635609f7b4520f4f73db8f70c973f8.tar.gz
aoc-c83970e459635609f7b4520f4f73db8f70c973f8.tar.bz2
aoc-c83970e459635609f7b4520f4f73db8f70c973f8.zip
2021, day1: done
Diffstat (limited to '2021/day1/depth.c')
-rw-r--r--2021/day1/depth.c73
1 files changed, 73 insertions, 0 deletions
diff --git a/2021/day1/depth.c b/2021/day1/depth.c
new file mode 100644
index 0000000..eff19b4
--- /dev/null
+++ b/2021/day1/depth.c
@@ -0,0 +1,73 @@
1#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4#include <stdint.h>
5
6int main(int argc, char *argv[])
7{
8 if (argc != 2) {
9 printf("Usage: ./depth <input-file>\n");
10 exit(EXIT_FAILURE);
11 }
12
13 FILE *fp;
14 char *buffer;
15 ssize_t read;
16 size_t len = 0;
17
18 fp = fopen(argv[1], "r");
19 if (fp == NULL) {
20 perror("fopen");
21 exit(EXIT_FAILURE);
22 }
23
24 int inc = 0;
25 int curr = 0;
26 uint8_t idx = 0;
27 int side = 0;
28 int depth[3] = { 0, 0, 0 };
29
30 read = getline(&buffer, &len, fp);
31 if (read != -1) {
32 depth[0] = atoi(buffer);
33 }
34
35 read = getline(&buffer, &len, fp);
36 if (read != -1) {
37 curr = atoi(buffer);
38 depth[0] += curr;
39 depth[1] = curr;
40 }
41
42 read = getline(&buffer, &len, fp);
43 if (read != -1) {
44 curr = atoi(buffer);
45 depth[0] += curr;
46 depth[1] += curr;
47 depth[2] = curr;
48 }
49
50 while ((read = getline(&buffer, &len, fp)) != -1) {
51 side = depth[idx];
52 depth[idx] = 0;
53 idx = (idx + 1) % 3;
54
55 curr = atoi(buffer);
56 depth[0] += curr;
57 depth[1] += curr;
58 depth[2] += curr;
59
60 if (depth[idx] > side) {
61 inc++;
62 }
63 }
64
65 fclose(fp);
66 if (buffer) {
67 free(buffer);
68 }
69
70 printf("%d", inc);
71
72 return 0;
73}