aboutsummaryrefslogtreecommitdiff
path: root/2015/02/solve.c
diff options
context:
space:
mode:
authorMarvin Borner2021-11-30 21:53:46 +0100
committerMarvin Borner2021-11-30 21:53:46 +0100
commit3aa641542918765f6ceb039cfa64ff6f58597eb2 (patch)
treeb665282b581d22c4189c57286ff8625a708f60a9 /2015/02/solve.c
parentf0566aee824569b8cd20186b40c9b9be53f9261a (diff)
Hype for tomorrow
Diffstat (limited to '2015/02/solve.c')
-rw-r--r--2015/02/solve.c65
1 files changed, 65 insertions, 0 deletions
diff --git a/2015/02/solve.c b/2015/02/solve.c
new file mode 100644
index 0000000..e1030a8
--- /dev/null
+++ b/2015/02/solve.c
@@ -0,0 +1,65 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+
+static int part_one(FILE *fp)
+{
+ int res = 0;
+
+ char *line = NULL;
+ size_t len = 0;
+ while (getline(&line, &len, fp) != EOF) {
+ int w, l, h;
+ sscanf(line, "%dx%dx%d", &w, &l, &h);
+ res += 2 * l * w + 2 * w * h + 2 * h * l;
+
+ int a = w * l, b = l * h, c = w * h;
+ res += (a <= b && a <= c) ? a : (b <= c && b <= a) ? b : (c <= a && c <= b) ? c : 0;
+ }
+
+ if (line)
+ free(line);
+
+ return res;
+}
+
+static int part_two(FILE *fp)
+{
+ int res = 0;
+
+ char *line = NULL;
+ size_t len = 0;
+ while (getline(&line, &len, fp) != EOF) {
+ int w, l, h;
+ sscanf(line, "%dx%dx%d", &w, &l, &h);
+
+ int a = 2 * w + 2 * l, b = 2 * l + 2 * h, c = 2 * w + 2 * h;
+ res += (a <= b && a <= c) ? a : (b <= c && b <= a) ? b : (c <= a && c <= b) ? c : 0;
+ res += w * h * l;
+ }
+
+ if (line)
+ free(line);
+
+ return res;
+}
+
+int main(int argc, char *argv[])
+{
+ (void)argc;
+ (void)argv;
+
+ FILE *fp = fopen("input", "r");
+ if (!fp)
+ exit(EXIT_FAILURE);
+
+ clock_t tic = clock();
+ printf("%d\n", part_one(fp));
+ rewind(fp);
+ printf("%d\n", part_two(fp));
+ clock_t toc = clock();
+ printf("TIME: %f seconds\n", (double)(toc - tic) / CLOCKS_PER_SEC);
+
+ fclose(fp);
+ return 0;
+}