-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday24a.c
116 lines (105 loc) · 2.39 KB
/
day24a.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
#include "common.h"
#include "set.h"
s64 pack (s32 q, s32 r) {
union {
s32 coords [2];
s64 packedvalue;
} coordunion;
coordunion.coords[0] = q;
coordunion.coords[1] = r;
return coordunion.packedvalue;
}
void unpack (s64 v, s32 * q, s32 * r) {
union {
s32 coords [2];
s64 packedvalue;
} coordunion;
coordunion.packedvalue = v;
*q = coordunion.coords[0];
*r = coordunion.coords[1];
}
enum Direction {
DIR_E,
DIR_NE,
DIR_NW,
DIR_W,
DIR_SW,
DIR_SE
};
void offset (s32 * q, s32 * r, enum Direction direction, s32 distance) {
s32 dq = 0;
s32 dr = 0;
switch (direction) {
case DIR_E: {
dq = 1;
} break;
case DIR_NE: {
dr = 1;
} break;
case DIR_NW: {
dq = -1;
dr = 1;
} break;
case DIR_W: {
dq = -1;
} break;
case DIR_SW: {
dr = -1;
} break;
case DIR_SE: {
dq = 1;
dr = -1;
} break;
}
*q += dq*distance;
*r += dr*distance;
}
int main (int argc, char ** argv) {
Set_t tiles;
set_init(&tiles, 512);
do {
char buf [BUFSIZ];
char * s = fgets(&(buf[0]), sizeof(buf), stdin);
if (!s) {
break;
}
s32 q = 0;
s32 r = 0;
while (*s != '\n') {
enum Direction dir = DIR_E;
if (*s == 'e') {
dir = DIR_E;
++s;
} else if (*s == 'w') {
dir = DIR_W;
++s;
} else if (*s == 'n') {
++s;
if (*s == 'e') {
dir = DIR_NE;
} else if (*s == 'w') {
dir = DIR_NW;
}
++s;
} else if (*s == 's') {
++s;
if (*s == 'e') {
dir = DIR_SE;
} else if (*s == 'w') {
dir = DIR_SW;
}
++s;
} else {
ASSERT(0);
}
offset(&q, &r, dir, 1);
}
s64 coordval = pack(q, r);
if (set_contains(&tiles, coordval)) {
set_remove(&tiles, coordval);
} else {
set_add(&tiles, coordval);
}
} while (1);
DISP(tiles.count);
}