-
Notifications
You must be signed in to change notification settings - Fork 9
/
day_03a.cpp
90 lines (84 loc) · 2.27 KB
/
day_03a.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
85
86
87
88
89
90
#include <algorithm>
#include <numeric>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
struct Number {
int col;
int row;
int number;
int order; // number of digits in number
};
int main(int argc, char * argv[]) {
std::string input = "../input/day_03_input";
if (argc > 1) {
input = argv[1];
}
std::string line;
std::fstream file(input);
std::vector<std::string> map;
std::vector<Number> numbers; // store numbers and their location
while(std::getline(file, line)) {
map.push_back(line);
bool is_number = false;
int number = -1;
int start = 0;
int order = 0;
const auto add_util = [&]() {
Number n;
n.row = map.size() - 1;
n.col = start;
n.number = number;
n.order = order;
numbers.push_back(n);
number = -1;
start = 0;
order = 0;
};
for (int i = 0; i < line.size(); i++) {
const char c = line[i];
if (c >= '0' && c <= '9') {
if (is_number == false) {
number = 0;
start = i;
order = 0;
}
is_number = true;
number = (number * 10) + int (c - '0');
order++;
} else {
is_number = false;
}
if (number != -1 && !is_number) {
add_util();
}
}
if (number != -1) {
add_util();
}
}
std::vector<int> parts;
for (const auto& n : numbers) {
bool still_searching = true;
for (int row = n.row - 1; (row <= n.row + 1) && still_searching; row++) {
if (row < 0 || row >= map.size()) continue;
// Only need to check the points around the number
// ????????
// ?number?
// ????????
// Check only the first and last element in the row that contains the number
const int col_increment = (row == n.row) ? n.order + 1 : 1;
for (int col = n.col - 1; (col <= n.col + n.order) && still_searching; col += col_increment) {
if (col < 0 || col >= map[row].size()) continue;
if (map[row][col] != '.' && !(map[row][col] >= '0' && map[row][col] <= '9')) {
still_searching = false;
parts.push_back(n.number);
}
}
}
}
const auto total = std::accumulate(std::begin(parts), std::end(parts), 0);
std::cout << total << '\n';
return 0;
}