Cleaning up through all code.

This commit is contained in:
Alexander Andreev 2023-12-06 05:18:43 +04:00
parent fbdc38a171
commit 3b277352d1
Signed by: Arav
GPG Key ID: D22A817D95815393
8 changed files with 80 additions and 103 deletions

View File

@ -7,36 +7,35 @@
int main(int argc, char **argv) {
FILE *input;
char *line = NULL;
size_t line_length = 0;
size_t line_buf_length = 0;
if (argc == 1 || argv[1][0] == '-')
input = stdin;
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;
}
int32_t calibration_value = 0;
uint64_t calibration_value = 0;
while (getline(&line, &line_length, input) != -1) {
int32_t current_calibration_value = -1;
int8_t last_digit = 0;
while (getline(&line, &line_buf_length, input) != -1) {
int64_t current_calibration_value = -1;
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 (current_calibration_value == -1)
current_calibration_value = (line[i]&0xf) * 10;
last_digit = line[i]&0xf;
}
}
if (current_calibration_value != -1)
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;
}

View File

@ -7,79 +7,74 @@
int main(int argc, char **argv) {
FILE *input;
char *line = NULL;
size_t line_length = 0;
size_t line_buf_length = 0;
if (argc == 1 || argv[1][0] == '-')
input = stdin;
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;
}
int32_t calibration_value = 0;
uint64_t calibration_value = 0;
while (getline(&line, &line_length, input) != -1) {
int32_t current_calibration_value = 0;
while (getline(&line, &line_buf_length, input) != -1) {
uint64_t current_calibration_value = 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 (current_calibration_value == 0)
current_calibration_value = (line[i]&0xf) * 10;
last_digit = line[i]&0xf;
} else {
int8_t digit = 0;
uint8_t digit = 0;
switch (line[i]) {
case 'o': // 1
if (i+2 <= line_length)
if (line[i+1] == 'n' && line[i+2] == 'e') {
if (i+2 <= line_buf_length)
if (line[i+1] == 'n' && line[i+2] == 'e')
digit = 1;
}
break;
case 't': // 2 3
if (i+2 <= line_length)
if (i+2 <= line_buf_length)
if (line[i+1] == 'w' && line[i+2] == 'o') {
digit = 2;
break;
}
if (i+4 <= line_length)
if (line[i+1] == 'h' && line[i+2] == 'r' && line[i+3] == 'e' && line[i+4] == 'e') {
if (i+4 <= line_buf_length)
if (line[i+1] == 'h' && line[i+2] == 'r' && line[i+3] == 'e' && line[i+4] == 'e')
digit = 3;
}
break;
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') {
digit = 4;
break;
}
if (i+3 <= line_length)
if (line[i+1] == 'i' && line[i+2] == 'v' && line[i+3] == 'e') {
if (i+3 <= line_buf_length)
if (line[i+1] == 'i' && line[i+2] == 'v' && line[i+3] == 'e')
digit = 5;
}
break;
case 's': // 6 7
if (i+2 <= line_length)
if (i+2 <= line_buf_length)
if (line[i+1] == 'i' && line[i+2] == 'x') {
digit = 6;
break;
}
if (i+4 <= line_length)
if (line[i+1] == 'e' && line[i+2] == 'v' && line[i+3] == 'e' && line[i+4] == 'n') {
if (i+4 <= line_buf_length)
if (line[i+1] == 'e' && line[i+2] == 'v' && line[i+3] == 'e' && line[i+4] == 'n')
digit = 7;
}
break;
case 'e': // 8
if (i+4 <= line_length)
if (line[i+1] == 'i' && line[i+2] == 'g' && line[i+3] == 'h' && line[i+4] == 't') {
if (i+4 <= line_buf_length)
if (line[i+1] == 'i' && line[i+2] == 'g' && line[i+3] == 'h' && line[i+4] == 't')
digit = 8;
}
break;
case 'n': // 9
if (i+3 <= line_length)
if (line[i+1] == 'i' && line[i+2] == 'n' && line[i+3] == 'e') {
if (i+3 <= line_buf_length)
if (line[i+1] == 'i' && line[i+2] == 'n' && line[i+3] == 'e')
digit = 9;
}
break;
}
@ -89,15 +84,14 @@ int main(int argc, char **argv) {
last_digit = digit;
}
}
}
if (current_calibration_value != 0)
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;
}

View File

@ -6,18 +6,10 @@
#include <stdbool.h>
// parse_number takes a position of a last digit in a number
// and returns a resulting number.
int32_t parse_number(char *end);
#define STR_E(tok) #tok
#define STR(tok) STR_E(tok)
uint64_t parse_number(char *end);
#define SETS_STR_MAX_LEN 200
#define CUBE_RED_MAX 12
#define CUBE_GREEN_MAX 13
#define CUBE_BLUE_MAX 14
@ -31,7 +23,7 @@ int main(int argc, char **argv) {
if (argc == 1 || argv[1][0] == '-')
input = stdin;
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;
}
@ -75,8 +67,8 @@ int main(int argc, char **argv) {
return 0;
}
int32_t parse_number(char *end) {
int32_t num = 0;
uint64_t parse_number(char *end) {
uint64_t num = 0;
for (size_t n = 1;; n *= 10) {
if (*end>>4 != 0x3)

View File

@ -6,18 +6,10 @@
#include <stdbool.h>
// parse_number takes a position of a last digit in a number
// and returns a resulting number.
int32_t parse_number(char *end);
#define STR_E(tok) #tok
#define STR(tok) STR_E(tok)
uint64_t parse_number(char *end);
#define SETS_STR_MAX_LEN 200
#define CUBE_RED 0
#define CUBE_GREEN 1
#define CUBE_BLUE 2
@ -31,11 +23,11 @@ int main(int argc, char **argv) {
if (argc == 1 || argv[1][0] == '-')
input = stdin;
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;
}
int32_t power_sum = 0;
uint64_t power_sum = 0;
while (getline(&line, &line_length, input) != -1) {
char *colon_pos = strchr(line, ':');
@ -51,7 +43,7 @@ int main(int argc, char **argv) {
continue;
}
int8_t n = parse_number(space_pos-1);
uint8_t n = parse_number(space_pos-1);
switch (*(space_pos+1)) {
case 'r':
@ -76,13 +68,13 @@ int main(int argc, char **argv) {
memset(line, 0, line_length);
}
printf("%d\n", power_sum);
printf("%lu\n", power_sum);
return 0;
}
int32_t parse_number(char *end) {
int32_t num = 0;
uint64_t parse_number(char *end) {
uint64_t num = 0;
for (size_t n = 1;; n *= 10) {
if (*end>>4 != 0x3)

View File

@ -10,12 +10,12 @@
int main(int argc, char **argv) {
FILE *input;
char *line = NULL;
size_t line_length = 0;
size_t line_buf_length = 0;
if (argc == 1 || argv[1][0] == '-')
input = stdin;
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;
}
@ -23,8 +23,9 @@ int main(int argc, char **argv) {
ssize_t engine_line_size = 0;
ssize_t engine_lines = 0;
while (getline(&line, &line_length, input) != -1) {
ssize_t len = strlen(line)-1;
while (getline(&line, &line_buf_length, input) != -1) {
ssize_t len = strlen(line) - 1;
if (engine == NULL) {
engine = (char **) calloc(len, sizeof(char*));
engine_line_size = len;
@ -34,7 +35,7 @@ int main(int argc, char **argv) {
memcpy(engine[engine_lines], line, len);
++engine_lines;
memset(line, 0, line_length);
memset(line, 0, line_buf_length);
}
int32_t part_number = 0;
@ -43,7 +44,7 @@ int main(int argc, char **argv) {
int32_t multiplier = 1;
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) {
if (ISDIGIT(engine[i][j])) {
n += (engine[i][j]&0xf) * multiplier;
@ -60,6 +61,7 @@ int main(int argc, char **argv) {
adjacent = true;
}
}
if ((!ISDIGIT(engine[i][j]) || j == 0) && n != 0) {
if (adjacent)
part_number += n;
@ -68,7 +70,6 @@ int main(int argc, char **argv) {
adjacent = false;
}
}
}
for (ssize_t i = 0; i < engine_lines; ++i)
free(engine[i]);

View File

@ -7,7 +7,7 @@
#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) {
FILE *input;
@ -17,7 +17,7 @@ int main(int argc, char **argv) {
if (argc == 1 || argv[1][0] == '-')
input = stdin;
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;
}
@ -27,6 +27,7 @@ int main(int argc, char **argv) {
while (getline(&line, &line_length, input) != -1) {
ssize_t len = strlen(line)-1;
if (engine == NULL) {
engine = (char **) calloc(len, sizeof(char*));
engine_line_size = len;
@ -39,14 +40,14 @@ int main(int argc, char **argv) {
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) {
if (engine[i][j] != '*')
continue;
int32_t n = 1;
uint64_t n = 1;
int8_t p = 0;
if (ISDIGIT(engine[i][j+1])) {
@ -89,20 +90,19 @@ int main(int argc, char **argv) {
if (p >= 2)
part_number += n;
}
}
for (ssize_t i = 0; i < engine_lines; ++i)
free(engine[i]);
free(engine);
printf("%d\n", part_number);
printf("%lu\n", part_number);
return 0;
}
int32_t parse_number(char *line, ssize_t j) {
int32_t n = 0;
int32_t multiplier = 1;
uint64_t parse_number(char *line, ssize_t j) {
uint64_t n = 0;
uint64_t multiplier = 1;
while (ISDIGIT(line[j+1])) j++;

View File

@ -6,7 +6,7 @@
#include <stdbool.h>
int32_t parse_number(char *end);
uint64_t parse_number(char *end);
int main(int argc, char **argv) {
@ -21,10 +21,10 @@ int main(int argc, char **argv) {
return -1;
}
int32_t total_points = 0;
uint64_t total_points = 0;
size_t winning_numbers_len = 0;
int8_t *winning_numbers = NULL;
uint8_t *winning_numbers = NULL;
char *colon_pos = NULL, *pipe_pos = NULL;
while (getline(&line, &line_length, input) != -1) {
@ -33,17 +33,16 @@ int main(int argc, char **argv) {
if (winning_numbers == NULL) {
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));
}
int32_t points = 0;
uint64_t points = 0;
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) {
if (n != winning_numbers[i])
continue;
@ -61,13 +60,13 @@ int main(int argc, char **argv) {
free(winning_numbers);
printf("%d\n", total_points);
printf("%lu\n", total_points);
return 0;
}
int32_t parse_number(char *end) {
int32_t num = 0;
uint64_t parse_number(char *end) {
uint64_t num = 0;
for (size_t n = 1;; n *= 10) {
if (*end>>4 != 0x3)

View File

@ -9,7 +9,7 @@
#define STACK_SZ UINT8_MAX+1
typedef uint8_t stackpointer_t;
int32_t parse_number(char *end);
uint64_t parse_number(char *end);
int main(int argc, char **argv) {
@ -20,14 +20,14 @@ int main(int argc, char **argv) {
if (argc == 1 || argv[1][0] == '-')
input = stdin;
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;
}
uint64_t total_copies = 0;
size_t winning_numbers_len = 0;
int8_t *winning_numbers = NULL;
uint8_t *winning_numbers = NULL;
char *colon_pos = NULL, *pipe_pos = NULL;
uint64_t stack[STACK_SZ] = {0};
@ -39,7 +39,7 @@ int main(int argc, char **argv) {
if (winning_numbers == NULL) {
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) {
@ -53,7 +53,7 @@ int main(int argc, char **argv) {
size_t old_sp = stack_pos;
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) {
if (n != winning_numbers[i])
continue;
@ -76,8 +76,8 @@ int main(int argc, char **argv) {
return 0;
}
int32_t parse_number(char *end) {
int32_t num = 0;
uint64_t parse_number(char *end) {
uint64_t num = 0;
for (size_t n = 1;; n *= 10) {
if (*end>>4 != 0x3)