-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday13.cpp
65 lines (54 loc) · 1.66 KB
/
day13.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
#include "utils/helpers.hpp"
bool almostEqual(long long a, double b) {
double epsilon = 0.001;
if (a + epsilon > b && a - epsilon < b)
return true;
return false;
}
ll searchLowerPrice(const Coord *machine)
{
double epsilon = 0.001;
Vector XY({
machine[2].first,
machine[2].second
});
Matrix buttons({
{machine[0].first, machine[1].first},
{machine[0].second, machine[1].second}
});
Vector ab = buttons.inverse() * XY;
ll a = ab[0] + epsilon;
ll b = ab[1] + epsilon;
if (almostEqual(a, ab[0]) && almostEqual(b, ab[1])) {
return 3 * a + b;
}
return 0;
}
int main() {
ui part = 0;
std::ifstream input;
inputLib::extracted extraced;
Coord machine[] = {Coord(0, 0), Coord(0, 0), Coord(0, 0)};
ll res = 0;
if (getFileAndPart(13, input, part))
return errno;
while (extraced.second != EOF)
{
for (int i = 0; i < 3; ++i) {
if (i == 2 && part != 1) {
extraced = inputLib::extractNextNumber(input);
machine[i].first = extraced.first.value() + 10000000000000;
extraced = inputLib::extractNextNumber(input);
machine[i].second = extraced.first.value() + 10000000000000;
}
else {
extraced = inputLib::extractNextNumber(input);
machine[i].first = extraced.first.value();
extraced = inputLib::extractNextNumber(input);
machine[i].second = extraced.first.value();
}
}
res += searchLowerPrice(machine);
}
std::cout << "result is " << res << '\n';
}