-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path12.c
93 lines (77 loc) · 2.47 KB
/
12.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
83
84
85
86
87
88
89
90
91
92
93
#include "adventofcode.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define PUZZLE_NAME "Day 12: Subterranean Sustainability"
#define PADDING 200 // should be larger than # of cycles
#define INITIAL_PLANTS 99
#define WIDTH (INITIAL_PLANTS + PADDING * 2)
struct Pattern {
char in[5];
char out;
};
static unsigned long count_plants(const char plants[static WIDTH], const unsigned long offset) {
unsigned long count = 0;
// for part 2 I noticed the grid would stabilise
// and then shifts right by 100 every 100 iterations
// so I used that property to compute the answer...
// not sure how it holds for other puzzle inputs though
for (unsigned i = 0; i < WIDTH; i++) {
if (plants[i] == '#') {
count += (unsigned long)((long)i - (long)PADDING) + offset;
}
}
return count;
}
static void growth_cycles(char plants[static WIDTH], const struct Pattern patterns[],
const unsigned npatterns, const unsigned ncycles) {
char next[WIDTH];
for (unsigned g = 0; g < ncycles; g++) {
memset(next, '.', WIDTH * sizeof(char));
// go over every plant
for (unsigned p = 2; p < WIDTH - 2; p++) {
for (unsigned r = 0; r < npatterns; r++) {
if (memcmp(patterns[r].in, &plants[p - 2], 5) == 0) {
next[p] = patterns[r].out;
break;
}
}
}
memcpy(plants, next, WIDTH * sizeof(char));
}
}
int main(void) {
clock_t t = clock_time();
char line[1024];
fgets(line, 1024, stdin);
// 20 generations
// so 32 padding should be enough
char plants[WIDTH];
memset(plants, '.', WIDTH * sizeof(char));
memcpy(&plants[PADDING], &line[15], INITIAL_PLANTS * sizeof(char));
// skip 2nd line
fgets(line, 1024, stdin);
unsigned npatterns = 0;
struct Pattern patterns[64];
while (fgets(line, 1024, stdin) && npatterns < 64) {
// we only need to know about the patterns that result into a plant
if (line[9] != '#') {
continue;
}
memcpy(patterns[npatterns].in, line, 5 * sizeof(char));
patterns[npatterns].out = line[9];
npatterns++;
}
unsigned long pt1 = 0;
unsigned long pt2 = 0;
growth_cycles(plants, patterns, npatterns, 20);
pt1 = count_plants(plants, 0);
growth_cycles(plants, patterns, npatterns, 180);
pt2 = count_plants(plants, 49999999800ul);
printf("--- %s ---\n", PUZZLE_NAME);
printf("Part 1: %ld\n", pt1);
printf("Part 2: %ld\n", pt2);
printf("Time: %.2fms\n", clock_time_since(t));
return EXIT_SUCCESS;
}