2023-12-01 12:37:32 +02:00
|
|
|
#include <ctype.h>
|
2023-12-01 01:20:50 +02:00
|
|
|
#include <stdio.h>
|
2023-12-01 12:37:32 +02:00
|
|
|
#include <string.h>
|
2023-12-01 01:20:50 +02:00
|
|
|
|
2023-12-01 12:37:32 +02:00
|
|
|
int puzzle1(FILE* fp)
|
2023-12-01 01:20:50 +02:00
|
|
|
{
|
2023-12-01 12:37:32 +02:00
|
|
|
int sum = 0;
|
|
|
|
|
|
|
|
char buffer[128];
|
|
|
|
while (fgets(buffer, sizeof(buffer), fp))
|
|
|
|
{
|
|
|
|
int line_len = strlen(buffer);
|
|
|
|
|
|
|
|
for (int i = 0; i < line_len; i++)
|
|
|
|
{
|
|
|
|
if (!isdigit(buffer[i]))
|
|
|
|
continue;
|
|
|
|
sum += 10 * (buffer[i] - '0');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = line_len - 1; i >= 0; i--)
|
|
|
|
{
|
|
|
|
if (!isdigit(buffer[i]))
|
|
|
|
continue;
|
|
|
|
sum += buffer[i] - '0';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return sum;
|
|
|
|
}
|
|
|
|
|
|
|
|
int puzzle2(FILE* fp)
|
|
|
|
{
|
|
|
|
const char* str_digits[] = {
|
|
|
|
"zero",
|
|
|
|
"one",
|
|
|
|
"two",
|
|
|
|
"three",
|
|
|
|
"four",
|
|
|
|
"five",
|
|
|
|
"six",
|
|
|
|
"seven",
|
|
|
|
"eight",
|
|
|
|
"nine"
|
|
|
|
};
|
|
|
|
|
|
|
|
int sum = 0;
|
|
|
|
|
|
|
|
char buffer[128];
|
|
|
|
while (fgets(buffer, sizeof(buffer), fp))
|
|
|
|
{
|
|
|
|
int line_len = strlen(buffer);
|
|
|
|
|
|
|
|
for (int i = 0; i < line_len; i++)
|
|
|
|
{
|
2023-12-01 13:31:55 +02:00
|
|
|
int digit = -1;
|
2023-12-01 12:37:32 +02:00
|
|
|
if (isdigit(buffer[i]))
|
|
|
|
digit = buffer[i] - '0';
|
2023-12-01 13:31:55 +02:00
|
|
|
else
|
2023-12-01 12:37:32 +02:00
|
|
|
{
|
2023-12-01 13:31:55 +02:00
|
|
|
for (int j = 0; j < 10; j++)
|
2023-12-01 12:37:32 +02:00
|
|
|
{
|
2023-12-01 13:31:55 +02:00
|
|
|
int str_len = strlen(str_digits[j]);
|
|
|
|
if (line_len - i < str_len)
|
|
|
|
continue;
|
|
|
|
if (strncmp(buffer + i, str_digits[j], str_len) == 0)
|
|
|
|
{
|
|
|
|
digit = j;
|
|
|
|
break;
|
|
|
|
}
|
2023-12-01 12:37:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (digit != -1)
|
2023-12-01 13:31:55 +02:00
|
|
|
{
|
|
|
|
sum += 10 * digit;
|
2023-12-01 12:37:32 +02:00
|
|
|
break;
|
2023-12-01 13:31:55 +02:00
|
|
|
}
|
2023-12-01 12:37:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = line_len - 1; i >= 0; i--)
|
|
|
|
{
|
2023-12-01 13:31:55 +02:00
|
|
|
int digit = -1;
|
2023-12-01 12:37:32 +02:00
|
|
|
if (isdigit(buffer[i]))
|
|
|
|
digit = buffer[i] - '0';
|
2023-12-01 13:31:55 +02:00
|
|
|
else
|
2023-12-01 12:37:32 +02:00
|
|
|
{
|
2023-12-01 13:31:55 +02:00
|
|
|
for (int j = 0; j < 10; j++)
|
2023-12-01 12:37:32 +02:00
|
|
|
{
|
2023-12-01 13:31:55 +02:00
|
|
|
int str_len = strlen(str_digits[j]);
|
|
|
|
if (i < str_len)
|
|
|
|
continue;
|
|
|
|
if (strncmp(buffer + i - str_len, str_digits[j], str_len) == 0)
|
|
|
|
{
|
|
|
|
digit = j;
|
|
|
|
break;
|
|
|
|
}
|
2023-12-01 12:37:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (digit != -1)
|
2023-12-01 13:31:55 +02:00
|
|
|
{
|
|
|
|
sum += digit;
|
2023-12-01 12:37:32 +02:00
|
|
|
break;
|
2023-12-01 13:31:55 +02:00
|
|
|
}
|
2023-12-01 12:37:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return sum;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
|
|
|
const char* file_path = "/usr/share/aoc2023/day1_input.txt";
|
|
|
|
if (argc >= 2)
|
|
|
|
file_path = argv[1];
|
|
|
|
|
|
|
|
FILE* fp = fopen(file_path, "r");
|
|
|
|
if (fp == nullptr)
|
|
|
|
{
|
|
|
|
perror("fopen");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("puzzle1: %d\n", puzzle1(fp));
|
|
|
|
|
|
|
|
fseek(fp, 0, SEEK_SET);
|
|
|
|
|
|
|
|
printf("puzzle2: %d\n", puzzle2(fp));
|
|
|
|
|
|
|
|
fclose(fp);
|
2023-12-01 01:20:50 +02:00
|
|
|
}
|