-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday_13.cpp
45 lines (40 loc) · 1.21 KB
/
day_13.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
// Advent of Code 2017
// Day 13 - Packet Scanners
#include <iostream>
#include <vector>
#include <algorithm>
#include <chrono>
using namespace std;
int main(int argc, char* argv[])
{
int depth, range;
vector<int> depths, ranges;
while (cin >> depth) {
if (cin.peek() == ':') cin.get();
cin >> range;
depths.push_back(depth);
ranges.push_back(range);
}
int severity{ 0 };
for (int step = 0; step<depths.size(); ++step) {
severity += (depths[step] % ((ranges[step] - 1) * 2))==0 ? (depths[step]*ranges[step]) : 0;
}
cout << "Part 1: " << severity << endl;
auto start = std::chrono::high_resolution_clock::now();
auto delay{ 0 }, fail{ 0 };
for (;; ++delay) {
fail = 0;
for (auto step{ 0 }; fail == 0 && step < depths.size(); ++step)
fail = !((depths[step] + delay) % ((ranges[step] - 1) * 2));
if (fail == 0) break;
}
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> elapsed_seconds = end - start;
cout << "Part 2: " << delay << " in " << elapsed_seconds.count()*1000 << " msec" << endl;
return 0;
}
/*
Output:
Part 1: 1640
Part 2: 3960702 in 49.2783 msec
*/