QUOTE: Never too old to chase dreams.

aoc

Advent of Code challenges

aoc_2017_2_1.c (658B)


      1#include <stdio.h>
      2#include <stdlib.h>
      3#include <string.h>
      4#include <ctype.h>
      5#include <limits.h>
      6
      7long perform(char *data) {
      8  (void) data;
      9  int len = strlen(data);
     10  int high = 0;
     11  int low = INT_MAX;
     12  int sum = 0;
     13  int i = 0;
     14  while (i < len) {
     15    char c = data[i];
     16    if (isdigit(c)) {
     17      char *end = NULL;
     18      int num = strtol(&data[i], &end, 10);
     19      if (num < low) {
     20        low = num;
     21      }
     22      if (num > high) {
     23        high = num;
     24      }
     25      i += end - (data + i);
     26      continue;
     27    }
     28    if (c == '\n') {
     29      int diff = high - low;
     30      high = 0;
     31      low = INT_MAX;
     32      sum += diff;
     33    }
     34    i++;
     35  }
     36  return sum;
     37}