|
|
@ -1,12 +1,14 @@ |
|
|
|
#include "Day06.h" |
|
|
|
#include "Day06.h" |
|
|
|
|
|
|
|
#include <cmath> |
|
|
|
|
|
|
|
|
|
|
|
Result Day06::Task1() { |
|
|
|
Result Day06::Task1() { |
|
|
|
uint64 product = 1; |
|
|
|
uint64 product = 1; |
|
|
|
|
|
|
|
|
|
|
|
for (auto &[time, distance] : parseRecords()){ |
|
|
|
for (auto &[time, distance] : parseRecords()){ |
|
|
|
uint64 wins = 0; |
|
|
|
auto [x1, x2] = calculateEdges(double(time), double(distance)); |
|
|
|
for (uint64 holdTime = 1; holdTime < time; holdTime++) |
|
|
|
|
|
|
|
if ((time - holdTime) * holdTime > distance) wins++; |
|
|
|
uint64 wins = uint64(ceil(x2 - 1)) - uint64(x1); |
|
|
|
|
|
|
|
|
|
|
|
product *= wins; |
|
|
|
product *= wins; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -16,9 +18,9 @@ Result Day06::Task1() { |
|
|
|
Result Day06::Task2() { |
|
|
|
Result Day06::Task2() { |
|
|
|
auto [time, distance] = parseRecord(); |
|
|
|
auto [time, distance] = parseRecord(); |
|
|
|
|
|
|
|
|
|
|
|
uint64 wins = 0; |
|
|
|
auto [x1, x2] = calculateEdges(double(time), double(distance)); |
|
|
|
for (uint64 holdTime = 1; holdTime < time; holdTime++) |
|
|
|
|
|
|
|
if ((time - holdTime) * holdTime > distance) wins++; |
|
|
|
uint64 wins = uint64(ceil(x2 - 1)) - uint64(x1); |
|
|
|
|
|
|
|
|
|
|
|
return to_string(wins); |
|
|
|
return to_string(wins); |
|
|
|
} |
|
|
|
} |
|
|
@ -44,3 +46,10 @@ Day06::Record Day06::parseRecord() const { |
|
|
|
uint64 distance = std::stoul(rawDistance); |
|
|
|
uint64 distance = std::stoul(rawDistance); |
|
|
|
return {time, distance}; |
|
|
|
return {time, distance}; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pair<double, double> Day06::calculateEdges(double t, double d) { |
|
|
|
|
|
|
|
// parabula f(x) = -x² + tx | set it equal to d, calculate x1, x2
|
|
|
|
|
|
|
|
double x1 = t / 2 - sqrt(t * t / 4 - d); |
|
|
|
|
|
|
|
double x2 = t / 2 + sqrt(t * t / 4 - d); |
|
|
|
|
|
|
|
return {x1, x2}; |
|
|
|
|
|
|
|
} |
|
|
|