Reorganized project structure

This commit is contained in:
Brandon Scott 2019-12-14 17:05:43 -06:00
parent b3b6a43c9e
commit 8f391a3cff
10 changed files with 120 additions and 73 deletions

12
.clang-format Normal file
View File

@ -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

7
CMakeLists.txt Normal file
View File

@ -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)

View File

@ -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)/*

View File

View File

@ -1,10 +1,10 @@
#include <iostream> #include "Day1Solution.h"
#include <fstream>
#include <string>
#include <vector>
#include <cmath> #include <cmath>
#include <fstream>
#include <iostream>
#include <string>
int main() void Day1Solution::run()
{ {
std::string line; std::string line;
std::ifstream input_file("../input/day1.txt"); std::ifstream input_file("../input/day1.txt");
@ -12,16 +12,13 @@ int main()
auto part1_sum = 0; auto part1_sum = 0;
auto part2_sum = 0; auto part2_sum = 0;
if (input_file.is_open()) if (input_file.is_open()) {
{ while (getline(input_file, line)) {
while (getline(input_file, line))
{
auto val = std::stoi(line); auto val = std::stoi(line);
auto calculated_value = std::floor(val / 3) - 2; auto calculated_value = std::floor(val / 3) - 2;
part1_sum += calculated_value; part1_sum += calculated_value;
while (calculated_value > 0) while (calculated_value > 0) {
{
part2_sum += calculated_value; part2_sum += calculated_value;
calculated_value = std::floor(calculated_value / 3) - 2; calculated_value = std::floor(calculated_value / 3) - 2;
} }
@ -29,8 +26,6 @@ int main()
input_file.close(); input_file.close();
} }
std::cout << "Part 1 Sum: " << part1_sum << std::endl; std::cout << "Part 1 Answer: " << part1_sum << std::endl;
std::cout << "Part 2 Sum: " << part2_sum << std::endl; std::cout << "Part 2 Answer: " << part2_sum << std::endl;
return 0;
} }

11
src/Day1Solution.h Normal file
View File

@ -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

View File

@ -1,23 +1,40 @@
#include <iostream> #include "Day2Solution.h"
#include <fstream> #include <fstream>
#include <string> #include <iostream>
#include <vector>
#include <cmath>
int run_program(std::vector<int> program, int noun, int verb) void Day2Solution::run()
{ {
int program_counter = 0; std::string line;
bool running = true; std::ifstream input_file("../input/day2.txt");
std::vector<int> 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<int> program, int noun, int verb)
{
auto program_counter = 0;
auto running = true;
program[1] = noun; program[1] = noun;
program[2] = verb; program[2] = verb;
while (running) while (running) {
{
auto opcode = program[program_counter++]; auto opcode = program[program_counter++];
auto val1 = program[program[program_counter++]]; auto val1 = program[program[program_counter++]];
auto val2 = program[program[program_counter++]]; auto val2 = program[program[program_counter++]];
switch (opcode) switch (opcode) {
{
case 1: // addition case 1: // addition
program[program[program_counter++]] = val1 + val2; program[program[program_counter++]] = val1 + val2;
break; break;
@ -33,43 +50,15 @@ int run_program(std::vector<int> program, int noun, int verb)
return program[0]; return program[0];
} }
int search_program(std::vector<int> program, int expected_value) int Day2Solution::search_program(std::vector<int> 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); auto value = run_program(program, noun, verb);
if (value == expected_value) if (value == expected_value) {
{
return 100 * noun + verb; return 100 * noun + verb;
} }
} }
} }
return -1; return -1;
} }
int main()
{
std::string line;
std::ifstream input_file("../input/day2.txt");
std::vector<int> 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;
}

16
src/Day2Solution.h Normal file
View File

@ -0,0 +1,16 @@
#ifndef AOC2019_DAY2SOLUTION_H
#define AOC2019_DAY2SOLUTION_H
#include "Solution.h"
#include <vector>
class Day2Solution : public Solution {
public:
void run() override;
private:
int run_program(std::vector<int> program, int noun, int verb);
int search_program(std::vector<int> program, int expected_value);
};
#endif //AOC2019_DAY2SOLUTION_H

11
src/Solution.h Normal file
View File

@ -0,0 +1,11 @@
#ifndef AOC2019_SOLUTION_H
#define AOC2019_SOLUTION_H
#include <string>
class Solution {
public:
virtual void run() = 0;
};
#endif //AOC2019_SOLUTION_H

21
src/main.cpp Normal file
View File

@ -0,0 +1,21 @@
#include "Day1Solution.h"
#include "Day2Solution.h"
#include "Solution.h"
#include <iostream>
#include <map>
int main()
{
std::map<int, Solution*> 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;
}