-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathday_06b.cpp
43 lines (38 loc) · 1.38 KB
/
day_06b.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
#include <algorithm>
#include <cmath>
#include <fstream>
#include <iostream>
#include <numeric>
#include <string>
#include <vector>
long long extract_number(const std::string& s, const int delta) {
std::string number;
std::size_t start = s.find_first_not_of(' ', delta);
std::size_t end = s.find(' ', start);
while (end != std::string::npos) {
number += s.substr(start, end - start);
start = s.find_first_not_of(' ', end + 1);
end = s.find(' ', start);
}
number += s.substr(start, s.size() - start);
return std::stoll(number);
}
int main(int argc, char * argv[]) {
std::string input = "../input/day_06_input";
if (argc > 1) {
input = argv[1];
}
std::string line;
std::fstream file(input);
std::getline(file, line);
const auto race_time = extract_number(line, 5);
std::getline(file, line);
const auto current_record = extract_number(line, 9);
const double discriminant_sqrt = std::sqrt(race_time * race_time - 4 * (1) * (current_record));
long long possible_root_1 = std::floor(race_time + discriminant_sqrt)/2;
long long possible_root_2 = std::ceil(race_time - discriminant_sqrt)/2;
if (possible_root_1 * (race_time - possible_root_1) <= current_record) possible_root_1--;
if (possible_root_2 * (race_time - possible_root_2) <= current_record) possible_root_2++;
std::cout << (possible_root_1 - possible_root_2 + 1) << '\n';
return 0;
}