~quf/computers-are-fast-2020

computers-are-fast-2020/run-all.c -rw-r--r-- 4.0 KiB
6f56515eLukas Himbert Draw the rest of the fine owl. 1 year, 11 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#define _XOPEN_SOURCE 600
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <time.h>
#include <assert.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <stdarg.h>
#include <sys/wait.h>

typedef struct {
  struct timespec start;
  struct timespec stop;
} timer;

void timer_start(timer *t) {
  clock_gettime(CLOCK_REALTIME, &t->start);
}

void timer_stop(timer *t) {
  clock_gettime(CLOCK_REALTIME, &t->stop);
}

double timer_delta_ns(timer *t) {
  double delta_ns = (double) t->stop.tv_nsec - (double) t->start.tv_nsec;
  double delta_s = (double) t->stop.tv_sec - (double) t->start.tv_sec;
  return delta_ns + 1e9 * delta_s;
}

void print_delta_ns(FILE *f, double ns) {
  if (ns >= 1e9) {
    /* seconds */
    fprintf(f, "%0.3lg s", ns * 1e-9);
  }
  else if (ns >= 1e6) {
    fprintf(f, "%0.3lg ms", ns * 1e-6);
  }
  else if (ns >= 1e3) {
    fprintf(f, "%0.3lg us", ns * 1e-3);
  }
  else {
    fprintf(f, "%0.3lg ns", ns);
  }
}

double smallest(const double *numbers, size_t n) {
  double min = numbers[0];
  for (size_t i = 1; i < n; ++i) {
    if (numbers[i] < min) {
      min = numbers[i];
    }
  }
  return min;
}

char *my_asprintf(const char *fmt, ...) {
  va_list args;
  /* calculate size */
  va_start(args, fmt);
  int n = vsnprintf(NULL, 0, fmt, args);
  va_end(args);
  /* allocate */
  size_t size = (size_t) n + 1; // + 1 for final \0.
  char *buf = malloc(size);
  assert(buf != NULL);
  /* print */
  va_start(args, fmt);
  n = vsnprintf(buf, size, fmt, args);
  va_end(args);
  /* check and return */
  assert(n >= 0);
  return buf;
}

bool run_day(unsigned day) {
  /* tries to run; return true if successful, false if the source file does not exist; aborts in case of any other problem */
  char *exe_fn = my_asprintf("src/%02u.c", day);
  char *inp_fn = my_asprintf("inputs/%02u", day);
  /* check if source code exists */
  struct stat buf;
  if (stat(exe_fn, &buf) != 0) {
    if (errno == ENOENT) {
      free(exe_fn);
      free(inp_fn);
      return false;
    }
    else {
      fprintf(stderr, "Day %02u ERROR: %s\n", day, strerror(errno));
      abort();
    }
  }
  /* Open input */
  int inp_fd = open(inp_fn, O_RDONLY);
  if (inp_fd < 0) {
    if (errno == ENOENT) {
      free(exe_fn);
      free(inp_fn);
      return false;
    }
    else {
      fprintf(stderr, "Day %02u ERROR: Could not open input file: %s\n", day, strerror(errno));
      abort();
    }
  }
  /* fork & exec */
  pid_t pid;
  if ((pid = fork()) == 0) {
    /* child: replace stdin with the input, stdin & stdout with /dev/null; then exec */
    if (dup2(inp_fd, 0) < 0) {
      fprintf(stderr, "IO redirect error: %s\n", strerror(errno));
      abort();
    }
    char * const args[] = { "tcc", "-run", exe_fn, NULL };
    assert(execvp("tcc", args) == 0);
  }
  else if (pid > 0) {
    /* parent, success */
    int status;
    assert(wait(&status) == pid);
    if (status != 0) {
      fprintf(stderr, "Day %02u ERROR: Program exit code %d.\n", day, status);
      abort();
    }
  }
  else {
    /* parent, failure */
    fprintf(stderr, "Day %02u ERROR: fork() failed: %s\n", day, strerror(errno));
    abort();
  }
  assert(close(inp_fd) == 0);
  free(inp_fn);
  free(exe_fn);
  return true;
}

int main(void) {
  timer t;
  timer_start(&t);
  for (unsigned day = 1; day <= 25; ++day) {
    printf("======= Day %02u =======\n", day);
    timer t;
    timer_start(&t);
    if (fflush(stdout) != 0) {
      fprintf(stderr, "Output error: %s\n", strerror(errno));
      return EXIT_FAILURE;
    }
    bool success = run_day(day);
    if (!success) {
      printf("MISSING\n");
    }
    if (fflush(stdout) != 0) {
      fprintf(stderr, "Output error: %s\n", strerror(errno));
      return EXIT_FAILURE;
    }
    timer_stop(&t);
    if (success) {
      printf("Took ");
      print_delta_ns(stdout, timer_delta_ns(&t));
      printf(".\n");
    }
  }
  timer_stop(&t);
  printf("Total runtime: ");
  print_delta_ns(stdout, timer_delta_ns(&t));
  printf("\n");
  return 0;
}