-
Notifications
You must be signed in to change notification settings - Fork 2
/
08.c
82 lines (66 loc) · 1.51 KB
/
08.c
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
#include "adventofcode.h"
#include <assert.h>
#include <linux/limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define PUZZLE_NAME "Day 8: Memory Maneuver"
static const char *pt1(const char *s, int *sum) {
int nchilds;
int nmetadata;
s = parse_int(&nchilds, s);
s = skip(" ", s);
s = parse_int(&nmetadata, s);
s = skip(" ", s);
for (int i = 0; i < nchilds; i++) {
s = pt1(s, sum);
}
for (int i = 0; i < nmetadata; i++) {
int value;
s = parse_int(&value, s);
if (*s == ' ')
s++;
*sum += value;
}
return s;
}
static const char *pt2(const char *s, int *sum) {
int nchilds;
int nmetadata;
s = parse_int(&nchilds, s);
s = skip(" ", s);
s = parse_int(&nmetadata, s);
s = skip(" ", s);
int child_values[32];
assert(nchilds <= 32);
for (int i = 0; i < nchilds; i++) {
child_values[i] = 0;
s = pt2(s, &child_values[i]);
}
for (int i = 0; i < nmetadata; i++) {
int value;
s = parse_int(&value, s);
if (*s == ' ')
s++;
if (nchilds == 0) {
*sum += value;
} else if (value > 0 && value <= nchilds) {
*sum += child_values[value - 1];
}
}
return s;
}
int main(void) {
clock_t t = clock_time();
char input[64 << 10];
read_input_file(input, 64 << 10, "08.txt");
int a1 = 0;
pt1(input, &a1);
int a2 = 0;
pt2(input, &a2);
printf("--- %s ---\n", PUZZLE_NAME);
printf("Part 1: %d\n", a1);
printf("Part 2: %d\n", a2);
printf("Time: %.2fms\n", clock_time_since(t));
return EXIT_SUCCESS;
}