QUOTE: Be your own kind of beautiful.

aoc_2021_1_1.c - aoc - Advent of Code challenges

aoc

Advent of Code challenges
git clone git@soophie.de:/srv/git/aoc
log | files | refs

aoc_2021_1_1.c (620B)


      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 #include <string.h>
      4 
      5 long perform(char *data) {
      6   int len = strlen(data);
      7   int ln = 0;
      8   for (int i = 0; i < len; i++) {
      9     char c = data[i];
     10     if (c == '\n') {
     11       ln++;
     12     }
     13   }
     14   int nums[ln];
     15   int idx = 0;
     16   int set = 0;
     17   for (int i = 0; i < len; i++) {
     18     char c = data[i];
     19     if (c == '\n') {
     20       int num = strtol(data + set, NULL, 10);
     21       nums[idx++] = num;
     22       set = i + 1;
     23     }
     24   }
     25   int sum = 0;
     26   int *last = NULL;
     27   for (int i = 0; i < ln; i++) {
     28     if (last != NULL && *last < nums[i]) {
     29       sum++;
     30     }
     31     last = &nums[i];
     32   }
     33   return sum;
     34 }