A bit late to the party, got my c++17 code working however
#include <fstream>
#include <vector>
#include <string>
#include <optional>
#include <optional>
#include <iostream>
#include <cmath>
#include <sstream>
std::optional<std::vector<unsignedint>>ReadFile(conststd::string&filename){std::ifstreamreader(filename.c_str());if(reader){std::stringdata;reader>>data;std::stringstreamss(data);std::vector<unsignedint>out;for(unsignedintval;ss>>val;){out.emplace_back(val);if(ss.peek()==',')ss.ignore();}returnout;}return{};}unsignedintCalculate(unsignedintfirst,unsignedintsecond,std::vector<unsignedint>data){// Set initial valuesdata[1]=first;data[2]=second;for(unsignedinti=0;i<data.size();i+=4){constunsignedintFIRST=data[i+1];constunsignedintSECOND=data[i+2];constunsignedintLOCATION=data[i+3];switch(data[i]){// Additioncase1:data[LOCATION]=data[FIRST]+data[SECOND];break;// Multiplicationcase2:data[LOCATION]=data[FIRST]*data[SECOND];break;// Terminatecase99:returndata[0];break;default:break;}}returndata[0];}intmain(){if(autodata=ReadFile("puzzle_input");data.has_value()){/* Part one */unsignedintans1=Calculate(12,2,data.value());std::cout<<"The answer to part_1 is "<<ans1<<"\n";/* Part 2 */// Reset data setdata.value()=ReadFile("puzzle_input").value();constunsignedintGOAL=19690720;// 100*100 is enough iterationsfor(unsignedintnoun=0;noun<100;noun++)for(unsignedintverb=0;verb<100;verb++)if(Calculate(noun,verb,data.value())==GOAL)std::cout<<"\nThe answer to part_2 is "<<100*noun+verb<<"\n";}elsestd::cerr<<"Failed to read puzzle_input!\n";return0;}
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
A bit late to the party, got my c++17 code working however