blob: 9a429c5b286d17efe58b35ce419f373342a0e04e (
plain)
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
|
#include <cstdint>
#include <fstream>
#include <iostream>
#include <numeric>
#include <sstream>
#include <vector>
using namespace std;
template<typename T = long>
vector<T>
read_ints(const string& line)
{
stringstream iss{ line };
return vector<T>{ istream_iterator<T>{ iss }, istream_iterator<T>{} };
}
long
solve(long time, long winningDistance)
{
long counter = 0;
for ( long pushTime = 0; pushTime < time; ++pushTime ) {
auto distance = (time * pushTime - pushTime * pushTime);
counter += long(distance > winningDistance);
}
return counter;
}
void
part1()
{
fstream input{ "data/day06.txt" };
string line;
getline(input, line);
auto times = read_ints(line.substr(line.find(':') + 1));
getline(input, line);
auto distances = read_ints(line.substr(line.find(':') + 1));
long result = 1;
for ( size_t idx = 0; idx != times.size(); ++idx ) {
result *= solve(times[idx], distances[idx]);
}
cout << result << endl;
}
string
join(const string& line)
{
stringstream iss{ line };
return accumulate(istream_iterator<string>{ iss }, istream_iterator<string>{}, string{});
}
void
part2()
{
fstream input{ "data/day06.txt" };
string line;
getline(input, line);
auto time = stol(join(line.substr(line.find(':') + 1)));
getline(input, line);
auto distance = stol(join(line.substr(line.find(':') + 1)));
cout << solve(time, distance) << endl;
}
int
main()
{
part1();
part2();
}
|