2024-12-03 15:35:07 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
2024-12-04 13:34:52 +01:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdbool.h>
|
2024-12-03 15:35:07 +01:00
|
|
|
|
|
|
|
#define LINES 1000
|
|
|
|
|
2024-12-04 13:34:52 +01:00
|
|
|
struct instruction {
|
|
|
|
char *coord;
|
|
|
|
struct instruction *next;
|
|
|
|
};
|
|
|
|
|
|
|
|
void addInstruction(struct instruction *start, char *coord)
|
2024-12-03 15:35:07 +01:00
|
|
|
{
|
2024-12-07 22:09:46 +01:00
|
|
|
// traverse list until last member
|
2024-12-04 13:34:52 +01:00
|
|
|
while(start->next) start = start->next;
|
|
|
|
|
|
|
|
// allocate and store ptr
|
|
|
|
start->next = malloc(sizeof(struct instruction));
|
|
|
|
|
|
|
|
// store coords in new item
|
|
|
|
start->next->coord = coord;
|
|
|
|
start->next->next = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool confirm_numbers(char *start, char *end) {
|
|
|
|
for (int i = 0; (start[i] >= '0' && start[i] <= '9') || start[i] == ','; i++) {
|
|
|
|
if (start[i + 1 ] == ')')
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int parse_numbers(char *string)
|
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
int a = 0;
|
|
|
|
int b = 0;
|
|
|
|
while (string[i] >= '0' && string[i] <= '9') {
|
|
|
|
a = a * 10 + string[i] - '0';
|
2024-12-03 15:35:07 +01:00
|
|
|
i++;
|
|
|
|
}
|
2024-12-04 13:34:52 +01:00
|
|
|
// skip comma!
|
2024-12-03 15:35:07 +01:00
|
|
|
i++;
|
2024-12-04 13:34:52 +01:00
|
|
|
while (string[i] >= '0' && string[i] <= '9') {
|
|
|
|
b = b * 10 + string[i] - '0';
|
2024-12-03 15:35:07 +01:00
|
|
|
i++;
|
|
|
|
}
|
2024-12-04 13:34:52 +01:00
|
|
|
return a * b;
|
|
|
|
}
|
|
|
|
|
2024-12-03 15:35:07 +01:00
|
|
|
int main()
|
|
|
|
{
|
2024-12-04 13:34:52 +01:00
|
|
|
unsigned int sum = 0;
|
|
|
|
char instructions[32];
|
|
|
|
|
|
|
|
FILE *input;
|
|
|
|
input = fopen("./input", "r");
|
|
|
|
|
|
|
|
fseek(input, 0L, SEEK_END);
|
|
|
|
unsigned int fLen = ftell(input);
|
2024-12-07 22:09:46 +01:00
|
|
|
char *memory = malloc(fLen * sizeof(char) + 1);
|
2024-12-04 13:34:52 +01:00
|
|
|
|
|
|
|
rewind(input);
|
|
|
|
fread(memory, fLen, sizeof(char), input);
|
2024-12-07 22:09:46 +01:00
|
|
|
memory[fLen + 1] = '\0';
|
2024-12-04 13:34:52 +01:00
|
|
|
fclose(input);
|
|
|
|
|
2024-12-07 22:09:46 +01:00
|
|
|
// find first instruction start for the list head
|
2024-12-04 13:34:52 +01:00
|
|
|
char *start = memory;
|
|
|
|
start = strstr(start, "mul(") + 4;
|
|
|
|
struct instruction *list = malloc(sizeof(struct instruction));
|
|
|
|
list->coord = start;
|
|
|
|
list->next = 0;
|
|
|
|
|
|
|
|
// find the rest of the instructions and append to the linked list
|
|
|
|
while (start = strstr(start, "mul(")) {
|
|
|
|
start = start + 4;
|
|
|
|
// confirm format %d,%d)
|
|
|
|
if (confirm_numbers(start, memory + fLen))
|
|
|
|
addInstruction(list, start);
|
2024-12-03 15:35:07 +01:00
|
|
|
}
|
2024-12-04 13:34:52 +01:00
|
|
|
|
|
|
|
// from each instruction start, parse and mult numbers
|
|
|
|
struct instruction *current = list;
|
|
|
|
while (current) {
|
|
|
|
sum += parse_numbers(current->coord);
|
|
|
|
current = current->next;
|
|
|
|
}
|
|
|
|
|
2024-12-03 15:35:07 +01:00
|
|
|
printf("%d\n", sum);
|
|
|
|
}
|