Cleaning up through all code.
This commit is contained in:
parent
fbdc38a171
commit
3b277352d1
19
day1/p1.c
19
day1/p1.c
@ -7,36 +7,35 @@
|
|||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
FILE *input;
|
FILE *input;
|
||||||
char *line = NULL;
|
char *line = NULL;
|
||||||
size_t line_length = 0;
|
size_t line_buf_length = 0;
|
||||||
|
|
||||||
if (argc == 1 || argv[1][0] == '-')
|
if (argc == 1 || argv[1][0] == '-')
|
||||||
input = stdin;
|
input = stdin;
|
||||||
else if ((input = fopen(argv[1], "r")) == NULL) {
|
else if ((input = fopen(argv[1], "r")) == NULL) {
|
||||||
printf("Cannot open file %s\n", argv[1]);
|
fprintf(stderr, "Cannot open file %s\n", argv[1]);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int32_t calibration_value = 0;
|
uint64_t calibration_value = 0;
|
||||||
|
|
||||||
while (getline(&line, &line_length, input) != -1) {
|
while (getline(&line, &line_buf_length, input) != -1) {
|
||||||
int32_t current_calibration_value = -1;
|
int64_t current_calibration_value = -1;
|
||||||
int8_t last_digit = 0;
|
uint8_t last_digit = 0;
|
||||||
|
|
||||||
for (size_t i = 0; i < line_length-1; ++i) {
|
for (size_t i = 0; i < line_buf_length-1; ++i)
|
||||||
if (line[i]>>4 == 0x3) {
|
if (line[i]>>4 == 0x3) {
|
||||||
if (current_calibration_value == -1)
|
if (current_calibration_value == -1)
|
||||||
current_calibration_value = (line[i]&0xf) * 10;
|
current_calibration_value = (line[i]&0xf) * 10;
|
||||||
last_digit = line[i]&0xf;
|
last_digit = line[i]&0xf;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (current_calibration_value != -1)
|
if (current_calibration_value != -1)
|
||||||
calibration_value += current_calibration_value + last_digit;
|
calibration_value += current_calibration_value + last_digit;
|
||||||
|
|
||||||
memset(line, 0, line_length);
|
memset(line, 0, line_buf_length);
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("%d\n", calibration_value);
|
printf("%lu\n", calibration_value);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
56
day1/p2.c
56
day1/p2.c
@ -7,79 +7,74 @@
|
|||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
FILE *input;
|
FILE *input;
|
||||||
char *line = NULL;
|
char *line = NULL;
|
||||||
size_t line_length = 0;
|
size_t line_buf_length = 0;
|
||||||
|
|
||||||
if (argc == 1 || argv[1][0] == '-')
|
if (argc == 1 || argv[1][0] == '-')
|
||||||
input = stdin;
|
input = stdin;
|
||||||
else if ((input = fopen(argv[1], "r")) == NULL) {
|
else if ((input = fopen(argv[1], "r")) == NULL) {
|
||||||
printf("Cannot open file %s\n", argv[1]);
|
fprintf(stderr, "Cannot open file %s\n", argv[1]);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int32_t calibration_value = 0;
|
uint64_t calibration_value = 0;
|
||||||
|
|
||||||
while (getline(&line, &line_length, input) != -1) {
|
while (getline(&line, &line_buf_length, input) != -1) {
|
||||||
int32_t current_calibration_value = 0;
|
uint64_t current_calibration_value = 0;
|
||||||
int8_t last_digit = 0;
|
int8_t last_digit = 0;
|
||||||
|
|
||||||
for (size_t i = 0; i < line_length-1; ++i) {
|
for (size_t i = 0; i < line_buf_length-1; ++i)
|
||||||
if (line[i]>>4 == 0x3) {
|
if (line[i]>>4 == 0x3) {
|
||||||
if (current_calibration_value == 0)
|
if (current_calibration_value == 0)
|
||||||
current_calibration_value = (line[i]&0xf) * 10;
|
current_calibration_value = (line[i]&0xf) * 10;
|
||||||
last_digit = line[i]&0xf;
|
last_digit = line[i]&0xf;
|
||||||
} else {
|
} else {
|
||||||
int8_t digit = 0;
|
uint8_t digit = 0;
|
||||||
|
|
||||||
switch (line[i]) {
|
switch (line[i]) {
|
||||||
case 'o': // 1
|
case 'o': // 1
|
||||||
if (i+2 <= line_length)
|
if (i+2 <= line_buf_length)
|
||||||
if (line[i+1] == 'n' && line[i+2] == 'e') {
|
if (line[i+1] == 'n' && line[i+2] == 'e')
|
||||||
digit = 1;
|
digit = 1;
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
case 't': // 2 3
|
case 't': // 2 3
|
||||||
if (i+2 <= line_length)
|
if (i+2 <= line_buf_length)
|
||||||
if (line[i+1] == 'w' && line[i+2] == 'o') {
|
if (line[i+1] == 'w' && line[i+2] == 'o') {
|
||||||
digit = 2;
|
digit = 2;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (i+4 <= line_length)
|
if (i+4 <= line_buf_length)
|
||||||
if (line[i+1] == 'h' && line[i+2] == 'r' && line[i+3] == 'e' && line[i+4] == 'e') {
|
if (line[i+1] == 'h' && line[i+2] == 'r' && line[i+3] == 'e' && line[i+4] == 'e')
|
||||||
digit = 3;
|
digit = 3;
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
case 'f': // 4 5
|
case 'f': // 4 5
|
||||||
if (i+3 <= line_length)
|
if (i+3 <= line_buf_length)
|
||||||
if (line[i+1] == 'o' && line[i+2] == 'u' && line[i+3] == 'r') {
|
if (line[i+1] == 'o' && line[i+2] == 'u' && line[i+3] == 'r') {
|
||||||
digit = 4;
|
digit = 4;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (i+3 <= line_length)
|
if (i+3 <= line_buf_length)
|
||||||
if (line[i+1] == 'i' && line[i+2] == 'v' && line[i+3] == 'e') {
|
if (line[i+1] == 'i' && line[i+2] == 'v' && line[i+3] == 'e')
|
||||||
digit = 5;
|
digit = 5;
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
case 's': // 6 7
|
case 's': // 6 7
|
||||||
if (i+2 <= line_length)
|
if (i+2 <= line_buf_length)
|
||||||
if (line[i+1] == 'i' && line[i+2] == 'x') {
|
if (line[i+1] == 'i' && line[i+2] == 'x') {
|
||||||
digit = 6;
|
digit = 6;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (i+4 <= line_length)
|
if (i+4 <= line_buf_length)
|
||||||
if (line[i+1] == 'e' && line[i+2] == 'v' && line[i+3] == 'e' && line[i+4] == 'n') {
|
if (line[i+1] == 'e' && line[i+2] == 'v' && line[i+3] == 'e' && line[i+4] == 'n')
|
||||||
digit = 7;
|
digit = 7;
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
case 'e': // 8
|
case 'e': // 8
|
||||||
if (i+4 <= line_length)
|
if (i+4 <= line_buf_length)
|
||||||
if (line[i+1] == 'i' && line[i+2] == 'g' && line[i+3] == 'h' && line[i+4] == 't') {
|
if (line[i+1] == 'i' && line[i+2] == 'g' && line[i+3] == 'h' && line[i+4] == 't')
|
||||||
digit = 8;
|
digit = 8;
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
case 'n': // 9
|
case 'n': // 9
|
||||||
if (i+3 <= line_length)
|
if (i+3 <= line_buf_length)
|
||||||
if (line[i+1] == 'i' && line[i+2] == 'n' && line[i+3] == 'e') {
|
if (line[i+1] == 'i' && line[i+2] == 'n' && line[i+3] == 'e')
|
||||||
digit = 9;
|
digit = 9;
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -89,15 +84,14 @@ int main(int argc, char **argv) {
|
|||||||
last_digit = digit;
|
last_digit = digit;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (current_calibration_value != 0)
|
if (current_calibration_value != 0)
|
||||||
calibration_value += current_calibration_value + last_digit;
|
calibration_value += current_calibration_value + last_digit;
|
||||||
|
|
||||||
memset(line, 0, line_length);
|
memset(line, 0, line_buf_length);
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("%d\n", calibration_value);
|
printf("%lu\n", calibration_value);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
16
day2/p1.c
16
day2/p1.c
@ -6,18 +6,10 @@
|
|||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
|
||||||
// parse_number takes a position of a last digit in a number
|
uint64_t parse_number(char *end);
|
||||||
// and returns a resulting number.
|
|
||||||
int32_t parse_number(char *end);
|
|
||||||
|
|
||||||
|
|
||||||
#define STR_E(tok) #tok
|
|
||||||
#define STR(tok) STR_E(tok)
|
|
||||||
|
|
||||||
|
|
||||||
#define SETS_STR_MAX_LEN 200
|
#define SETS_STR_MAX_LEN 200
|
||||||
|
|
||||||
|
|
||||||
#define CUBE_RED_MAX 12
|
#define CUBE_RED_MAX 12
|
||||||
#define CUBE_GREEN_MAX 13
|
#define CUBE_GREEN_MAX 13
|
||||||
#define CUBE_BLUE_MAX 14
|
#define CUBE_BLUE_MAX 14
|
||||||
@ -31,7 +23,7 @@ int main(int argc, char **argv) {
|
|||||||
if (argc == 1 || argv[1][0] == '-')
|
if (argc == 1 || argv[1][0] == '-')
|
||||||
input = stdin;
|
input = stdin;
|
||||||
else if ((input = fopen(argv[1], "r")) == NULL) {
|
else if ((input = fopen(argv[1], "r")) == NULL) {
|
||||||
printf("Cannot open file %s\n", argv[1]);
|
fprintf(stderr, "Cannot open file %s\n", argv[1]);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -75,8 +67,8 @@ int main(int argc, char **argv) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int32_t parse_number(char *end) {
|
uint64_t parse_number(char *end) {
|
||||||
int32_t num = 0;
|
uint64_t num = 0;
|
||||||
|
|
||||||
for (size_t n = 1;; n *= 10) {
|
for (size_t n = 1;; n *= 10) {
|
||||||
if (*end>>4 != 0x3)
|
if (*end>>4 != 0x3)
|
||||||
|
22
day2/p2.c
22
day2/p2.c
@ -6,18 +6,10 @@
|
|||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
|
||||||
// parse_number takes a position of a last digit in a number
|
uint64_t parse_number(char *end);
|
||||||
// and returns a resulting number.
|
|
||||||
int32_t parse_number(char *end);
|
|
||||||
|
|
||||||
|
|
||||||
#define STR_E(tok) #tok
|
|
||||||
#define STR(tok) STR_E(tok)
|
|
||||||
|
|
||||||
|
|
||||||
#define SETS_STR_MAX_LEN 200
|
#define SETS_STR_MAX_LEN 200
|
||||||
|
|
||||||
|
|
||||||
#define CUBE_RED 0
|
#define CUBE_RED 0
|
||||||
#define CUBE_GREEN 1
|
#define CUBE_GREEN 1
|
||||||
#define CUBE_BLUE 2
|
#define CUBE_BLUE 2
|
||||||
@ -31,11 +23,11 @@ int main(int argc, char **argv) {
|
|||||||
if (argc == 1 || argv[1][0] == '-')
|
if (argc == 1 || argv[1][0] == '-')
|
||||||
input = stdin;
|
input = stdin;
|
||||||
else if ((input = fopen(argv[1], "r")) == NULL) {
|
else if ((input = fopen(argv[1], "r")) == NULL) {
|
||||||
printf("Cannot open file %s\n", argv[1]);
|
fprintf(stderr, "Cannot open file %s\n", argv[1]);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int32_t power_sum = 0;
|
uint64_t power_sum = 0;
|
||||||
|
|
||||||
while (getline(&line, &line_length, input) != -1) {
|
while (getline(&line, &line_length, input) != -1) {
|
||||||
char *colon_pos = strchr(line, ':');
|
char *colon_pos = strchr(line, ':');
|
||||||
@ -51,7 +43,7 @@ int main(int argc, char **argv) {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
int8_t n = parse_number(space_pos-1);
|
uint8_t n = parse_number(space_pos-1);
|
||||||
|
|
||||||
switch (*(space_pos+1)) {
|
switch (*(space_pos+1)) {
|
||||||
case 'r':
|
case 'r':
|
||||||
@ -76,13 +68,13 @@ int main(int argc, char **argv) {
|
|||||||
memset(line, 0, line_length);
|
memset(line, 0, line_length);
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("%d\n", power_sum);
|
printf("%lu\n", power_sum);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int32_t parse_number(char *end) {
|
uint64_t parse_number(char *end) {
|
||||||
int32_t num = 0;
|
uint64_t num = 0;
|
||||||
|
|
||||||
for (size_t n = 1;; n *= 10) {
|
for (size_t n = 1;; n *= 10) {
|
||||||
if (*end>>4 != 0x3)
|
if (*end>>4 != 0x3)
|
||||||
|
15
day3/p1.c
15
day3/p1.c
@ -10,12 +10,12 @@
|
|||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
FILE *input;
|
FILE *input;
|
||||||
char *line = NULL;
|
char *line = NULL;
|
||||||
size_t line_length = 0;
|
size_t line_buf_length = 0;
|
||||||
|
|
||||||
if (argc == 1 || argv[1][0] == '-')
|
if (argc == 1 || argv[1][0] == '-')
|
||||||
input = stdin;
|
input = stdin;
|
||||||
else if ((input = fopen(argv[1], "r")) == NULL) {
|
else if ((input = fopen(argv[1], "r")) == NULL) {
|
||||||
printf("Cannot open file %s\n", argv[1]);
|
fprintf(stderr, "Cannot open file %s\n", argv[1]);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -23,8 +23,9 @@ int main(int argc, char **argv) {
|
|||||||
ssize_t engine_line_size = 0;
|
ssize_t engine_line_size = 0;
|
||||||
ssize_t engine_lines = 0;
|
ssize_t engine_lines = 0;
|
||||||
|
|
||||||
while (getline(&line, &line_length, input) != -1) {
|
while (getline(&line, &line_buf_length, input) != -1) {
|
||||||
ssize_t len = strlen(line)-1;
|
ssize_t len = strlen(line) - 1;
|
||||||
|
|
||||||
if (engine == NULL) {
|
if (engine == NULL) {
|
||||||
engine = (char **) calloc(len, sizeof(char*));
|
engine = (char **) calloc(len, sizeof(char*));
|
||||||
engine_line_size = len;
|
engine_line_size = len;
|
||||||
@ -34,7 +35,7 @@ int main(int argc, char **argv) {
|
|||||||
memcpy(engine[engine_lines], line, len);
|
memcpy(engine[engine_lines], line, len);
|
||||||
++engine_lines;
|
++engine_lines;
|
||||||
|
|
||||||
memset(line, 0, line_length);
|
memset(line, 0, line_buf_length);
|
||||||
}
|
}
|
||||||
|
|
||||||
int32_t part_number = 0;
|
int32_t part_number = 0;
|
||||||
@ -43,7 +44,7 @@ int main(int argc, char **argv) {
|
|||||||
int32_t multiplier = 1;
|
int32_t multiplier = 1;
|
||||||
bool adjacent = false;
|
bool adjacent = false;
|
||||||
|
|
||||||
for (ssize_t i = engine_lines-1; i >= 0; --i) {
|
for (ssize_t i = engine_lines-1; i >= 0; --i)
|
||||||
for (ssize_t j = engine_line_size-1; j >= 0; --j) {
|
for (ssize_t j = engine_line_size-1; j >= 0; --j) {
|
||||||
if (ISDIGIT(engine[i][j])) {
|
if (ISDIGIT(engine[i][j])) {
|
||||||
n += (engine[i][j]&0xf) * multiplier;
|
n += (engine[i][j]&0xf) * multiplier;
|
||||||
@ -60,6 +61,7 @@ int main(int argc, char **argv) {
|
|||||||
adjacent = true;
|
adjacent = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((!ISDIGIT(engine[i][j]) || j == 0) && n != 0) {
|
if ((!ISDIGIT(engine[i][j]) || j == 0) && n != 0) {
|
||||||
if (adjacent)
|
if (adjacent)
|
||||||
part_number += n;
|
part_number += n;
|
||||||
@ -68,7 +70,6 @@ int main(int argc, char **argv) {
|
|||||||
adjacent = false;
|
adjacent = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
for (ssize_t i = 0; i < engine_lines; ++i)
|
for (ssize_t i = 0; i < engine_lines; ++i)
|
||||||
free(engine[i]);
|
free(engine[i]);
|
||||||
|
20
day3/p2.c
20
day3/p2.c
@ -7,7 +7,7 @@
|
|||||||
|
|
||||||
#define ISDIGIT(c) (c >= 0x30 && c <= 0x39)
|
#define ISDIGIT(c) (c >= 0x30 && c <= 0x39)
|
||||||
|
|
||||||
int32_t parse_number(char *line, ssize_t j);
|
uint64_t parse_number(char *line, ssize_t j);
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
FILE *input;
|
FILE *input;
|
||||||
@ -17,7 +17,7 @@ int main(int argc, char **argv) {
|
|||||||
if (argc == 1 || argv[1][0] == '-')
|
if (argc == 1 || argv[1][0] == '-')
|
||||||
input = stdin;
|
input = stdin;
|
||||||
else if ((input = fopen(argv[1], "r")) == NULL) {
|
else if ((input = fopen(argv[1], "r")) == NULL) {
|
||||||
printf("Cannot open file %s\n", argv[1]);
|
fprintf(stderr, "Cannot open file %s\n", argv[1]);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -27,6 +27,7 @@ int main(int argc, char **argv) {
|
|||||||
|
|
||||||
while (getline(&line, &line_length, input) != -1) {
|
while (getline(&line, &line_length, input) != -1) {
|
||||||
ssize_t len = strlen(line)-1;
|
ssize_t len = strlen(line)-1;
|
||||||
|
|
||||||
if (engine == NULL) {
|
if (engine == NULL) {
|
||||||
engine = (char **) calloc(len, sizeof(char*));
|
engine = (char **) calloc(len, sizeof(char*));
|
||||||
engine_line_size = len;
|
engine_line_size = len;
|
||||||
@ -39,14 +40,14 @@ int main(int argc, char **argv) {
|
|||||||
memset(line, 0, line_length);
|
memset(line, 0, line_length);
|
||||||
}
|
}
|
||||||
|
|
||||||
int32_t part_number = 0;
|
uint64_t part_number = 0;
|
||||||
|
|
||||||
for (ssize_t i = engine_lines-1; i >= 0; --i) {
|
for (ssize_t i = engine_lines-1; i >= 0; --i)
|
||||||
for (ssize_t j = engine_line_size-1; j >= 0; --j) {
|
for (ssize_t j = engine_line_size-1; j >= 0; --j) {
|
||||||
if (engine[i][j] != '*')
|
if (engine[i][j] != '*')
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
int32_t n = 1;
|
uint64_t n = 1;
|
||||||
int8_t p = 0;
|
int8_t p = 0;
|
||||||
|
|
||||||
if (ISDIGIT(engine[i][j+1])) {
|
if (ISDIGIT(engine[i][j+1])) {
|
||||||
@ -89,20 +90,19 @@ int main(int argc, char **argv) {
|
|||||||
if (p >= 2)
|
if (p >= 2)
|
||||||
part_number += n;
|
part_number += n;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
for (ssize_t i = 0; i < engine_lines; ++i)
|
for (ssize_t i = 0; i < engine_lines; ++i)
|
||||||
free(engine[i]);
|
free(engine[i]);
|
||||||
free(engine);
|
free(engine);
|
||||||
|
|
||||||
printf("%d\n", part_number);
|
printf("%lu\n", part_number);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int32_t parse_number(char *line, ssize_t j) {
|
uint64_t parse_number(char *line, ssize_t j) {
|
||||||
int32_t n = 0;
|
uint64_t n = 0;
|
||||||
int32_t multiplier = 1;
|
uint64_t multiplier = 1;
|
||||||
|
|
||||||
while (ISDIGIT(line[j+1])) j++;
|
while (ISDIGIT(line[j+1])) j++;
|
||||||
|
|
||||||
|
21
day4/p1.c
21
day4/p1.c
@ -6,7 +6,7 @@
|
|||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
|
||||||
int32_t parse_number(char *end);
|
uint64_t parse_number(char *end);
|
||||||
|
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
@ -21,10 +21,10 @@ int main(int argc, char **argv) {
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int32_t total_points = 0;
|
uint64_t total_points = 0;
|
||||||
|
|
||||||
size_t winning_numbers_len = 0;
|
size_t winning_numbers_len = 0;
|
||||||
int8_t *winning_numbers = NULL;
|
uint8_t *winning_numbers = NULL;
|
||||||
char *colon_pos = NULL, *pipe_pos = NULL;
|
char *colon_pos = NULL, *pipe_pos = NULL;
|
||||||
|
|
||||||
while (getline(&line, &line_length, input) != -1) {
|
while (getline(&line, &line_length, input) != -1) {
|
||||||
@ -33,17 +33,16 @@ int main(int argc, char **argv) {
|
|||||||
|
|
||||||
if (winning_numbers == NULL) {
|
if (winning_numbers == NULL) {
|
||||||
winning_numbers_len = (pipe_pos - colon_pos - 2) / 3;
|
winning_numbers_len = (pipe_pos - colon_pos - 2) / 3;
|
||||||
winning_numbers = (int8_t *) calloc(winning_numbers_len, sizeof(int8_t));
|
winning_numbers = (uint8_t *) calloc(winning_numbers_len, sizeof(uint8_t));
|
||||||
}
|
}
|
||||||
|
|
||||||
for (size_t i = 0, wl_pos = (size_t)colon_pos+4; i < winning_numbers_len; ++i, wl_pos += 3) {
|
for (size_t i = 0, wl_pos = (size_t)colon_pos+4; i < winning_numbers_len; ++i, wl_pos += 3)
|
||||||
winning_numbers[i] = parse_number((char *)(wl_pos-1));
|
winning_numbers[i] = parse_number((char *)(wl_pos-1));
|
||||||
}
|
|
||||||
|
|
||||||
int32_t points = 0;
|
uint64_t points = 0;
|
||||||
|
|
||||||
for (char *n_pos = pipe_pos + 4;; n_pos += 3) {
|
for (char *n_pos = pipe_pos + 4;; n_pos += 3) {
|
||||||
int8_t n = parse_number(n_pos-1);
|
uint8_t n = parse_number(n_pos-1);
|
||||||
for (size_t i = 0; i < winning_numbers_len; ++i) {
|
for (size_t i = 0; i < winning_numbers_len; ++i) {
|
||||||
if (n != winning_numbers[i])
|
if (n != winning_numbers[i])
|
||||||
continue;
|
continue;
|
||||||
@ -61,13 +60,13 @@ int main(int argc, char **argv) {
|
|||||||
|
|
||||||
free(winning_numbers);
|
free(winning_numbers);
|
||||||
|
|
||||||
printf("%d\n", total_points);
|
printf("%lu\n", total_points);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int32_t parse_number(char *end) {
|
uint64_t parse_number(char *end) {
|
||||||
int32_t num = 0;
|
uint64_t num = 0;
|
||||||
|
|
||||||
for (size_t n = 1;; n *= 10) {
|
for (size_t n = 1;; n *= 10) {
|
||||||
if (*end>>4 != 0x3)
|
if (*end>>4 != 0x3)
|
||||||
|
14
day4/p2.c
14
day4/p2.c
@ -9,7 +9,7 @@
|
|||||||
#define STACK_SZ UINT8_MAX+1
|
#define STACK_SZ UINT8_MAX+1
|
||||||
typedef uint8_t stackpointer_t;
|
typedef uint8_t stackpointer_t;
|
||||||
|
|
||||||
int32_t parse_number(char *end);
|
uint64_t parse_number(char *end);
|
||||||
|
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
@ -20,14 +20,14 @@ int main(int argc, char **argv) {
|
|||||||
if (argc == 1 || argv[1][0] == '-')
|
if (argc == 1 || argv[1][0] == '-')
|
||||||
input = stdin;
|
input = stdin;
|
||||||
else if ((input = fopen(argv[1], "r")) == NULL) {
|
else if ((input = fopen(argv[1], "r")) == NULL) {
|
||||||
printf("Cannot open file %s\n", argv[1]);
|
fprintf(stderr, "Cannot open file %s\n", argv[1]);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint64_t total_copies = 0;
|
uint64_t total_copies = 0;
|
||||||
|
|
||||||
size_t winning_numbers_len = 0;
|
size_t winning_numbers_len = 0;
|
||||||
int8_t *winning_numbers = NULL;
|
uint8_t *winning_numbers = NULL;
|
||||||
char *colon_pos = NULL, *pipe_pos = NULL;
|
char *colon_pos = NULL, *pipe_pos = NULL;
|
||||||
|
|
||||||
uint64_t stack[STACK_SZ] = {0};
|
uint64_t stack[STACK_SZ] = {0};
|
||||||
@ -39,7 +39,7 @@ int main(int argc, char **argv) {
|
|||||||
|
|
||||||
if (winning_numbers == NULL) {
|
if (winning_numbers == NULL) {
|
||||||
winning_numbers_len = (pipe_pos - colon_pos - 2) / 3;
|
winning_numbers_len = (pipe_pos - colon_pos - 2) / 3;
|
||||||
winning_numbers = (int8_t *) calloc(winning_numbers_len, sizeof(int8_t));
|
winning_numbers = (uint8_t *) calloc(winning_numbers_len, sizeof(uint8_t));
|
||||||
}
|
}
|
||||||
|
|
||||||
for (size_t i = 0, wl_pos = (size_t)colon_pos+4; i < winning_numbers_len; ++i, wl_pos += 3) {
|
for (size_t i = 0, wl_pos = (size_t)colon_pos+4; i < winning_numbers_len; ++i, wl_pos += 3) {
|
||||||
@ -53,7 +53,7 @@ int main(int argc, char **argv) {
|
|||||||
size_t old_sp = stack_pos;
|
size_t old_sp = stack_pos;
|
||||||
|
|
||||||
for (char *n_pos = pipe_pos + 4;; n_pos += 3) {
|
for (char *n_pos = pipe_pos + 4;; n_pos += 3) {
|
||||||
int8_t n = parse_number(n_pos-1);
|
uint8_t n = parse_number(n_pos-1);
|
||||||
for (size_t i = 0; i < winning_numbers_len; ++i) {
|
for (size_t i = 0; i < winning_numbers_len; ++i) {
|
||||||
if (n != winning_numbers[i])
|
if (n != winning_numbers[i])
|
||||||
continue;
|
continue;
|
||||||
@ -76,8 +76,8 @@ int main(int argc, char **argv) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int32_t parse_number(char *end) {
|
uint64_t parse_number(char *end) {
|
||||||
int32_t num = 0;
|
uint64_t num = 0;
|
||||||
|
|
||||||
for (size_t n = 1;; n *= 10) {
|
for (size_t n = 1;; n *= 10) {
|
||||||
if (*end>>4 != 0x3)
|
if (*end>>4 != 0x3)
|
||||||
|
Loading…
Reference in New Issue
Block a user