-
Notifications
You must be signed in to change notification settings - Fork 2
/
03.c
76 lines (63 loc) · 1.58 KB
/
03.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
#include <assert.h>
#include <err.h>
#include <stdint.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include "inputs/03.h"
typedef enum {
SQ_TREE = 1,
SQ_OPEN = 0,
} square_t;
const int HEIGHT = 323;
const int WIDTH = 31;
static uint64_t count_trees(square_t* grid, int slope_y, int slope_x);
int day3(void) {
const unsigned char *s = input;
square_t* grid = calloc(HEIGHT * WIDTH, sizeof(square_t));
if (!grid) {
err(EXIT_FAILURE, "error allocating memory for grid values");
}
int y = 0;
while (*s != '\0') {
int x = 0;
while (*s != '\n' && *s != '\0') {
grid[y * WIDTH + x] = (*s == '#') ? SQ_TREE : SQ_OPEN;
s++;
x++;
}
if (*s == '\n') {
s++;
y++;
}
}
// // print grid
// for (int y=0; y < HEIGHT; y++) {
// for (int x=0; x < WIDTH; x++) {
// printf("%c", grid[y][x] == SQ_OPEN ? '.' : '#');
// }
// printf("\n");
// }
// last x, y
// printf("\n\nLast: %d\n", grid[(HEIGHT-1) * WIDTH + WIDTH-1]);
int64_t product = count_trees(grid, 1, 1) * count_trees(grid, 1, 3) *
count_trees(grid, 1, 5) * count_trees(grid, 1, 7) *
count_trees(grid, 2, 1);
printf("%ld\n", product);
assert(product == 4355551200);
free(grid);
return 0;
}
static
uint64_t count_trees(square_t* grid, int slope_y, int slope_x) {
uint64_t tree_count = 0;
for (int y = 0, x = 0; y < 322; y += slope_y, x += slope_x) {
if (x >= 31) {
x -= 31;
}
if (grid[y * WIDTH + x] == SQ_TREE) {
tree_count++;
}
}
return tree_count;
}