-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathadventofcode.h
130 lines (109 loc) · 2.85 KB
/
adventofcode.h
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
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
/* Allocate SIZE bytes of memory. */
static inline void *malloc_or_die(unsigned long size) {
void *ptr = malloc(size);
if (ptr == NULL) {
perror("malloc error");
exit(EXIT_FAILURE);
}
return ptr;
}
/* Allocate NMEMB elements of SIZE bytes each, all initialized to 0. */
static inline void *calloc_or_die(unsigned long n, unsigned long size) {
void *ptr = calloc(n, size);
if (ptr == NULL) {
perror("calloc error");
exit(EXIT_FAILURE);
}
return ptr;
}
static size_t read_input_file(char *dest, unsigned int size, char *file) {
FILE *fp = fopen(file, "r");
if (!fp) {
fprintf(stderr, "error reading %s", file);
exit(EXIT_FAILURE);
return 0;
}
size_t nread = fread(dest, 1, size, fp);
// skip trailing newlines
while (dest[nread - 1] == '\n')
nread--;
dest[nread] = '\0';
fclose(fp);
return nread;
}
// parse_ident parses a string until the next whitespace
static inline const char *parse_ident(char *dst, const char *s) {
while ((*s >= 'a' && *s <= 'z') || (*s >= 'A' && *s <= 'Z')) {
*dst++ = *s++;
}
*dst = '\0';
return s;
}
// parse_int parses a (signed) integer, with optional plus sign
static inline const char *parse_int(int *dst, const char *s) {
int n = 0;
int mod = 1;
if (*s == '-') {
mod = -1;
s++;
} else if (*s == '+') {
s++;
}
while (*s >= '0' && *s <= '9') {
n = (n * 10) + (*s - '0');
s++;
}
*dst = n * mod;
return s;
}
static inline const char *parse_uint(unsigned int *dst, const char *s) {
return parse_int((int *)dst, s);
}
// parse_int parses a (signed) integer, with optional plus sign
static inline const char *parse_uint16(int16_t *dst, const char *s) {
return parse_int((int *)dst, s);
}
// parse_uint8 parses a single unsigned byte value
static inline const char *parse_uint8(uint8_t *dst, char *s) {
return parse_int((int *)dst, s);
}
// skip advanced the pointer s as long as it matches the corresponding chars
// from expects
static inline const char *skip(const char *expects, const char *s) {
while (*s == *expects && *expects != 0x0) {
s++;
expects++;
};
if (*expects != '\0') {
printf("Parse error. Expected '%c', got '%c'.\n%s\n", *expects, *s, s);
exit(EXIT_FAILURE);
}
return s;
}
static inline const char *skip_optional(char c, const char *s) {
if (*s == c) {
s++;
}
return s;
}
static inline const char *skip_until(char c, const char *s) {
while (*s != c && *s != 0x0) {
s++;
}
return s;
}
static inline const char *skip_until_digit(const char *s) {
while ((*s < '0' || *s > '9') && *s != 0x0) {
s++;
}
return s;
}
static clock_t clock_time(void) { return clock(); }
static double clock_time_since(clock_t start_t) {
clock_t end_t = clock();
return (double)(end_t - start_t) / CLOCKS_PER_SEC * 1000;
}