blob: 8f4e9ad3780cb67b2207c946be76340653ac769d (
plain) (
blame)
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
|
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define FORLINE \
char *line = NULL; \
size_t len = 0; \
while (getline(&line, &len, fp) != EOF)
#define FREELINE \
if (line) \
free(line)
#define FORCH \
char ch = 0; \
while ((ch = fgetc(fp)) != EOF)
#define INFINITE 1024
static int part_one(FILE *fp)
{
int res = 1;
char houses[INFINITE * 2][INFINITE * 2] = { 0 };
int x = INFINITE, y = INFINITE;
houses[x][y] = 1;
FORCH
{
if (ch == '>')
x++;
else if (ch == '<')
x--;
else if (ch == '^')
y++;
else if (ch == 'v')
y--;
if (!houses[x][y]) {
houses[x][y] = 1;
res++;
}
}
return res;
}
static int part_two(FILE *fp)
{
int res = 1;
char houses[INFINITE * 2][INFINITE * 2] = { 0 };
int rx = INFINITE, ry = INFINITE, sx = INFINITE, sy = INFINITE;
houses[sx][sy] = 1;
int robo = 0;
FORCH
{
int *x = robo ? &rx : &sx;
int *y = robo ? &ry : &sy;
if (ch == '>')
(*x)++;
else if (ch == '<')
(*x)--;
else if (ch == '^')
(*y)++;
else if (ch == 'v')
(*y)--;
if (!houses[*x][*y]) {
houses[*x][*y] = 1;
res++;
}
robo = !robo;
}
return res;
}
int main(int argc, char *argv[])
{
(void)argc;
(void)argv;
FILE *fp = fopen("input", "r");
if (!fp)
exit(EXIT_FAILURE);
clock_t tic = clock();
printf("%d\n", part_one(fp));
rewind(fp);
printf("%d\n", part_two(fp));
clock_t toc = clock();
printf("TIME: %f seconds\n", (double)(toc - tic) / CLOCKS_PER_SEC);
fclose(fp);
return 0;
}
|