Created
December 2, 2019 16:49
-
-
Save invakid404/d0b3223955695ce78a163118f751f182 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) | |
namespace { | |
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) -> void { | |
auto a = data[idx + 1]; | |
auto b = data[idx + 2]; | |
auto dest = data[idx + 3]; | |
data[dest] = data[a] + data[b]; | |
} | |
auto handle_mult(std::vector<int>& data, int idx) -> void { | |
auto a = data[idx + 1]; | |
auto b = data[idx + 2]; | |
auto dest = data[idx + 3]; | |
data[dest] = data[a] * data[b]; | |
} | |
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]; | |
auto flag = false; | |
switch(curr) { | |
case 1: | |
handle_add(data, i); | |
break; | |
case 2: | |
handle_mult(data, i); | |
break; | |
case 99: | |
flag = true; | |
break; | |
default: | |
std::cerr << "How did this happen?" << ENDL; | |
} | |
if(flag) { | |
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() { | |
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