-
Notifications
You must be signed in to change notification settings - Fork 0
/
day-03.c
152 lines (120 loc) Β· 2.98 KB
/
day-03.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
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Point {
int x;
int y;
};
int main(void) {
FILE* fp = fopen("input/day-03.txt", "r");
if (fp == NULL) {
perror("Error opening file");
return -1;
}
char* directions = (char*)malloc(8192 * sizeof(char));
fgets(directions, 8193, fp);
fclose(fp);
// This size is arbitrary, but it should be large enough to hold all the
// visited points.
struct Point* visited = (struct Point*)malloc(8192 * sizeof(struct Point));
struct Point current = {0, 0};
int num_visited = 0;
for (int i = 0; i < 8192; i++) {
char direction = directions[i];
if (direction == '\0') {
break;
}
if (i == 0) {
visited[i].x = 0;
visited[i].y = 0;
num_visited++;
}
if (direction == '^') {
current.y++;
} else if (direction == 'v') {
current.y--;
} else if (direction == '>') {
current.x++;
} else if (direction == '<') {
current.x--;
}
bool already_visited = false;
for (int j = 0; j < 8192; j++) {
if (visited[j].x == current.x && visited[j].y == current.y) {
already_visited = true;
break;
}
}
if (!already_visited) {
visited[i].x = current.x;
visited[i].y = current.y;
num_visited++;
}
}
printf("Part 1: Visited %d houses\n", num_visited);
memset(visited, 0, 8192 * sizeof(struct Point));
struct Point santa = {0, 0};
struct Point robo = {0, 0};
num_visited = 0;
for (int i = 0; i < 8192; i += 2) {
char santa_direction = directions[i];
char robo_direction = directions[i + 1];
if (santa_direction == '\0') {
break;
}
if (i == 0) {
visited[i].x = 0;
visited[i].y = 0;
num_visited++;
}
if (santa_direction == '^') {
santa.y++;
} else if (santa_direction == 'v') {
santa.y--;
} else if (santa_direction == '>') {
santa.x++;
} else if (santa_direction == '<') {
santa.x--;
}
bool already_visited = false;
for (int j = 0; j < 8192; j++) {
if (visited[j].x == santa.x && visited[j].y == santa.y) {
already_visited = true;
break;
}
}
if (!already_visited) {
visited[i].x = santa.x;
visited[i].y = santa.y;
num_visited++;
}
if (robo_direction == '\0') {
break;
}
if (robo_direction == '^') {
robo.y++;
} else if (robo_direction == 'v') {
robo.y--;
} else if (robo_direction == '>') {
robo.x++;
} else if (robo_direction == '<') {
robo.x--;
}
already_visited = false;
for (int j = 0; j < 8192; j++) {
if (visited[j].x == robo.x && visited[j].y == robo.y) {
already_visited = true;
break;
}
}
if (!already_visited) {
visited[i + 1].x = robo.x;
visited[i + 1].y = robo.y;
num_visited++;
}
}
printf("Part 2: Visited %d houses\n", num_visited);
free(visited);
free(directions);
}