In a weary stupor my code grew wrought with linear searching a sorted list and has been dealt with accordingly :vomit:

This commit is contained in:
Gnarwhal 2020-12-01 07:11:29 -08:00
parent a70e623f2a
commit 0a65e04354
Signed by: Gnarwhal
GPG key ID: 0989A73D8C421174
2 changed files with 10 additions and 2 deletions

BIN
2020/day1 Executable file

Binary file not shown.

View file

@ -51,10 +51,18 @@ auto find_2020_x2(const std::vector<i32> & list) -> void {
auto find_2020_x3(const std::vector<i32> & list) -> void { auto find_2020_x3(const std::vector<i32> & list) -> void {
for (auto n0 = 0; n0 < list.size() - 2; ++n0) { for (auto n0 = 0; n0 < list.size() - 2; ++n0) {
for (auto n1 = 1; n1 < list.size() - 1; ++n1) { for (auto n1 = 1; n1 < list.size() - 1; ++n1) {
for (auto n2 = 2; n2 < list.size(); ++n2) { auto low = n0 + 1;
if (list[n0] + list[n1] + list[n2] == 2020) { auto high = n1;
auto n2 = (low + high) / 2;
while (low < high - 1) {
auto sum = 0;
if ((sum = list[n0] + list[n1] + list[n2]) == 2020) {
std::cout << (list[n0] * list[n1] * list[n2]) << std::endl; std::cout << (list[n0] * list[n1] * list[n2]) << std::endl;
return; return;
} else if (sum > 2020) {
low = n2 + 1;
} else {
high = n2;
} }
} }
} }