-
Notifications
You must be signed in to change notification settings - Fork 10
/
compressreq.html
98 lines (87 loc) · 1.79 KB
/
compressreq.html
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
<html>
<body>
<script>
function makeShape(map) {
var i,j,s=[];
for(i=0; i<map.length; ++i ) {
s[i]=[];
for(j=0; j<map[i].length; ++j ) {
if( map[i].substr(j,1) != '.' ) {
s[i][j] = 1;
}
}
}
return s;
}
map = [];
map[0] = [
'..........',
'.#...##.#.',
'.#........',
'.#........',
'.#........',
'..........'
];
map[1] = [
'..........',
'.#........',
'.#........',
'.#######..',
'.#######..',
'..........'
];
map[2] = [
'..........',
'.#######..',
'.#######..',
'.#######..',
'..........',
'..........'
];
shape = [];
shape[0] = makeShape(map[0]);
shape[1] = makeShape(map[1]);
shape[2] = makeShape(map[2]);
function compress(s) {
var i,j,a=null,b=[],q=[];
// horizontal
for(i=0; i<s.length;++i) {
for(j=0; j<=s[i].length;++j) {
if( a===null && s[i][j]==1 ) {
a=j;
}
if( a!==null && s[i][j]!=1 ) {
if( j-1>a ) {
b.push( { i:i, j:a+'-'+(j-1) } );
} else {
b.push( { i:i, j:a } );
}
a=null;
}
}
}
// sort to optimize compression
b.sort(function(a,b){return a.i-b.i});
b.sort(function(a,b){return (a.j+'').localeCompare(b.j)});
// add dummy
b.push( { i:null, j:null } );
// vertical compression
if(b.length>1) {
a=0;
for(i=1; i<b.length;++i) {
if( (b[i].j !== b[i-1].j) || ( (b[i].i-1) !== b[i-1].i ) ) {
if( b[i-1].i > b[a].i ) {
q.push( b[a].i+'-'+b[i-1].i+','+b[a].j );
} else {
q.push( b[a].i+','+b[a].j );
}
a=i;
}
}
}
return q.join('|');
}
compress(shape[0]);
</script>
</body>
</html>