-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathday_14b.cpp
84 lines (77 loc) · 2.68 KB
/
day_14b.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 <array>
#include <fstream>
#include <iostream>
#include <regex>
#include <string>
#include <vector>
#include <limits>
// Checked for lowest safety factor, printing out each time it reduced.
// Factor does not seem to increase after that, at least for a while
struct Robot {
int p_col;
int p_row;
int v_col;
int v_row;
};
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::vector<Robot> robots;
while(std::getline(file, line)) {
robots.emplace_back();
std::regex_search(line, match, pattern);
robots.back().p_col = std::stoi(match[1]) % room_col;
robots.back().p_row = std::stoi(match[2]) % room_row;
robots.back().v_col = std::stoi(match[3]) % room_col;
robots.back().v_row = std::stoi(match[4]) % room_row;
}
long long seconds_elapsed = 1;
int safety_factor = std::numeric_limits<int>::max();
while(true) {
std::array<std::array<int, room_col>, room_row> room;
for (auto& row : room) {
for (auto& ele : row) {
ele = 0;
}
}
std::array<int, 4> count = {{0,0,0,0}};
for (auto& robot : robots) {
robot.p_col += robot.v_col + room_col;
robot.p_col %= room_col;
robot.p_row += robot.v_row + room_row;
robot.p_row %= room_row;
room[robot.p_row][robot.p_col] = '#';
if (robot.p_row < room_row / 2 && robot.p_col < room_col / 2) count[0]++;
else if (robot.p_row < room_row / 2 && robot.p_col > room_col / 2) count[1]++;
else if (robot.p_row > room_row / 2 && robot.p_col < room_col / 2) count[2]++;
else if (robot.p_row > room_row / 2 && robot.p_col > room_col / 2) count[3]++;
}
int new_safety_factor = 1;
for (const auto ele : count) {
new_safety_factor *= ele;
}
if (new_safety_factor < safety_factor) {
safety_factor = new_safety_factor;
for (const auto row : room) {
for (const auto ele : row) {
std::cout << char(ele == 0 ? '.' : ele + '0');
}
std::cout << '\n';
}
std::cout << seconds_elapsed << '\n';
}
seconds_elapsed++;
}
return 0;
}