-
Notifications
You must be signed in to change notification settings - Fork 2
/
11.awk
74 lines (58 loc) · 1.06 KB
/
11.awk
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
#!/usr/bin/awk -f
BEGIN {
core = sprintf("awk -f 09.awk -v program=%s", program);
x = 0;
y = 0;
minx = 0;
maxx = 0;
miny = 0;
maxy = 0;
dir = 0;
map[0, 0] = int(start);
print(int(map[x, y])) |& core
while ((core |& getline color) > 0) {
if ((core |& getline turn) <= 0) {
print("Illegal sequence");
exit 1;
}
map[x, y] = color;
switch (turn) {
case 0:
dir = (dir + 270) % 360;
break;
case 1:
dir = (dir + 90) % 360;
break;
default:
print("Unknown direction", turn);
exit 1;
}
switch (dir) {
case 0: y++; break;
case 90: x++; break;
case 180: y--; break;
case 270: x--; break;
default:
print("Unknown direction", turn);
exit 1;
}
minx = x < minx ? x : minx;
maxx = x > maxx ? x : maxx;
miny = y < miny ? y : miny;
maxy = y > maxy ? y : maxy;
print(int(map[x, y])) |& core;
}
}
/seen/ {
for (coord in map)
seen++;
print seen;
}
/print/ {
for (y = maxy; y >= miny; y--) {
for (x = minx; x <= maxx; x++) {
printf("%s", map[x, y] == 1 ? "##" : " ");
}
print("");
}
}