49 lines
1.5 KiB
C
49 lines
1.5 KiB
C
#include <stdio.h>
|
|
#include <string.h>
|
|
|
|
#include "../../lib/aoc.h"
|
|
|
|
int volume(int length, int width, int height) {
|
|
return length * width * height;
|
|
}
|
|
|
|
int ribbon_length(int length, int width, int height) {
|
|
int first_perimeter = (length + width) * 2;
|
|
int second_perimeter = (length + height) * 2;
|
|
int third_perimeter = (height + width) * 2;
|
|
int smallest_perimeter = first_perimeter;
|
|
if (second_perimeter < smallest_perimeter) smallest_perimeter = second_perimeter;
|
|
if (third_perimeter < smallest_perimeter) smallest_perimeter = third_perimeter;
|
|
return smallest_perimeter + volume(length, width, height);
|
|
}
|
|
|
|
int surface_area(int length, int width, int height) {
|
|
int first_side = length * width * 2;
|
|
int second_side = length * height * 2;
|
|
int third_side = height * width * 2;
|
|
int smallest_side = first_side;
|
|
if (second_side < smallest_side) smallest_side = second_side;
|
|
if (third_side < smallest_side) smallest_side = third_side;
|
|
return first_side + second_side + third_side + (smallest_side / 2);
|
|
}
|
|
|
|
int main() {
|
|
char *input = aoc_read_input();
|
|
int paper = 0;
|
|
int ribbon = 0;
|
|
char *tok = strtok(input, "x");
|
|
while (tok != NULL) {
|
|
int l = atoi(tok);
|
|
tok = strtok(NULL, "x");
|
|
int w = atoi(tok);
|
|
tok = strtok(NULL, "\n");
|
|
int h = atoi(tok);
|
|
paper += surface_area(l, w, h);
|
|
ribbon += ribbon_length(l, w, h);
|
|
tok = strtok(NULL, "x");
|
|
}
|
|
printf("Part 1: %d\n", paper);
|
|
printf("Part 1: %d\n", ribbon);
|
|
free(input);
|
|
}
|