|
1 |
| -#include <stdlib.h> |
2 | 1 | #include <stdio.h>
|
3 |
| -#include <time.h> |
| 2 | +#include <stdlib.h> |
4 | 3 | #include <string.h>
|
| 4 | +#include <time.h> |
5 | 5 |
|
6 |
| -void read_input_file(char *dest, char *file) { |
7 |
| - FILE *fp = fopen(file, "r"); |
8 |
| - if (!fp) { |
9 |
| - fprintf(stderr, "error reading input.txt"); |
10 |
| - exit(EXIT_FAILURE); |
11 |
| - } |
12 |
| - size_t nread = fread(dest, 1, 64*1024, fp); |
13 |
| - dest[nread] = '\0'; |
14 |
| - fclose(fp); |
| 6 | +static void read_input_file(char *dest, const char *file) { |
| 7 | + FILE *fp = fopen(file, "r"); |
| 8 | + if (!fp) { |
| 9 | + fprintf(stderr, "error reading input.txt"); |
| 10 | + exit(EXIT_FAILURE); |
| 11 | + } |
| 12 | + size_t nread = fread(dest, 1, 64 * 1024, fp); |
| 13 | + dest[nread] = '\0'; |
| 14 | + fclose(fp); |
15 | 15 | }
|
16 | 16 |
|
17 |
| -int validpw(char words[16][32], int nwords) { |
18 |
| - for (int i = 0; i < nwords; i++) { |
19 |
| - for (int j = i+1; j < nwords; j++) { |
20 |
| - if (strcmp(words[i], words[j]) == 0) { |
21 |
| - return 0; |
22 |
| - } |
23 |
| - } |
| 17 | +unsigned int validpw(char words[16][32], const unsigned int nwords) { |
| 18 | + for (unsigned int i = 0; i < nwords; i++) { |
| 19 | + for (unsigned int j = i + 1; j < nwords; j++) { |
| 20 | + if (strcmp(words[i], words[j]) == 0) { |
| 21 | + return 0; |
| 22 | + } |
24 | 23 | }
|
25 |
| - return 1; |
| 24 | + } |
| 25 | + return 1; |
26 | 26 | }
|
27 | 27 |
|
28 |
| -int compare_chars(const void* a, const void* b) |
29 |
| -{ |
30 |
| - int arg1 = *(const char*)a; |
31 |
| - int arg2 = *(const char*)b; |
32 |
| - |
33 |
| - if (arg1 < arg2) return -1; |
34 |
| - if (arg1 > arg2) return 1; |
35 |
| - return 0; |
| 28 | +static int compare_chars(const void *a, const void *b) { |
| 29 | + const char ca = *(const char *)a; |
| 30 | + const char cb = *(const char *)b; |
| 31 | + return (ca > cb) - (ca < cb); |
36 | 32 | }
|
37 | 33 |
|
38 |
| -int solve(char *s) { |
39 |
| - int count = 0; |
40 |
| - int count_pt2 = 0; |
41 |
| - char words[16][32]; |
| 34 | +static unsigned int solve(const char *s) { |
| 35 | + unsigned int count = 0; |
| 36 | + unsigned int count_pt2 = 0; |
| 37 | + char words[16][32]; |
42 | 38 |
|
43 |
| - while (*s != '\0') { |
44 |
| - int nwords = 0; |
| 39 | + // go over every line |
| 40 | + while (*s != '\0') { |
| 41 | + unsigned int nwords = 0; |
45 | 42 |
|
46 |
| - while (*s != '\n' && *s != '\0') { |
47 |
| - char *w = words[nwords++]; |
48 |
| - while (*s != ' ' && *s != '\n') { |
49 |
| - *w++ = *s++; |
50 |
| - } |
51 |
| - *w = '\0'; |
52 |
| - if (*s == ' ') s++; |
53 |
| - } |
54 | 43 |
|
55 |
| - count += validpw(words, nwords); |
| 44 | + // go over every word in line |
| 45 | + while (*s != '\n' && *s != '\0' && nwords < 16) { |
| 46 | + char *w = words[nwords++]; |
| 47 | + unsigned len = 0; |
56 | 48 |
|
57 |
| - // for part 2, sort all words alphabetically |
58 |
| - for (int i = 0; i < nwords; i++) { |
59 |
| - qsort(words[i], strlen(words[i]), sizeof(char), compare_chars); |
60 |
| - } |
61 |
| - count_pt2 += validpw(words, nwords); |
| 49 | + while (*s != ' ' && *s != '\n' && len < 32) { |
| 50 | + w[len++] = *s++; |
| 51 | + } |
| 52 | + w[len] = '\0'; |
62 | 53 |
|
63 |
| - if (*s == '\n') s++; |
| 54 | + if (*s == ' ') { |
| 55 | + s++; |
| 56 | + } |
64 | 57 | }
|
65 | 58 |
|
66 |
| - // return both answers in a single 32-bit int |
67 |
| - return (count_pt2 << 16 ) + count; |
| 59 | + count += validpw(words, nwords); |
| 60 | + |
| 61 | + // for part 2, sort all words alphabetically |
| 62 | + for (unsigned int i = 0; i < nwords; i++) { |
| 63 | + qsort(words[i], strlen(words[i]), sizeof(char), compare_chars); |
| 64 | + } |
| 65 | + count_pt2 += validpw(words, nwords); |
| 66 | + |
| 67 | + s++; |
| 68 | + } |
| 69 | + |
| 70 | + // return both answers in a single 32-bit int |
| 71 | + return (count_pt2 << 16) + count; |
68 | 72 | }
|
69 | 73 |
|
70 |
| -int main() { |
71 |
| - clock_t start_t, end_t; |
72 |
| - start_t = clock(); |
| 74 | +int main(void) { |
| 75 | + clock_t start_t, end_t; |
| 76 | + start_t = clock(); |
73 | 77 |
|
74 |
| - char input[1024 * 64]; |
75 |
| - read_input_file(input, "input.txt"); |
| 78 | + char input[1024 * 64]; |
| 79 | + read_input_file(input, "input.txt"); |
76 | 80 |
|
77 |
| - int answers = solve(input); |
78 |
| - printf("--- Day 4: High-Entropy Passphrases ---\n"); |
79 |
| - printf("Part 1: %d\n", answers & 0xFFFF); |
80 |
| - printf("Part 2: %d\n", answers >> 16); |
| 81 | + unsigned int answers = solve(input); |
| 82 | + printf("--- Day 4: High-Entropy Passphrases ---\n"); |
| 83 | + printf("Part 1: %d\n", answers & 0xFFFF); |
| 84 | + printf("Part 2: %d\n", answers >> 16); |
81 | 85 |
|
82 |
| - end_t = clock(); |
83 |
| - double total_t = (double)(end_t - start_t) / CLOCKS_PER_SEC * 1000; |
84 |
| - printf("Time: %.2fms\n", total_t); |
85 |
| - return EXIT_SUCCESS; |
| 86 | + end_t = clock(); |
| 87 | + double total_t = (double)(end_t - start_t) / CLOCKS_PER_SEC * 1000; |
| 88 | + printf("Time: %.2fms\n", total_t); |
| 89 | + return EXIT_SUCCESS; |
86 | 90 | }
|
0 commit comments