Skip to content
This repository was archived by the owner on Apr 13, 2023. It is now read-only.

Commit 2623e25

Browse files
committed
Add solution for day 5, improve older solutions
1 parent 1ab82de commit 2623e25

File tree

6 files changed

+1007
-34
lines changed

6 files changed

+1007
-34
lines changed

Day-01/c/JohnnyJayJay/Day01.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
/* compile from project root using
2-
cc -Wall -g shared/JohnnyJayJay/aoc.c Day-01/c/JohnnyJayJay/Day01.c -o day01 */
1+
// Read my README in shared/JohnnyJayJay
32

43
#include <stdio.h>
54
#include <stdlib.h>

Day-02/c/JohnnyJayJay/Day02.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ int main(int argc, char** argv) {
3030
int valid_passwords1 = 0;
3131
int valid_passwords2 = 0;
3232
for (int i = 0; i < lines; i++) {
33-
db_entry entry = entries[i];
34-
char* pw = entry.password;
35-
char c = entry.c;
36-
int min = entry.min;
37-
int max = entry.max;
33+
db_entry* entry = &entries[i];
34+
char* pw = entry->password;
35+
char c = entry->c;
36+
int min = entry->min;
37+
int max = entry->max;
3838
int count = charcount(pw, c, 0, strlen(pw));
3939
if (count >= min && count <= max) {
4040
valid_passwords1++;

Day-05/c/JohnnyJayJay/Day05.c

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Read my README in shared/JohnnyJayJay
2+
3+
#include <stdio.h>
4+
#include <stdlib.h>
5+
#include "../../../shared/JohnnyJayJay/aoc.h"
6+
7+
int find_value(char* desc, int length, char lower, char upper) {
8+
int min = 0;
9+
int delta = 1 << length;
10+
for (int i = 0; i < length; i++) {
11+
delta /= 2;
12+
if (desc[i] == upper) {
13+
min += delta;
14+
}
15+
}
16+
return min;
17+
}
18+
19+
int intcmp(const void* one, const void* two) {
20+
int a = *((int*) one);
21+
int b = *((int*) two);
22+
if (a == b) {
23+
return 0;
24+
} else if (a < b) {
25+
return -1;
26+
} else {
27+
return 1;
28+
}
29+
}
30+
31+
int main(int argc, char** argv) {
32+
FILE* file = fopen(argv[1], "r");
33+
int lines = count_lines(file);
34+
char row_desc[8];
35+
char column_desc[4];
36+
int* passes = malloc(sizeof(int) * lines);
37+
for (int i = 0; i < lines; i++) {
38+
fscanf(file, "%7s%3s", row_desc, column_desc);
39+
int row = find_value(row_desc, 7, 'F', 'B');
40+
int column = find_value(column_desc, 3, 'L', 'R');
41+
passes[i] = row * 8 + column;
42+
}
43+
qsort(passes, lines, sizeof(int), &intcmp);
44+
int max_seat_id = passes[lines - 1];
45+
printf("Highest seat id: %d\n", max_seat_id);
46+
int own_seat_id = 0;
47+
for (int i = 0; i < lines - 1; i++) {
48+
int first = passes[i];
49+
int second = passes[i + 1];
50+
if (abs(first - second) == 2) {
51+
own_seat_id = first + 1;
52+
break;
53+
}
54+
}
55+
printf("%d\n", lines);
56+
printf("Own seat id: %d\n", own_seat_id);
57+
}
58+
59+
60+

shared/JohnnyJayJay/aoc.c

+5-1
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,17 @@
33

44
int count_lines(FILE* file) {
55
long pos = ftell(file);
6-
int lines = 1;
6+
int lines = 0;
77
int c;
88
while ((c = fgetc(file)) != EOF) {
99
if (c == '\n') {
1010
lines++;
1111
}
1212
}
13+
fseek(file, -1, SEEK_CUR);
14+
if (fgetc(file) != '\n') {
15+
lines++;
16+
}
1317
fseek(file, pos, SEEK_SET);
1418
return lines;
1519
}

0 commit comments

Comments
 (0)