-
Notifications
You must be signed in to change notification settings - Fork 9
/
day_03b.cpp
107 lines (100 loc) · 2.58 KB
/
day_03b.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#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
};
struct Gear {
int n = 0;
std::vector<int> numbers;
};
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::unordered_map<int, Gear> gears;
for (const auto& n : numbers) {
for (int row = n.row - 1; (row <= n.row + 1); 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); col += col_increment) {
if (col < 0 || col >= map[row].size()) continue;
if (map[row][col] == '*') {
const int id = map[0].size() * row + col; // assumes each row is the same length
if (gears.find(id) == gears.end()) {
Gear g;
g.n = 1;
g.numbers.push_back(n.number);
gears[id] = g;
} else {
gears[id].n++;
gears[id].numbers.push_back(n.number);
}
}
}
}
}
std::size_t ans = 0;
for (const auto& [id, gear] : gears) {
if (gear.n == 2) {
ans += (gear.numbers[0] * gear.numbers[1]);
}
}
std::cout << ans << '\n';
return 0;
}