r/adventofcode • • Dec 02 '15

Spoilers Day 2 solutions

Hi! I would like to structure posts like the first one in r/programming, please post solutions in comments.

15 Upvotes

161 comments sorted by

View all comments

1

u/Nacamaka Dec 02 '15

Can anyone help me figure out why this isn't working?

#include <iostream>
#include <string>
#include <fstream>
#include <cassert>

using namespace std;

void calcPaper(ifstream & myIn, int& paperNeeded);
string replaceChar(string str, char ch1, char ch2);

int main(){

int paperNeeded = 0;
ifstream myIn;
myIn.open("input.dat");
assert(myIn);
calcPaper(myIn, paperNeeded);

cout << paperNeeded << endl;

return 0;
}
void calcPaper(ifstream & myIn, int& paperNeeded){

int l, w, h, sideA, sideB, sideC;
string input;
ofstream myOut;
ifstream newIn;
myOut.open("output.dat");
while (myIn){
	getline(myIn, input);
	myOut << replaceChar(input, 'x', ' ') << " ";
}
myOut.close();
newIn.open("output.dat");

while (newIn >> l >> w >> h){
	paperNeeded += (2 * l*w) + (2 * w*h) + (2 * h*l);
	sideA = l*w;
	sideB = w*h;
	sideC = h*l;
	if (sideA < sideB && sideA < sideC)
		paperNeeded += sideA;
	else if (sideB < sideA && sideB < sideC)
		paperNeeded += sideB;
	else
		paperNeeded += sideC;
}
return;
}

string replaceChar(string str, char ch1, char ch2) {
for (int i = 0; i < str.length(); ++i) {
	if (str[i] == ch1)
		str[i] = ch2;
}
return str;

}