adventofcode/2015/day02/day02.c

67 lines
1.2 KiB
C
Raw Normal View History

2018-03-15 17:00:30 +00:00
#include <stdio.h>
2018-03-16 16:06:54 +00:00
int getBoxSidesArea(int l, int w, int h) {
2018-03-15 17:00:30 +00:00
return (2*l*w + 2*w*h + 2*h*l);
}
2018-03-16 16:06:54 +00:00
int getSmallestSideArea(int l, int w, int h) {
int smallest = (l * w);
if((l * h) < smallest) {
smallest = (l * h);
2018-03-15 17:00:30 +00:00
}
2018-03-16 16:06:54 +00:00
if((w * h) < smallest) {
smallest = (w * h);
2018-03-15 17:00:30 +00:00
}
2018-03-16 16:06:54 +00:00
return smallest;
}
int getSmallestPerimeter(int l, int w, int h) {
int use1, use2;
if(l < w || l < h) {
use1 = l;
if(w < h) {
use2 = w;
} else {
use2 = h;
}
} else {
use1 = w;
use2 = h;
}
return (use1 * 2) + (use2 * 2);
}
int getBoxArea(int l, int w, int h) {
return l * w * h;
2018-03-15 17:00:30 +00:00
}
void part1() {
printf("Part 1\n");
int l, w, h, result = 0;
while(scanf("%dx%dx%d", &l, &w, &h) > 0) {
2018-03-16 16:06:54 +00:00
result += getBoxSidesArea(l, w, h) + getSmallestSideArea(l, w, h);
2018-03-15 17:00:30 +00:00
}
printf("Square Feet Needed: %d\n", result);
}
void part2() {
2018-03-16 16:06:54 +00:00
printf("Part 2\n");
int l, w, h, result = 0;
while(scanf("%dx%dx%d", &l, &w, &h) > 0) {
result += getSmallestPerimeter(l, w, h) + getBoxArea(l, w, h);
}
printf("Ribbon Length Needed: %d\n", result);
2018-03-15 17:00:30 +00:00
}
int main(int argc, char **argv) {
int doPart = 1;
if(argc > 1) { doPart = argv[1][0] - '0'; }
if(doPart == 1) {
part1();
} else {
part2();
}
return 0;
}