fix d2p1 to be shorter
This commit is contained in:
		
							
								
								
									
										35
									
								
								02/part1.c
									
									
									
									
									
								
							
							
						
						
									
										35
									
								
								02/part1.c
									
									
									
									
									
								
							| @@ -1,27 +1,23 @@ | |||||||
| #include <stdio.h> | #include <stdio.h> | ||||||
| #include <stdlib.h> | #include <stdlib.h> | ||||||
| #define MAX_LENGTH 165 | #define MAX_LENGTH 165 | ||||||
| #define RED 1 |  | ||||||
| #define GREEN 2 |  | ||||||
| #define BLUE 3 |  | ||||||
|  |  | ||||||
| int strtocolor(char* c, char** end) { | char* skip_color(char* c) { | ||||||
|     if (c[0] == 'r' && c[1] == 'e' && c[2] == 'd') { |     switch (*c) { | ||||||
|         *end = c + 3; |         case 'r': | ||||||
|         return RED; |             return c + 3; | ||||||
|     } else if (c[0] == 'g' && c[1] == 'r' && c[2] == 'e' && c[3] == 'e' && c[4] == 'n') { |         case 'g': | ||||||
|         *end = c + 5; |             return c + 5; | ||||||
|         return GREEN; |         case 'b': | ||||||
|     } else if (c[0] == 'b' && c[1] == 'l' && c[2] == 'u' && c[3] == 'e') { |             return c + 4; | ||||||
|         *end = c + 4; |  | ||||||
|         return BLUE; |  | ||||||
|     } |     } | ||||||
|     abort(); |     abort(); | ||||||
| } | } | ||||||
|  |  | ||||||
|  |  | ||||||
| int main(void) { | int main(void) { | ||||||
|     char buf[MAX_LENGTH]; |     char buf[MAX_LENGTH]; | ||||||
|     int game_num, color_num, color, sum; |     int game_num, cubes, sum; | ||||||
|  |  | ||||||
|     sum = 0; |     sum = 0; | ||||||
|     while (fgets(buf, MAX_LENGTH, stdin)) { |     while (fgets(buf, MAX_LENGTH, stdin)) { | ||||||
| @@ -29,13 +25,14 @@ int main(void) { | |||||||
|         game_num = strtol(c + 5, &c, 10);  // +5 skips the "Game " |         game_num = strtol(c + 5, &c, 10);  // +5 skips the "Game " | ||||||
|  |  | ||||||
|         while (*c != '\n') { |         while (*c != '\n') { | ||||||
|             color_num = strtol(c + 2, &c, 10);  // +2 skips ": ", ", ", or "; " |             cubes = strtol(c + 2, &c, 10);  // +2 skips ": ", ", ", or "; " | ||||||
|             color = strtocolor(c + 1, &c);  // +1 skips the space |             c++;  // +1 skips the space | ||||||
|             if ((color == RED && color_num > 12) || |             if ((*c == 'r' && cubes > 12) || | ||||||
|                 (color == GREEN && color_num > 13) || |                 (*c == 'g' && cubes > 13) || | ||||||
|                 (color == BLUE && color_num > 14)) { |                 (*c == 'b' && cubes > 14)) { | ||||||
|                 game_num = 0; |                 game_num = 0; | ||||||
|             } |             } | ||||||
|  |             c = skip_color(c); | ||||||
|         } |         } | ||||||
|         sum += game_num; |         sum += game_num; | ||||||
|     } |     } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Duy Truong
					Duy Truong