-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadventofcodeday1.c
More file actions
58 lines (49 loc) · 1.24 KB
/
adventofcodeday1.c
File metadata and controls
58 lines (49 loc) · 1.24 KB
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
#include <stdio.h>
#include <stdlib.h>
#define MAX_STEPS 100
struct rotation {
char direction;
int steps;
int location;
int passwd;
};
void init(struct rotation *rotation) {
rotation->direction = 0;
rotation->steps = 0;
rotation->location = 50;
rotation->passwd = 0;
}
void format(int *location) {
printf("Location before: %d\n", *location);
*location = ((*location % MAX_STEPS) + MAX_STEPS) % MAX_STEPS;
printf("Location after: %d\n", *location);
}
void rotate(struct rotation *rotation) {
if (rotation->direction == 'R') {
rotation->location += rotation->steps;
} else {
rotation->location -= rotation->steps;
}
format(&rotation->location);
if (!rotation->location) {
rotation->passwd++;
}
}
int main() {
struct rotation *rotation = malloc(sizeof(struct rotation));
init(rotation);
FILE *file = fopen("input", "r");
if (file == NULL) {
printf("Error opening file\n");
return 1;
}
char *line = malloc(500);
while (fgets(line, 500, file)) {
sscanf(line, "%c%d", &rotation->direction, &rotation->steps);
printf("Direction: %c, Steps: %d\n", rotation->direction, rotation->steps);
rotate(rotation);
}
printf("Password is: %d\n", rotation->passwd);
fclose(file);
return 0;
}