-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
211 lines (171 loc) · 5.05 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#include <ctype.h>
#include <errno.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define FILENANME "input.txt"
typedef struct
{
int num1;
int num2;
int count;
} gear;
gear Gears[2000] = {0};
int gear_count = 0;
int valid_number[6000] = {0};
int number_index = 0;
char number_storage[4];
int pot_gear_storage[10];
int pot_gear_arr_index = 0;
size_t width = 0;
size_t height = 0;
char *read_from_file()
{
FILE *file = fopen(FILENANME, "r");
if (NULL == file)
{
fprintf(stderr, "input file : %s: %s \n", FILENANME, strerror(errno));
exit(EXIT_FAILURE);
}
fseek(file, 0, SEEK_END); // seek to end of file
size_t length_of_file = ftell(file); // get current file pointer
fseek(file, 0, SEEK_SET); // seek back to beginning
char *buffer = malloc(sizeof(char) * (length_of_file + 1)); // +1 for null terminator
if (NULL == buffer)
{
printf("failed to allocate buffer memory\n\r");
fclose(file);
exit(EXIT_FAILURE);
}
size_t read_size = fread(buffer, 1, length_of_file, file);
buffer[read_size] = '\0';
fclose(file);
return buffer;
}
void get_num(int row_index, int column_index, char matrix_array[height][width])
{
int row = row_index;
int column = column_index;
// Define directions to check
int directions[8][2] = {
{-1, -1}, // Top-left
{-1, 0}, // Up
{-1, 1}, // Top-right
{0, -1}, // Left
{0, 1}, // Right
{1, -1}, // Bottom-left
{1, 0}, // Down
{1, 1} // Bottom-right
};
int found_numbers[2] = {0}; // Store found numbers
int count = 0;
for (int i = 0; i < 8; i++)
{
int new_column = column + directions[i][1]; // Column index vector tranpose maths
int new_row = row + directions[i][0]; // Row index
// Check if the new coordinates are within bounds
if (new_column >= 0 && new_column < width && new_row >= 0 && new_row < height && isdigit(matrix_array[new_row][new_column]))
{
// Check if there's a digit at the new position
// Start extracting the number
// Storage for the number
int idx = 0;
// Find the start of the number by moving left
int start_column = new_column;
while (start_column > 0 && isdigit(matrix_array[new_row][start_column - 1]))
{
start_column--; // Move left to find the start of the number
}
// Collect the digits into number_storage
while (start_column < width && isdigit(matrix_array[new_row][start_column]))
{
number_storage[idx++] = matrix_array[new_row][start_column];
start_column++;
}
number_storage[idx] = '\0'; // Null-terminate the string
// Convert the found number to integer
int found_number = atoi(number_storage);
// Check if the number is not already found
int is_duplicate = 0;
for (int j = 0; j < count; j++)
{
if (found_numbers[j] == found_number)
{
is_duplicate = 1;
break;
}
}
// Store the found number if it's not a duplicate
if (!is_duplicate)
{
if (count < 2)
{ // Store only the first two unique numbers
found_numbers[count++] = found_number;
}
}
// Stop if two unique numbers are found
if (count == 2)
{
break;
}
}
}
// Assign found numbers to the output parameters
if (count == 2)
{
Gears[gear_count].num1 = found_numbers[0];
Gears[gear_count].num2 = found_numbers[1];
Gears[gear_count].count = count;
gear_count++;
}
}
int main()
{
char *contents = read_from_file();
for (size_t i = 0; contents[i] != '\0'; i++)
{
if (contents[i] == '\n')
{
break;
}
width++;
}
height = strlen(contents) / (width + 1);
char matrix[height][width];
size_t height_index = 0;
size_t w_index = 0;
for (size_t i = 0; contents[i] != '\0'; i++)
{
if (contents[i] == '\n')
{
w_index = 0;
height_index++;
}
else
{
matrix[height_index][w_index] = contents[i];
w_index++;
}
}
int ans;
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
if (matrix[i][j] == '*')
{
get_num(i, j, matrix);
}
}
}
long long total_gear_ratio = 0;
for (int i = 0; i < gear_count; i++)
{
total_gear_ratio += (long long)Gears[i].num1 * Gears[i].num2;
}
printf("Total gear ratio: %lld\n", total_gear_ratio);
return 0;
}