From 07fbabd2ca781f119124ce1e9bec09113c986bd1 Mon Sep 17 00:00:00 2001 From: Shanti Chellaram Date: Sat, 23 Oct 2021 00:56:34 -0400 Subject: [PATCH] Working C++ Day 1 --- c++/Makefile | 8 ++++++++ c++/main.cpp | 30 ++++++++++++++++++++++++++++++ c++/problem1.cpp | 46 ++++++++++++++++++++++++++++++++++++++++++++++ c++/solutions.hpp | 9 +++++++++ 4 files changed, 93 insertions(+) create mode 100644 c++/Makefile create mode 100644 c++/main.cpp create mode 100644 c++/problem1.cpp create mode 100644 c++/solutions.hpp diff --git a/c++/Makefile b/c++/Makefile new file mode 100644 index 0000000..d834a23 --- /dev/null +++ b/c++/Makefile @@ -0,0 +1,8 @@ +aoc-cpp: $(wildcard *.cpp) + g++ -std=c++20 -o aoc-cpp $^ + +all: aoc-cpp + +.PHONY: clean +clean: + rm aoc-cpp diff --git a/c++/main.cpp b/c++/main.cpp new file mode 100644 index 0000000..a1c9d22 --- /dev/null +++ b/c++/main.cpp @@ -0,0 +1,30 @@ +#include +#include +#include + +#include "solutions.hpp" + +using namespace std; + +int main() { + auto x = solve1; + auto answer = x(cin); + cout << "Solution 1: " << answer.first << endl; + cout << "Solution 2: " << answer.second << endl; + return 0; +} + +//istream get_input() { +// CURL *curl; +// CURLcode res; +// +// char url[44] = {0}; +// sprintf(url, "https://adventofcode.com/2020/day/%d/input"); +// +// curl_global_init(CURL_GLOBAL_ALL); +// curl = curl_easy_init(); +// curl_easy_setopt(curl, CURLOPT_URL, "https://adventofcode.com/2020/day/1/input"); +// curl_easy_setopt(curl, CURLOPT_URL, "https://adventofcode.com/2020/day/1/input"); +// +// curl_global_cleanup(); +//} diff --git a/c++/problem1.cpp b/c++/problem1.cpp new file mode 100644 index 0000000..8dfd6f5 --- /dev/null +++ b/c++/problem1.cpp @@ -0,0 +1,46 @@ +#include +#include // for std::pair +#include +#include +#include +#include +//#include +#include +#include + +#include "solutions.hpp" + +using namespace std; + +pair solve1(istream &input) { + unordered_set seen; + unordered_map> targets; + string part1; + string part2; + bool part1_solved = false; + bool part2_solved = false; + for(string line; getline(input, line); ) { + int64_t number = stoi(line); + if (!part1_solved && seen.contains(2020 - number)) { + part1 = to_string((2020 - number) * number); + part1_solved = true; + } + if (!part2_solved) { + auto search = targets.find(number); + if (search != targets.end()) { + part2 = to_string(number * search->second.first * search->second.second); + part2_solved = true; + } + for (int64_t partner : seen) { + if (number + partner <= 2020) { + targets[2020 - number - partner] = pair(number, partner); + } + } + } + if (part1_solved && part2_solved) { + return pair(part1, part2); + } + seen.insert(number); + } + return pair(part1, part2); +} diff --git a/c++/solutions.hpp b/c++/solutions.hpp new file mode 100644 index 0000000..a79a764 --- /dev/null +++ b/c++/solutions.hpp @@ -0,0 +1,9 @@ +#pragma once + +#include +#include +#include + +#define DECL_SOLUTION(x) std::pair solve##x(std::istream&); + +DECL_SOLUTION(1)