Created
December 2, 2019 18:08
-
-
Save invakid404/ecac536d5fc72a5590b6b9a5b4901b6d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* ===================================================================================== | |
* | |
* Filename: day2.cpp | |
* | |
* Description: Day 2 of Advent of Code solution | |
* | |
* Version: 1.0 | |
* Created: 12/2/2019 6:20:07 PM | |
* Revision: none | |
* Compiler: gcc | |
* | |
* Author: invakid404 | |
* | |
* ===================================================================================== | |
*/ | |
#include <bits/stdc++.h> | |
#define IN_PATH R"(D:\Coding\Problems\adventofcode\in\day2.txt)" | |
#define ENDL '\n'; | |
#define RANGE(i, a, b, s) for(auto i = a; i < b; i += s) | |
typedef std::function<bool(std::vector<int>&, int)> opcode_func; | |
namespace { | |
std::unordered_map<int, opcode_func> opcode_map; | |
auto split(const std::string& s, char d) -> std::vector<std::string> { | |
std::vector<std::string> result; | |
std::stringstream ss(s); | |
std::string curr; | |
while(std::getline(ss, curr, d)) { | |
result.emplace_back(curr); | |
} | |
return result; | |
} | |
auto handle_add(std::vector<int>& data, int idx) -> bool { | |
auto a = data[idx + 1]; | |
auto b = data[idx + 2]; | |
auto dest = data[idx + 3]; | |
data[dest] = data[a] + data[b]; | |
return true; | |
} | |
auto handle_mult(std::vector<int>& data, int idx) -> bool { | |
auto a = data[idx + 1]; | |
auto b = data[idx + 2]; | |
auto dest = data[idx + 3]; | |
data[dest] = data[a] * data[b]; | |
return true; | |
} | |
auto define_opcodes() -> void { | |
opcode_map[1] = handle_add; | |
opcode_map[2] = handle_mult; | |
opcode_map[99] = [](...) { return false; }; | |
} | |
auto part_one(const std::vector<int>& opcodes, int n, int v) -> int { | |
std::vector<int> data = opcodes; | |
data[1] = n; | |
data[2] = v; | |
RANGE(i, 0LL, data.size(), 4) { | |
auto curr = data[i]; | |
if(!opcode_map[curr](data, i)) { | |
break; | |
} | |
} | |
return data[0]; | |
} | |
auto part_two(const std::vector<int>& opcodes, int search) -> int { | |
auto initial = part_one(opcodes, 0, 0); | |
auto increase = part_one(opcodes, 1, 0) - 99 - initial; | |
auto range = search - initial; | |
auto d = std::div(range, increase); | |
return d.quot + d.rem; | |
} | |
} | |
int main() { | |
define_opcodes(); | |
std::ifstream f_in; | |
f_in.open(IN_PATH); | |
if(!f_in) { | |
std::cerr << "Failed to open file!" << ENDL; | |
return 1; | |
} | |
std::string data; | |
std::getline(f_in, data); | |
auto parts = split(data, ','); | |
std::vector<int> opcodes(parts.size()); | |
std::transform(parts.begin(), parts.end(), opcodes.begin(), | |
[](const auto& val) { | |
return std::stoi(val); | |
}); | |
std::cout << part_one(opcodes, 12, 2) << ENDL; | |
std::cout << part_two(opcodes, 19690720) << ENDL; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment