diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..8a10953 --- /dev/null +++ b/.clang-format @@ -0,0 +1,12 @@ +--- +Language: Cpp +BasedOnStyle: WebKit +SpaceAfterTemplateKeyword: false +AlignEscapedNewlines: true +AlignTrailingComments: true +BreakBeforeInheritanceComma: true +BreakConstructorInitializers: BeforeComma +IndentPPDirectives: AfterHash +BreakBeforeBraces: Custom +BraceWrapping: + AfterFunction: true diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..b7a561c --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,7 @@ +cmake_minimum_required(VERSION 3.15) +project(aoc2019) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(aoc2019 + src/Solution.h src/main.cpp src/Day1Solution.cpp src/Day1Solution.h src/Day2Solution.cpp src/Day2Solution.h) diff --git a/Makefile b/Makefile deleted file mode 100644 index e893316..0000000 --- a/Makefile +++ /dev/null @@ -1,15 +0,0 @@ -CC=g++ -CFLAGS=-Wall -SRC_DIR=src -OUTPUT_DIR=output - -all: day1 day2 - -day1: - $(CC) $(CFLAGS) $(SRC_DIR)/day1.cpp -o $(OUTPUT_DIR)/$@ - -day2: - $(CC) $(CFLAGS) $(SRC_DIR)/day2.cpp -o $(OUTPUT_DIR)/$@ - -clean: - rm $(OUTPUT_DIR)/* diff --git a/output/placeholder b/output/placeholder deleted file mode 100644 index e69de29..0000000 diff --git a/src/day1.cpp b/src/Day1Solution.cpp similarity index 63% rename from src/day1.cpp rename to src/Day1Solution.cpp index 35beba8..fadef85 100644 --- a/src/day1.cpp +++ b/src/Day1Solution.cpp @@ -1,10 +1,10 @@ -#include -#include -#include -#include +#include "Day1Solution.h" #include +#include +#include +#include -int main() +void Day1Solution::run() { std::string line; std::ifstream input_file("../input/day1.txt"); @@ -12,16 +12,13 @@ int main() auto part1_sum = 0; auto part2_sum = 0; - if (input_file.is_open()) - { - while (getline(input_file, line)) - { + if (input_file.is_open()) { + while (getline(input_file, line)) { auto val = std::stoi(line); auto calculated_value = std::floor(val / 3) - 2; part1_sum += calculated_value; - while (calculated_value > 0) - { + while (calculated_value > 0) { part2_sum += calculated_value; calculated_value = std::floor(calculated_value / 3) - 2; } @@ -29,8 +26,6 @@ int main() input_file.close(); } - std::cout << "Part 1 Sum: " << part1_sum << std::endl; - std::cout << "Part 2 Sum: " << part2_sum << std::endl; - - return 0; + std::cout << "Part 1 Answer: " << part1_sum << std::endl; + std::cout << "Part 2 Answer: " << part2_sum << std::endl; } \ No newline at end of file diff --git a/src/Day1Solution.h b/src/Day1Solution.h new file mode 100644 index 0000000..a57a125 --- /dev/null +++ b/src/Day1Solution.h @@ -0,0 +1,11 @@ +#ifndef AOC2019_DAY1SOLUTION_H +#define AOC2019_DAY1SOLUTION_H + +#include "Solution.h" + +class Day1Solution : public Solution { +public: + void run() override; +}; + +#endif //AOC2019_DAY1SOLUTION_H diff --git a/src/day2.cpp b/src/Day2Solution.cpp similarity index 60% rename from src/day2.cpp rename to src/Day2Solution.cpp index fb2518a..44507fc 100644 --- a/src/day2.cpp +++ b/src/Day2Solution.cpp @@ -1,23 +1,40 @@ -#include +#include "Day2Solution.h" #include -#include -#include -#include +#include -int run_program(std::vector program, int noun, int verb) +void Day2Solution::run() { - int program_counter = 0; - bool running = true; + std::string line; + std::ifstream input_file("../input/day2.txt"); + std::vector program; + + if (input_file.is_open()) { + while (getline(input_file, line, ',')) { + program.push_back(std::stoi(line)); + } + + auto result1 = run_program(program, 12, 2); + std::cout << "Part 1 Answer: " << result1 << std::endl; + + auto result2 = search_program(program, 19690720); + std::cout << "Part 2 Answer: " << result2 << std::endl; + + input_file.close(); + } +} + +int Day2Solution::run_program(std::vector program, int noun, int verb) +{ + auto program_counter = 0; + auto running = true; program[1] = noun; program[2] = verb; - while (running) - { + while (running) { auto opcode = program[program_counter++]; auto val1 = program[program[program_counter++]]; auto val2 = program[program[program_counter++]]; - switch (opcode) - { + switch (opcode) { case 1: // addition program[program[program_counter++]] = val1 + val2; break; @@ -33,43 +50,15 @@ int run_program(std::vector program, int noun, int verb) return program[0]; } -int search_program(std::vector program, int expected_value) +int Day2Solution::search_program(std::vector program, int expected_value) { - for (auto noun = 0; noun < 99; noun++) - { - for (auto verb = 0; verb < 99; verb++) - { + for (auto noun = 0; noun < 99; noun++) { + for (auto verb = 0; verb < 99; verb++) { auto value = run_program(program, noun, verb); - if (value == expected_value) - { + if (value == expected_value) { return 100 * noun + verb; } } } return -1; -} - -int main() -{ - std::string line; - std::ifstream input_file("../input/day2.txt"); - std::vector program; - - if (input_file.is_open()) - { - while (getline(input_file, line, ',')) - { - program.push_back(std::stoi(line)); - } - - auto result1 = run_program(program, 12, 2); - std::cout << "Part 1 Result: " << result1 << std::endl; - - auto result2 = search_program(program, 19690720); - std::cout << "Part 2 Result: " << result2 << std::endl; - - input_file.close(); - } - - return 0; } \ No newline at end of file diff --git a/src/Day2Solution.h b/src/Day2Solution.h new file mode 100644 index 0000000..ed987e4 --- /dev/null +++ b/src/Day2Solution.h @@ -0,0 +1,16 @@ +#ifndef AOC2019_DAY2SOLUTION_H +#define AOC2019_DAY2SOLUTION_H + +#include "Solution.h" +#include + +class Day2Solution : public Solution { +public: + void run() override; + +private: + int run_program(std::vector program, int noun, int verb); + int search_program(std::vector program, int expected_value); +}; + +#endif //AOC2019_DAY2SOLUTION_H diff --git a/src/Solution.h b/src/Solution.h new file mode 100644 index 0000000..e90104f --- /dev/null +++ b/src/Solution.h @@ -0,0 +1,11 @@ +#ifndef AOC2019_SOLUTION_H +#define AOC2019_SOLUTION_H + +#include + +class Solution { +public: + virtual void run() = 0; +}; + +#endif //AOC2019_SOLUTION_H diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..1195ed2 --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,21 @@ +#include "Day1Solution.h" +#include "Day2Solution.h" +#include "Solution.h" +#include +#include + +int main() +{ + std::map solutions = { + { 1, new Day1Solution }, + { 2, new Day2Solution } + }; + + for (const auto& [day, solution] : solutions) { + std::cout << "Day " << day << std::endl; + solution->run(); + std::cout << "--------------------------------------------------------------------" << std::endl; + } + + return 0; +} \ No newline at end of file