2016-12-03 Advent of Code in C

Since the previous two days of Advent of Code went so well, I decided to up the ante and try different programming languages. Today: C. I haven’t written C since abandoning German Atlantis.

Advent of Code

German Atlantis

The question is this: given three numbers per line, count the potential triangles. “In a valid triangle, the sum of any two sides must be larger than the remaining side.”

Here goes. Save as `day3.c`, compile using `make day3`, test from the shell using `echo 1 2 3 | ./day3` (not a triangle), save your input in a file called `data` and run using `./day3 < data`.

#include <stdio.h>
#include <stdlib.h>

int read_num () {
  int k;
  if (scanf("%d", &k) == 1) {
    return k;
  }
  return 0;
}

int * read_triangle() {
  static int t[3];
  for (int i = 0; i < 3; i++) {
    int n = read_num();
    if (n == 0) {
      if (i == 0) {
	return 0;
      }
      fprintf(stderr, "triangle incomplete, missing %d sides.\n", 3 - i);
      abort();
    }
    t[i] = n;
  }
  return t;
}

int legal(int *t) {
  if (t[0] + t[1] > t[2]
      && t[0] + t[2] > t[1]
      && t[1] + t[2] > t[0]) {
    return 1;
  }
  return 0;
}

int main() {
  int n = 0;
  int *t;
  while ((t = read_triangle()) != 0) {
    /* if (legal(t) == 1) { */
    /*   printf("triangle: [%d %d %d]\n", t[0], t[1], t[2]); */
    /* } else { */
    /*   printf("not a triangle: [%d %d %d]\n", t[0], t[1], t[2]); */
    /* } */
    n += legal(t);
  }
  printf("Counted %d triangles.\n", n);
  return 0;
}

The setup I had was particularly fortuitous for the second part because the array could just as well hold three triangles instead of just one, and the legal function could return a count instead of just 1 or 0.

In part two, we count vertical triangle candidates. In other words, we read three rows and get three potential triangles. Here’s the example given:

101 301 501
102 302 502
103 303 503
201 401 601
202 402 602
203 403 603

This counts as 6 triangles, because every [101 102 103] is a triangle, [201 202 203] is a triangle, etc.

#include <stdio.h>
#include <stdlib.h>

int read_num () {
  int k;
  if (scanf("%d", &k) == 1) {
    return k;
  }
  return 0;
}

int * read_three_triangles() {
  static int t[9];
  for (int i = 0; i < 9; i++) {
    int n = read_num();
    if (n == 0) {
      if (i == 0) {
	return 0;
      }
      fprintf(stderr, "triangle incomplete, missing %d sides.\n", 3 - i);
      abort();
    }
    t[i] = n;
  }
  return t;
}

int legal(int *t) {
  int n = 0;
  for (int i = 0; i < 3; i++) {
    if (t[i + 0] + t[i + 3] > t[i + 6]
	&& t[i + 0] + t[i + 6] > t[i + 3]
	&& t[i + 3] + t[i + 6] > t[i + 0]) {
      n++;
    }
  }
  return n;
}

int main() {
  int n = 0;
  int *t;
  while ((t = read_three_triangles()) != 0) {
    n += legal(t);
  }
  printf("Counted %d triangles.\n", n);
  return 0;
}

All in all I must say C was quite easy to slide back into. The core itself is easy. Luckily this exercise did not require memory management.

​#C ​#Advent of Code ​#Programming