2015-01,02
This commit is contained in:
		
							
								
								
									
										7
									
								
								include/advent_math.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										7
									
								
								include/advent_math.h
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,7 @@ | |||||||
|  | #ifndef ADVENT_MATH_H_ | ||||||
|  | #define ADVENT_MATH_H_ | ||||||
|  |  | ||||||
|  | int int_max(int, int); | ||||||
|  | int int_min(int, int); | ||||||
|  |  | ||||||
|  | #endif // ADVENT_MATH_H_ | ||||||
| @@ -1,10 +1,29 @@ | |||||||
| #include <stdio.h> | #include <stdio.h> | ||||||
| #include <stdlib.h> | #include <stdlib.h> | ||||||
|  | #include <string.h> | ||||||
|  |  | ||||||
| #include "input.h" | #include "input.h" | ||||||
|  |  | ||||||
| void advent2015day01(void) { | void advent2015day01(void) { | ||||||
|   char *input = get_input("input/2015/01"); |   char *input = get_input("input/2015/01"); | ||||||
|   printf("Solution for Day 01 of 2015 is not completed yet\n"); |   char *c = input; | ||||||
|  |   int f = 0, b = 0, s = 1; | ||||||
|  |  | ||||||
|  |   while (*c != '\0') { | ||||||
|  |     if (*c == '(') { | ||||||
|  |       ++f; | ||||||
|  |     } else { | ||||||
|  |       --f; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     if (f == -1 && b == 0) { | ||||||
|  |       b = s; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     ++c; | ||||||
|  |     ++s; | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   printf("%d\n%d\n", f, b); | ||||||
|   free(input); |   free(input); | ||||||
| } | } | ||||||
|   | |||||||
| @@ -1,10 +1,27 @@ | |||||||
| #include <stdio.h> | #include <stdio.h> | ||||||
| #include <stdlib.h> | #include <stdlib.h> | ||||||
|  | #include <string.h> | ||||||
|  |  | ||||||
| #include "input.h" | #include "input.h" | ||||||
|  | #include "advent_math.h" | ||||||
|  |  | ||||||
| void advent2015day02(void) { | void advent2015day02(void) { | ||||||
|   char *input = get_input("input/2015/02"); |   size_t sz = 0; | ||||||
|   printf("Solution for Day 02 of 2015 is not completed yet\n"); |   char **lines = get_lines("input/2015/02", &sz); | ||||||
|   free(input); |   int paper = 0, ribbon = 0; | ||||||
|  |  | ||||||
|  |   for (size_t i = 0; i < sz; i++) { | ||||||
|  |     char *t = strtok(lines[i], "x"); | ||||||
|  |     int w = atoi(t); | ||||||
|  |     t = strtok(NULL, "x"); | ||||||
|  |     int l = atoi(t); | ||||||
|  |     t = strtok(NULL, "x"); | ||||||
|  |     int h = atoi(t); | ||||||
|  |  | ||||||
|  |     paper += 2*l*w + 2*w*h + 2*h*l + int_min(w*l, int_min(l*h, h*w)); | ||||||
|  |     ribbon += l*w*h + int_min(w+w+h+h, int_min(h+h+l+l, l+l+w+w)); | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   printf("%d\n%d\n", paper, ribbon); | ||||||
|  |   del_lines(lines); | ||||||
| } | } | ||||||
|   | |||||||
							
								
								
									
										9
									
								
								src/advent_math.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								src/advent_math.c
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,9 @@ | |||||||
|  | #include "advent_math.h" | ||||||
|  |  | ||||||
|  | int int_max(int a, int b) { | ||||||
|  |     return a > b ? a : b; | ||||||
|  | } | ||||||
|  |  | ||||||
|  | int int_min(int a, int b) { | ||||||
|  |     return a < b ? a : b; | ||||||
|  | } | ||||||
		Reference in New Issue
	
	Block a user