-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay3.cpp
84 lines (65 loc) · 1.72 KB
/
Day3.cpp
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
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <assert.h>
#include <string>
#include <vector>
#include <chrono>
#include "utils.cpp"
void printGrid(std::vector<std::string>& grid) {
for (int j = 0; j < grid.size(); j++) {
for (int i = 0; i < grid[0].size(); i++) {
printf("%c", grid[j][i]);
}
printf("\n");
}
}
void printGrid(std::vector<std::string>& grid, vec2 position) {
for (int j = 0; j < grid.size(); j++) {
for (int i = 0; i < grid[0].size(); i++) {
if (position.y == j && position.x == i) {
if (grid[j][i] == '.') {
printf("O");
} else {
printf("X");
}
} else {
printf("%c", grid[j][i]);
}
}
printf("\n");
}
}
i32 main(i32 argc, char const *argv[]) {
auto begin = std::chrono::high_resolution_clock::now();
auto input = loadInput("Day3_input.txt");
std::vector<std::string> grid;
while (input.cursor < input.end) {
auto line = getLine(&input);
grid.push_back(line);
}
std::vector<vec2> gradients = {{1,1},{3,1},{5,1},{7,1},{1,2}};
std::vector<i32> numTrees(gradients.size());
for (int i = 0; i < gradients.size(); i++) {
vec2 position = gradients[i];
while (position.y < grid.size()) {
if (grid[position.y][position.x] == '#') {
++numTrees[i];
}
position += gradients[i];
position.x = position.x % grid[0].size();
}
}
printf("number of trees encountered = \n");
i64 product = 1;
for (auto n: numTrees) {
printf("%d, ", n);
product *= n;
}
printf("\n");
printf("product = %lld \n", product);
auto end = std::chrono::high_resolution_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
printf("Time measured: %.3f miliseconds.\n", elapsed.count() * 1e-6);
return 0;
}