~quf/computers-are-fast-2020

computers-are-fast-2020/src/10.c -rw-r--r-- 1.3 KiB
6f56515eLukas Himbert Draw the rest of the fine owl. 1 year, 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
47
48
49
50
51
52
53
54
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdbool.h>
#include <stdint.h>

#define LENGTH(a) (sizeof a / sizeof *a)

int main(void) {
  bool adapters[200] = { 0, };
  adapters[0] = true;
  int ret = 0;
  size_t max_rating = 0;
  size_t rating;
  while ((ret = scanf("%zu\n", &rating)) > 0) {
    if (rating >= LENGTH(adapters)) {
      fprintf(stderr, "Error: Too many adapters.\n");
      return 1;
    }
    adapters[rating] = true;
    if (rating > max_rating) {
      max_rating = rating;
    }
  }
  if (ferror(stdin)) {
    fprintf(stderr, "Input error: %s.\n", strerror(errno));
    return 1;
  }

  long long combinations[2 + LENGTH(adapters)] = { 0, };
  combinations[max_rating] = 1;
  unsigned deltas[4] = { 0, };
  size_t delta = 0;
  for (size_t i = max_rating - 1; i != SIZE_MAX; --i) {
    ++delta;
    if (adapters[i]) {
      combinations[i] += combinations[i+1];
      combinations[i] += combinations[i+2];
      combinations[i] += combinations[i+3];
      if (delta > LENGTH(deltas)) {
        fprintf(stderr, "Error: gap in ratings larger than 3.\n");
        return 1;
      }
      else {
        ++deltas[delta];
        delta = 0;
      }
    }
  }

  printf("%u\n", deltas[1] * (deltas[3]+1));
  printf("%lld\n", combinations[0]);
  return 0;
}