2022-03
This commit is contained in:
parent
f707e08fa1
commit
0ad14354a6
@ -1,12 +1,66 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "lfinput.h"
|
||||
|
||||
static int part_one(char **input, size_t sz) {
|
||||
int score = 0;
|
||||
|
||||
for (size_t i = 0; i < sz; ++i) {
|
||||
size_t l = strlen(input[i]) / 2;
|
||||
for (size_t x = 0; x < l; ++x) {
|
||||
for (char *y = input[i] + l; *y != '\0'; ++y) {
|
||||
if (input[i][x] == *y) {
|
||||
if (*y >= 'a' && *y <= 'z') {
|
||||
score += *y - 'a' + 1;
|
||||
} else {
|
||||
score += *y - 'A' + 27;
|
||||
}
|
||||
goto NEXT_P1;
|
||||
}
|
||||
}
|
||||
}
|
||||
NEXT_P1:
|
||||
continue;
|
||||
}
|
||||
|
||||
return score;
|
||||
}
|
||||
|
||||
static int part_two(char **input, size_t sz) {
|
||||
int score = 0;
|
||||
|
||||
for (size_t i = 0; i < sz; i += 3) {
|
||||
char found = 'a';
|
||||
for (char *a = input[i]; *a != '\0'; ++a) {
|
||||
for (char *b = input[i + 1]; *b != '\0'; ++b) {
|
||||
if (*a == *b) {
|
||||
for (char *c = input[i + 2]; *c != '\0'; ++c) {
|
||||
if (*a == *c) {
|
||||
found = *c;
|
||||
goto FOUND_SAME;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
FOUND_SAME:
|
||||
if (found >= 'a' && found <= 'z') {
|
||||
score += found - 'a' + 1;
|
||||
} else {
|
||||
score += found - 'A' + 27;
|
||||
}
|
||||
}
|
||||
|
||||
return score;
|
||||
}
|
||||
|
||||
void advent2022day03(void) {
|
||||
size_t sz = 0;
|
||||
char **input = get_lines("input/2022/03", &sz);
|
||||
|
||||
printf("%d\n", part_one(input, sz));
|
||||
printf("%d\n", part_two(input, sz));
|
||||
|
||||
del_lines(input);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user