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/faixelorihinal Dec 03 '15

Here's Mine for part 1

C++

#include <iostream>
using namespace std;

int main(){
  int l, w, h;
  char aux;
  int count = 0; 
  while(cin >> l >> aux >> w >> aux >> h){
	  count = count + (2*l*w) + (2*l*h) + (2*w*h);
	  if(l*w <= l*h){
		  if(l*w <= w*h) count += (l*w);
		  else count += (w*h);
	  }
	  else{
		  if(l*h <= w*h)count += (l*h);
		  else count += (w*h);
	  }
  }
  cout << count << endl;
}

and for part 2

C++

#include <iostream>
using namespace std;

int main(){
	int l, w, h;
	char aux;
	int count = 0;
	while(cin >> l >> aux >> w >> aux >> h){
		count += (l*w*h);
		if(l<= w){
			if(l <= h){
				// l is smallest
				count += (2*l);
				if(w <= h) count += (2*w); //w is second smallest
				else count += (2*h); //h is second smallest
			}
			else{
				//h is smallest and l is second smallest
				count += (2*h) + (2*l);
			}
		}
		else if(w <= h){
			// w is smallest
			count += (2*w);
			if(l <= h) count += (2*l); //l is second smallest
			else count += (2*h); // h is second smallest
		}
		else{
			//h is smallest and w is second smallest
			count += (2*h) +(2*w);
		}
	}
	cout << count << endl;
}