diff --git a/2020/day1 b/2020/day1 new file mode 100755 index 0000000..4cdc619 Binary files /dev/null and b/2020/day1 differ diff --git a/2020/day1.cpp b/2020/day1.cpp index a9a028c..ca56491 100644 --- a/2020/day1.cpp +++ b/2020/day1.cpp @@ -51,10 +51,18 @@ auto find_2020_x2(const std::vector & list) -> void { auto find_2020_x3(const std::vector & list) -> void { for (auto n0 = 0; n0 < list.size() - 2; ++n0) { for (auto n1 = 1; n1 < list.size() - 1; ++n1) { - for (auto n2 = 2; n2 < list.size(); ++n2) { - if (list[n0] + list[n1] + list[n2] == 2020) { + auto low = n0 + 1; + 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; return; + } else if (sum > 2020) { + low = n2 + 1; + } else { + high = n2; } } }