-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path18.cpp
122 lines (103 loc) · 2.9 KB
/
18.cpp
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
#include "lib/global.hpp"
#include "lib/os_linux.cpp"
#include "lib/format.cpp"
#include "lib/hashmap.cpp"
#include "lib/number.cpp"
#include "lib/algorithm.cpp"
int main() {
auto arr = array_load_from_file("input/18"_arr);
struct Pos {
s64 x = 0, y = 0;
};
auto move = [](Pos dp, u8 d, s64 a) -> Pos {
switch (d) {
case 0: return {dp.x+a, dp.y};
case 1: return {dp.x, dp.y-a};
case 2: return {dp.x-a, dp.y};
case 3: return {dp.x, dp.y+a};
default: assert(false);
}
};
struct Seg {
Pos a, b;
u8 d;
s64 len;
};
Array_dyn<Seg> segs;
Pos p;
Pos maxp, minp;
for (s64 i = 0; i < arr.size; ++i) {
u8 c = arr[i];
if (c == '\n') break;
// This is not the correct parsing routine, but I misplaced the other one
while (arr[i] != '#') ++i;
++i;
s64 a = 0;
for (s64 j = 0; j < 5; ++j) {
u8 c = arr[i];
if ('0' <= arr[i] and arr[i] <= '9') {
a = 16*a + (arr[i] - '0');
} else if ('a' <= arr[i] and arr[i] <= 'f') {
a = 16*a + (arr[i] - 'a' + 10);
} else assert(false);
++i;
}
u8 d = arr[i] - '0';
d ^= 1;
while (arr[i] != '\n') ++i;
Seg s;
s.a = p;
p = move(p, d, a);
s.b = p;
s.d = d;
s.len = a;
array_push_back(&segs, s);
maxp.x = max(maxp.x, p.x);
maxp.y = max(maxp.y, p.y);
minp.x = min(minp.x, p.x);
minp.y = min(minp.y, p.y);
}
assert(p.x == 0 and p.y == 0);
for (Seg& i: segs) {
i.a.x -= minp.x;
i.a.y -= minp.y;
i.b.x -= minp.x;
i.b.y -= minp.y;
}
for (s64 i = 0; i < segs.size; ++i) {
if (segs[i].d & 1) continue;
segs[i] = segs.back();
--i;
--segs.size;
}
for (Seg& i: segs) {
if (i.d == 1) {
simple_swap(&i.a, &i.b);
i.d = 3;
}
}
Pos size = maxp;
size.x -= minp.x;
size.y -= minp.y;
++size.x; ++size.y;
s64 sum = 0;
array_sort_heap(&segs, [](Seg s) { return s.a.y; });
for (s64 y = 0; y < size.y; ++y) {
bool inside0 = false;
bool inside1 = false;
for (s64 x = 0; x < size.x; ++x) {
bool flip0 = false;
bool flip1 = false;
for (Seg i: segs) {
if (i.a.x != x) continue;
if (not (i.a.y <= y and y <= i.b.y)) continue;
flip0 |= i.a.y < y;
flip1 |= i.b.y > y;
}
sum += inside0 or inside1 or flip0 or flip1;
inside0 ^= flip0;
inside1 ^= flip1;
}
}
format_print("%d\n", sum);
}