aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore1
-rw-r--r--day1.c9
-rw-r--r--day2.c26
3 files changed, 32 insertions, 4 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..cba7efc
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+a.out
diff --git a/day1.c b/day1.c
index 365e525..dbd188d 100644
--- a/day1.c
+++ b/day1.c
@@ -1,8 +1,9 @@
#include <stdio.h>
int main() {
- int ch, floor = 0;
+ int ch, floor = 0, pos = 0, first_basement = 1;
while ((ch = getchar()) != EOF) {
+ pos += 1;
switch(ch) {
case '(':
floor += 1;
@@ -10,7 +11,11 @@ int main() {
case ')':
floor -= 1;
}
+ if (floor == -1 && first_basement) {
+ printf("In the basement at position: %d\n", pos);
+ first_basement = 0;
+ }
}
- printf("%d\n", floor);
+ printf("Final floor: %d\n", floor);
return 0;
}
diff --git a/day2.c b/day2.c
index de96175..d02526f 100644
--- a/day2.c
+++ b/day2.c
@@ -18,17 +18,39 @@ void calcsides(int length, int width, int height, int* side1, int* side2, int* s
(*side3) = height * length;
}
+void calcperi(int edge1, int edge2, int* peri) {
+ (*peri) = 2 * edge1 + 2 * edge2;
+}
+
+void calcarea(int side1, int side2, int side3, int* area) {
+ (*area) = 2*side1 + 2*side2 + 2*side3 + min3(side1, side2, side3);
+}
+
+void calcribbon(int length, int width, int height, int* ribbon) {
+ int bow, wrap;
+ int side1, side2, side3;
+ calcperi(length, width, &side1);
+ calcperi(width, height, &side2);
+ calcperi(height, length, &side3);
+ wrap = min3(side1, side2, side3);
+ bow = length * width * height;
+ (*ribbon) = wrap + bow;
+}
+
int main() {
int length, width, height;
int side1, side2, side3;
int area, totalarea = 0;
+ int ribbon, totalribbon = 0;
while (readsides(&length, &width, &height) > 0) {
calcsides(length, width, height, &side1, &side2, &side3);
- area = 2*side1 + 2*side2 + 2*side3 + min3(side1, side2, side3);
+ calcarea(side1, side2, side3, &area);
+ calcribbon(length, width, height, &ribbon);
totalarea += area;
+ totalribbon += ribbon;
}
- printf("%d\n", totalarea);
+ printf("Total area: %d\nTotal ribbon: %d\n", totalarea, totalribbon);
return 0;
}