advent_of_code/2020/days/day1.cpp

87 lines
2.6 KiB
C++
Raw Permalink Normal View History

2020-12-01 08:00:04 +00:00
/*******************************************************************************
*
* Copyright (c) 2020 Gnarwhal
*
* -----------------------------------------------------------------------------
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files(the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*******************************************************************************/
#include <vector>
#include <algorithm>
#include <string>
#include <iostream>
#include <fstream>
2020-12-11 17:24:22 +00:00
#include "../misc/types.hpp"
#include "../misc/print.hpp"
2020-12-01 08:00:04 +00:00
// Find 2 numbers that sum to 2020
auto find_2020_x2(const std::vector<i32> & list) -> void {
auto begin = 0;
auto end = list.size() - 1;
auto sum = 0;
while ((sum = list[begin] + list[end]) != 2020) {
if (sum < 2020) {
++begin;
} else {
--end;
}
}
2020-12-11 17:24:22 +00:00
print((list[begin] * list[end]));
2020-12-01 08:00:04 +00:00
}
// Find 3 numbers that sum to 2020
auto find_2020_x3(const std::vector<i32> & list) -> void {
for (auto n0 = 0; n0 < list.size() - 2; ++n0) {
for (auto n1 = 1; n1 < list.size() - 1; ++n1) {
auto low = n0 + 1;
auto high = n1;
2020-12-11 17:24:22 +00:00
while (low < high) {
auto n2 = (low + high) / 2;
auto sum = 0;
if ((sum = list[n0] + list[n1] + list[n2]) == 2020) {
2020-12-11 17:24:22 +00:00
print((list[n0] * list[n1] * list[n2]));
2020-12-01 08:00:04 +00:00
return;
2020-12-11 17:24:22 +00:00
} else if (sum < 2020) {
low = n2 + 1;
} else {
high = n2;
2020-12-01 08:00:04 +00:00
}
}
}
}
}
2020-12-11 17:24:22 +00:00
auto day1() -> void {
2020-12-01 08:00:04 +00:00
auto list = std::vector<i32>();
{
auto line = std::string();
2020-12-11 17:24:22 +00:00
auto file = std::ifstream("inputs/day1.input");
2020-12-01 08:00:04 +00:00
while (getline(file, line)) {
list.push_back(std::stoi(line));
}
}
std::sort(list.begin(), list.end());
find_2020_x2(list);
find_2020_x3(list);
}