-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathday_14a.cpp
67 lines (63 loc) · 2.06 KB
/
day_14a.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
#include <array>
#include <fstream>
#include <iostream>
#include <regex>
#include <string>
#include <vector>
int main(int argc, char* argv[]) {
std::string input = "../input/day_14_input";
if (argc > 1) {
input = argv[1];
}
std::ifstream file(input);
std::string line;
constexpr int time = 100;
constexpr int room_col = 101;
constexpr int room_row = 103;
const std::regex pattern(R"(p=(-?[0-9]+),(-?[0-9]+) v=(-?[0-9]+),(-?[0-9]+))");
std::smatch match;
int total_cost = 0;
std::array<int, 4> count = {{0,0,0,0}};
std::array<std::array<int, room_col>, room_row> room;
for (auto& row : room) {
for (auto& ele : row) {
ele = 0;
}
}
while(std::getline(file, line)) {
std::regex_search(line, match, pattern);
const int p_col = std::stoi(match[1]);
const int p_row = std::stoi(match[2]);
const int v_col = std::stoi(match[3]);
const int v_row = std::stoi(match[4]);
int final_col = (p_col + v_col * time);
while(final_col < 0) {
final_col += (room_col * time);
}
final_col %= room_col;
int final_row = p_row + v_row * time;
while(final_row < 0) {
final_row += (room_row * time);
}
final_row %= room_row;
if (final_row < room_row / 2 && final_col < room_col / 2) count[0]++;
else if (final_row < room_row / 2 && final_col > room_col / 2) count[1]++;
else if (final_row > room_row / 2 && final_col < room_col / 2) count[2]++;
else if (final_row > room_row / 2 && final_col > room_col / 2) count[3]++;
room[final_row][final_col]++;
}
// for (const auto row : room) {
// for (const auto ele : row) {
// std::cout << char(ele == 0 ? '.' : ele + '0');
// }
// std::cout << '\n';
// }
int safety_factor = 1;
for (const auto ele : count) {
// std::cout << ele << ' ';
safety_factor *= ele;
}
// std::cout << '\n';
std::cout << safety_factor << '\n';
return 0;
}