Skip to content

Commit 0e77c00

Browse files
2018 day 13
1 parent c9a17ed commit 0e77c00

File tree

11 files changed

+339
-106
lines changed

11 files changed

+339
-106
lines changed

2015-c/02-i-was-told-there-would-be-no-math/main.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ static inline char *parse(int *dst, char *s) {
1414
return s;
1515
}
1616

17-
int compare_ints(const void *ptr_a, const void *ptr_b) {
17+
static int compare_ints(const void *ptr_a, const void *ptr_b) {
1818
const int a = *(const int *)ptr_a;
1919
const int b = *(const int *)ptr_b;
2020
return (a > b) - (a < b);

2015-c/03-perfectly-spherical-houses-in-a-vacuum/main.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ typedef struct {
99
int8_t y;
1010
} pos_t;
1111

12-
int solve(const char *s, uint8_t nworkers) {
12+
static int solve(const char *s, const uint8_t nworkers) {
1313
int8_t dx, dy;
1414
uint16_t tally[256 * 256] = {0};
1515
uint8_t w = 0;

2015-c/17-no-such-thing-as-too-much/main.c

+4-10
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,10 @@ unsigned int permute(unsigned int sizes[],unsigned int flags, unsigned int i, un
4141
permute(sizes, flags | 1 << (n - i), i + 1, n);
4242
}
4343

44-
int qsort_compare_int_desc(const void *ptra, const void *ptrb) {
45-
unsigned int a = *(unsigned int *)ptra;
46-
unsigned int b = *(unsigned int *)ptrb;
47-
if (b < a) {
48-
return -1;
49-
}
50-
if (b > a) {
51-
return 1;
52-
}
53-
return 0;
44+
static int qsort_compare_int_desc(const void *ptra, const void *ptrb) {
45+
const unsigned int a = *(unsigned int *)ptra;
46+
const unsigned int b = *(unsigned int *)ptrb;
47+
return (b > a) - (b < a);
5448
}
5549

5650
unsigned int parse(const char *s,unsigned int *dest) {

2015-c/24-it-hangs-in-the-balance/main.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ static unsigned int parse(const char *s, int weights[]) {
1515

1616
// qsort callback
1717
static int cmp_ints(const void *p1, const void *p2) {
18-
int a = *(int *)p1;
19-
int b = *(int *)p2;
18+
const int a = *(int *)p1;
19+
const int b = *(int *)p2;
2020
return (a > b) + (a < b);
2121
}
2222

2017-c/02-corruption-checksum/main.c

+3-7
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,11 @@
44

55
static int compare_ints(const void* a, const void* b)
66
{
7-
int arg1 = *(const int*)a;
8-
int arg2 = *(const int*)b;
9-
10-
if (arg1 < arg2) return -1;
11-
if (arg1 > arg2) return 1;
12-
return 0;
7+
const int ia = *(const int*)a;
8+
const int ib = *(const int*)b;
9+
return (ia > ib) - (ia < ib);
1310
}
1411

15-
1612
int main(void) {
1713
clock_t start_t, end_t;
1814
start_t = clock();
+67-63
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,90 @@
1-
#include <stdlib.h>
21
#include <stdio.h>
3-
#include <time.h>
2+
#include <stdlib.h>
43
#include <string.h>
4+
#include <time.h>
55

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);
1515
}
1616

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+
}
2423
}
25-
return 1;
24+
}
25+
return 1;
2626
}
2727

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);
3632
}
3733

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];
4238

43-
while (*s != '\0') {
44-
int nwords = 0;
39+
// go over every line
40+
while (*s != '\0') {
41+
unsigned int nwords = 0;
4542

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-
}
5443

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;
5648

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';
6253

63-
if (*s == '\n') s++;
54+
if (*s == ' ') {
55+
s++;
56+
}
6457
}
6558

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;
6872
}
6973

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();
7377

74-
char input[1024 * 64];
75-
read_input_file(input, "input.txt");
78+
char input[1024 * 64];
79+
read_input_file(input, "input.txt");
7680

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);
8185

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;
8690
}

2018-c/.clangd

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
CompileFlags:
2-
Add: [-std=c11, -Wall, -Wextra, -Wpedantic, -Wundef, -Winline, -Wimplicit-fallthrough, -Wformat=2, -Wconversion ]
2+
Add: [-std=c17, -Wall, -Wextra, -Wpedantic, -Wundef, -Winline, -Wimplicit-fallthrough, -Wformat=2, -Wconversion ]

2018-c/04.c

+9-10
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ struct guard {
2222
};
2323

2424
static int log_entry_compare(const void *p1, const void *p2) {
25-
struct log_entry *a = (struct log_entry *)p1;
26-
struct log_entry *b = (struct log_entry *)p2;
25+
const struct log_entry *a = (struct log_entry *)p1;
26+
const struct log_entry *b = (struct log_entry *)p2;
2727

2828
if (a->year < b->year) {
2929
return -1;
@@ -54,7 +54,7 @@ static int log_entry_compare(const void *p1, const void *p2) {
5454
return 1;
5555
}
5656

57-
int parse(struct log_entry *log_entries, char *s) {
57+
static int parse(struct log_entry *log_entries, const char *s) {
5858
int nentries = 0;
5959
while (*s != 0x0) {
6060
struct log_entry *e = &log_entries[nentries++];
@@ -88,7 +88,7 @@ int parse(struct log_entry *log_entries, char *s) {
8888
return nentries;
8989
}
9090

91-
struct guard *guard_by_id(struct guard *guards, int nguards, int id) {
91+
static struct guard *guard_by_id(struct guard *guards, const int nguards, const int id) {
9292
for (int i = 0; i < nguards; i++) {
9393
if (guards[i].id == id) {
9494
return &guards[i];
@@ -98,8 +98,8 @@ struct guard *guard_by_id(struct guard *guards, int nguards, int id) {
9898
return NULL;
9999
}
100100

101-
int log_entries_into_guards(struct guard *guards, struct log_entry *entries,
102-
int nentries) {
101+
static int log_entries_into_guards(struct guard *guards, struct log_entry *entries,
102+
const int nentries) {
103103
int nguards = 0;
104104
struct guard *guard = NULL;
105105

@@ -129,7 +129,7 @@ int log_entries_into_guards(struct guard *guards, struct log_entry *entries,
129129
return nguards;
130130
}
131131

132-
int solve_pt1(struct guard *guards, int nguards) {
132+
static int solve_pt1(struct guard *guards, const int nguards) {
133133
struct guard *guard = &guards[0];
134134

135135
for (int i = 0; i < nguards; i++) {
@@ -149,7 +149,7 @@ int solve_pt1(struct guard *guards, int nguards) {
149149
return answer_pt1;
150150
}
151151

152-
int solve_pt2(struct guard *guards, int nguards) {
152+
static int solve_pt2(struct guard *guards, const int nguards) {
153153
struct guard *guard = &guards[0];
154154
int max = 0;
155155

@@ -170,8 +170,7 @@ int main(void) {
170170
char input[1024 * 64] = "";
171171
read_input_file(input, 1024 * 64, "04.txt");
172172

173-
struct log_entry *log_entries =
174-
malloc_or_die(2048 * sizeof(struct log_entry));
173+
struct log_entry *log_entries = malloc_or_die(2048 * sizeof(struct log_entry));
175174
int nentries = parse(log_entries, input);
176175

177176
struct guard *guards = malloc_or_die(512 * sizeof(struct guard));

2018-c/07.c

+7-10
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,13 @@ static struct step *step_next(struct step **steps, size_t nsteps) {
4949
return NULL;
5050
}
5151

52-
int step_compare(const void *p1, const void *p2) {
53-
struct step *a = *(struct step **)p1;
54-
struct step *b = *(struct step **)p2;
55-
if (a->name == b->name) {
56-
return 0;
57-
}
58-
return a->name < b->name ? -1 : 1;
52+
static int step_compare(const void *p1, const void *p2) {
53+
const struct step *a = *(struct step **)p1;
54+
const struct step *b = *(struct step **)p2;
55+
return (a->name > b->name) - (a->name < b->name);
5956
}
6057

61-
void pt1(char *buf, struct step **steps, size_t nsteps) {
58+
static void pt1(char *buf, struct step **steps, const size_t nsteps) {
6259
char *o = buf;
6360
while (1) {
6461
struct step *cur = step_next(steps, nsteps);
@@ -74,8 +71,8 @@ void pt1(char *buf, struct step **steps, size_t nsteps) {
7471
steps[i]->done = 0;
7572
}
7673

77-
int pt2(struct step **steps, size_t nsteps) {
78-
size_t nworkers = 5;
74+
static int pt2(struct step **steps, const size_t nsteps) {
75+
const size_t nworkers = 5;
7976
int delay_s = 60;
8077
int worker_finish_times[nworkers];
8178
struct step *worker_tasks[nworkers];

0 commit comments

Comments
 (0)