Created
December 10, 2016 16:21
-
-
Save phesch/224d3845220a1e05b657a1e629a40e3a to your computer and use it in GitHub Desktop.
A C++ solution for the Advent of Code 2016 Day 10 puzzle.
This file contains 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
// AOC-10.cpp : Defines the entry point for the console application. | |
// | |
#include "stdafx.h" | |
#include <iostream> | |
#include <fstream> | |
#include <ctype.h> | |
#include <string> | |
#include <vector> | |
#include <ctime> | |
struct BotInstruction | |
{ | |
int bot; | |
int low; | |
int high; | |
bool firstout = false; | |
bool secondout = false; | |
}; | |
struct ValueInstruction | |
{ | |
int chip; | |
int bot; | |
}; | |
struct Bot | |
{ | |
int left = 0; | |
int right = 0; | |
BotInstruction* inst; | |
}; | |
void readInput(std::string input); | |
int getNum(char*& p); | |
void incChar(char*& p, int count); | |
bool isOut(std::string* str); | |
void assignChip(int bot, int chip); | |
bool Round(int& ans); | |
std::string input; | |
std::string botstr = "bot"; | |
std::string valstr = "value"; | |
std::string outstr = "output"; | |
std::vector<BotInstruction*> botinst; | |
std::vector<ValueInstruction*> valinst; | |
Bot* bots[210]; | |
int rounds = 0; | |
int main() | |
{ | |
std::clock_t start; | |
double duration; | |
start = std::clock(); | |
readInput("Input.txt"); | |
/*for(size_t i = 0; i < botinst.size(); i++) | |
{ | |
std::cout << "BotInstruction " << i + 1 << ": Bot = " << botinst[i]->bot << "; Low = " << botinst[i]->low << "; High = " << botinst[i]->high << "; FO & SO = " << botinst[i]->firstout << botinst[i]->secondout << ";\n"; | |
} | |
for(size_t i = 0; i < valinst.size(); i++) | |
{ | |
std::cout << "ValueInstruction " << i + 1 << ": Chip = " << valinst[i]->chip << "; Bot = " << valinst[i]->bot << ";\n"; | |
}*/ | |
for(size_t i = 0; i < 210; i++) | |
{ | |
bots[i] = new Bot; | |
for(size_t j = 0; j < botinst.size(); j++) | |
{ | |
if(botinst[j]->bot == i) | |
{ | |
bots[i]->inst = botinst[j]; | |
} | |
} | |
} | |
for(size_t i = 0; i < valinst.size(); i++) | |
{ | |
assignChip(valinst[i]->bot, valinst[i]->chip); | |
} | |
bool found = false; | |
int ansBot = 0; | |
while(!found) | |
{ | |
found = Round(ansBot); | |
} | |
std::cout << ansBot << std::endl; | |
duration = (std::clock() - start) / (double) CLOCKS_PER_SEC; | |
std::cout << "Duration: " << duration << '\n'; | |
std::cin.get(); | |
return 0; | |
} | |
bool Round(int& ans) | |
{ | |
for(size_t i = 0; i < 210; i++) | |
{ | |
if(bots[i]->left && bots[i]->right) | |
{ | |
if((bots[i]->left == 61 && bots[i]->right == 17) || (bots[i]->right == 61 && bots[i]->left == 17)) | |
{ | |
ans = i; | |
return true; | |
} | |
if(bots[i]->inst->firstout) | |
{ | |
if(!bots[i]->inst->secondout) | |
{ | |
(bots[i]->left < bots[i]->right) ? | |
assignChip(bots[i]->inst->high, bots[i]->right) : | |
assignChip(bots[i]->inst->high, bots[i]->left); | |
} | |
} | |
else if(bots[i]->inst->secondout) | |
{ | |
(bots[i]->left < bots[i]->right) ? | |
assignChip(bots[i]->inst->low, bots[i]->left) : | |
assignChip(bots[i]->inst->low, bots[i]->right); | |
} | |
else | |
{ | |
(bots[i]->left < bots[i]->right) ? | |
(assignChip(bots[i]->inst->low, bots[i]->left), assignChip(bots[i]->inst->high, bots[i]->right)) : | |
(assignChip(bots[i]->inst->high, bots[i]->left), assignChip(bots[i]->inst->low, bots[i]->right)); | |
} | |
bots[i]->left = 0; | |
bots[i]->right = 0; | |
} | |
} | |
std::cout << "Round " << ++rounds << std::endl; | |
return false; | |
} | |
void readInput(const std::string path) | |
{ | |
std::ifstream stream; | |
stream.open(path); | |
for(std::string line; std::getline(stream, line); ) | |
{ | |
if(line.compare(0, botstr.length(), botstr) == 0) | |
{ | |
BotInstruction* inst = new BotInstruction; | |
char* ch = &line[4]; | |
inst->bot = getNum(ch); | |
incChar(ch, 14); | |
inst->firstout = isOut(&line.substr((ch - &line[0])/sizeof(char), 6)); | |
incChar(ch, INT32_MAX); | |
inst->low = getNum(ch); | |
incChar(ch, 13); | |
inst->secondout = isOut(&line.substr((ch - &line[0]) / sizeof(char), 6)); | |
incChar(ch, INT32_MAX); | |
inst->high = getNum(ch); | |
botinst.push_back(inst); | |
} | |
else if(line.compare(0, valstr.length(), valstr) == 0) | |
{ | |
ValueInstruction* inst = new ValueInstruction; | |
char* ch = &line[6]; | |
inst->chip = getNum(ch); | |
incChar(ch, INT32_MAX); | |
inst->bot = getNum(ch); | |
valinst.push_back(inst); | |
} | |
} | |
} | |
int getNum(char*& p) | |
{ | |
std::string num; | |
for(size_t i = 0; isdigit(*p); i++) | |
{ | |
num += *p; | |
p++; | |
} | |
return std::atoi(num.c_str()); | |
} | |
void incChar(char*& p, int count) | |
{ | |
for(size_t i = 0; !isdigit(*p) && i < count; i++) | |
{ | |
p++; | |
} | |
} | |
bool isOut(std::string* str) | |
{ | |
return str->compare(0, outstr.length(), outstr) == 0; | |
} | |
void assignChip(int bot, int chip) | |
{ | |
if(bots[bot]->left) | |
{ | |
bots[bot]->right = chip; | |
} | |
else | |
{ | |
bots[bot]->left = chip; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment