Skip to content

Commit af29a40

Browse files
authored
Merge pull request devcordde#35 from JohnnyJayJay/master
Add day 6 solution in C
2 parents 53bdb07 + 11118dd commit af29a40

File tree

7 files changed

+2123
-26
lines changed

7 files changed

+2123
-26
lines changed

Day-02/c/JohnnyJayJay/Day02.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ int main(int argc, char** argv) {
1717
FILE* file = fopen(argv[1], "r");
1818
int lines = count_lines(file);
1919

20-
db_entry entries[lines];
20+
db_entry* entries = malloc(sizeof(db_entry) * lines);
2121
for (int i = 0; i < lines; i++) {
2222
int min, max;
2323
char c;

Day-03/c/JohnnyJayJay/Day03.c

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22

33
#include <stdio.h>
44
#include <stdlib.h>
5+
#include <stdint.h>
56
#include "../../../shared/JohnnyJayJay/aoc.h"
67

7-
int traverse(u_int32_t* levels, int lines, int width, int x_step, int y_step) {
8+
int traverse(uint32_t* levels, int lines, int width, int x_step, int y_step) {
89
int x_pos = 0;
910
int tree_count = 0;
1011
for (int i = 0; i < lines; i += y_step) {
11-
u_int32_t level = levels[i];
12-
if ((level >> (width - 1 - x_pos)) & ((u_int32_t) 1)) {
12+
uint32_t level = levels[i];
13+
if ((level >> (width - 1 - x_pos)) & 1) {
1314
tree_count++;
1415
}
1516
x_pos = (x_pos + x_step) % width;
@@ -21,9 +22,9 @@ int main(int argc, char** argv) {
2122
FILE* file = fopen(argv[1], "r");
2223
int lines = count_lines(file);
2324
int width = chars_until(file, '\n', 1);
24-
u_int32_t* levels = malloc(sizeof(u_int32_t) * lines);
25+
uint32_t* levels = malloc(sizeof(uint32_t) * lines);
2526
for (int i = 0; i < lines; i++) {
26-
u_int32_t level = 0;
27+
uint32_t level = 0;
2728
int spot;
2829
while ((spot = fgetc(file)) != '\n' && spot != EOF) {
2930
level <<= 1;
@@ -32,12 +33,12 @@ int main(int argc, char** argv) {
3233
levels[i] = level;
3334
}
3435

35-
u_int64_t first = traverse(levels, lines, width, 3, 1);
36+
uint64_t first = traverse(levels, lines, width, 3, 1);
3637
printf("You encounter %lu trees the first slope.\n", first);
37-
u_int64_t second = traverse(levels, lines, width, 1, 1);
38-
u_int64_t third = traverse(levels, lines, width, 5, 1);
39-
u_int64_t fourth = traverse(levels, lines, width, 7, 1);
40-
u_int64_t fiveth = traverse(levels, lines, width, 1, 2);
38+
uint64_t second = traverse(levels, lines, width, 1, 1);
39+
uint64_t third = traverse(levels, lines, width, 5, 1);
40+
uint64_t fourth = traverse(levels, lines, width, 7, 1);
41+
uint64_t fiveth = traverse(levels, lines, width, 1, 2);
4142
printf("The trees on all slopes multiplied together: %lu\n", first * second * third * fourth * fiveth);
4243

43-
}
44+
}

Day-05/c/JohnnyJayJay/Day05.c

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,7 @@ int find_value(char* desc, int length, char lower, char upper) {
1919
int intcmp(const void* one, const void* two) {
2020
int a = *((int*) one);
2121
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-
}
22+
return a - b;
2923
}
3024

3125
int main(int argc, char** argv) {
@@ -47,7 +41,7 @@ int main(int argc, char** argv) {
4741
for (int i = 0; i < lines - 1; i++) {
4842
int first = passes[i];
4943
int second = passes[i + 1];
50-
if (abs(first - second) == 2) {
44+
if (second - first == 2) {
5145
own_seat_id = first + 1;
5246
break;
5347
}

Day-06/c/JohnnyJayJay/Day06.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Read my README in shared/JohnnyJayJay
2+
3+
#include <stdio.h>
4+
#include <stdint.h>
5+
#include "../../../shared/JohnnyJayJay/aoc.h"
6+
7+
int popcount(uint32_t bits) {
8+
int count;
9+
for (count = 0; bits > 0; bits >>= 1) {
10+
count += bits & 1;
11+
}
12+
return count;
13+
}
14+
15+
int main(int argc, char** argv) {
16+
FILE* file = fopen(argv[1], "r");
17+
int groups = count_blank_lines(file) + 1;
18+
int sum_1 = 0;
19+
int sum_2 = 0;
20+
for (int i = 0; i < groups; i++) {
21+
int next;
22+
uint32_t group_set_1 = 0;
23+
uint32_t group_set_2 = 0xffffffff;
24+
while ((next = fgetc(file)) != '\n' && next != EOF) {
25+
uint32_t person_set = 0;
26+
do {
27+
int pos = next - 'a';
28+
person_set |= 1 << pos;
29+
next = fgetc(file);
30+
} while (next != EOF && next != '\n');
31+
group_set_1 |= person_set;
32+
group_set_2 &= person_set;
33+
}
34+
sum_1 += popcount(group_set_1);
35+
sum_2 += popcount(group_set_2);
36+
}
37+
printf("Sum of the group answer counts (part 1): %d\n", sum_1);
38+
printf("Sum of the group answer counts (part 2): %d\n", sum_2);
39+
}

shared/JohnnyJayJay/aoc.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#include <stdio.h>
2-
31
int count_lines(FILE* file);
42

53
int count_blank_lines(FILE* file);

0 commit comments

Comments
 (0)