-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path15.cpp
78 lines (64 loc) · 1.67 KB
/
15.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
#include <assert.h>
#include <chrono>
#include <iostream>
#include <regex>
using std::getline;
using std::regex;
using std::smatch;
using std::string;
using std::vector;
class Disc {
public:
int start;
int npositions;
};
int drop_capsule(const vector<Disc>& discs) {
// brute-force every start time until we were able to pass all disks
// an alternative approach may be to use LCM?
for (int tstart = 0;; tstart++) {
int t = tstart;
bool all = true;
for (Disc d : discs) {
t += 1;
if ((d.start + t) % d.npositions != 0) {
all = false;
break;
}
}
if (all) {
return tstart;
}
}
return -1;
}
int main() {
auto tstart = std::chrono::high_resolution_clock::now();
int pt1 = 0;
int pt2 = 0;
// parse puzzle input
string input;
regex rx(
"^Disc #(\\d+) has (\\d+) positions; at time=0, it is at "
"position (\\d+).");
smatch matches;
vector<Disc> discs;
while (getline(std::cin, input)) {
std::regex_match(input, matches, rx);
assert(matches.size() == 4);
int start = std::stoi(matches[3].str());
int positions = std::stoi(matches[2].str());
discs.push_back(Disc{start, positions});
}
pt1 = drop_capsule(discs);
discs.push_back(Disc{0, 11});
pt2 = drop_capsule(discs);
std::cout << "--- Day 15: Timing is Everything ---\n";
std::cout << "Part 1: " << pt1 << "\n";
std::cout << "Part 2: " << pt2 << "\n";
auto tstop = std::chrono::high_resolution_clock::now();
auto duration =
std::chrono::duration_cast<std::chrono::microseconds>(tstop - tstart);
std::cout << "Time: " << duration.count() << " μs"
<< "\n";
return EXIT_SUCCESS;
}