-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
250 lines (222 loc) · 8.47 KB
/
Copy pathindex.html
File metadata and controls
250 lines (222 loc) · 8.47 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
<!doctype html>
<html lang="ja">
<head>
<meta charset="utf-8">
<title>Bit Sequence Condition Visualizer v8</title>
<style>
body { font-family: Arial, sans-serif; margin: 20px; }
textarea { width: 100%; box-sizing: border-box; margin-bottom: 10px; font-family: monospace; }
#canvas { border: 1px solid #444; margin-top: 10px; }
.label { font-weight: bold; margin-top: 10px; }
button { margin-right: 6px; }
input[type=text]{ width:120px; margin-right:10px;}
#controls { margin-top:8px; display:flex; align-items:center; flex-wrap:wrap;}
#speedLbl { margin-left:6px; }
#seekLbl { margin: 0 6px; }
details summary { cursor: pointer; font-weight: bold; margin: 12px 0; }
pre.problem { white-space: pre-wrap; background:#f2f2f2; padding:10px; border:1px solid #ccc; }
</style>
</head>
<body>
<h2>Bit Sequence Condition Visualizer v8 (AHC style)</h2>
<details>
<summary>📄 問題文を表示 / Hide spec</summary>
<pre class="problem">
## 概要
長さ N = 100 の 0/1 ビット列 S を提出し、M = 30 条件の得点を最大化せよ。
### 入力
```
N M (固定 100 30)
D1 X1 Y1
...
D_M X_M Y_M
```
制約: 1 ≤ D_i ≤ 30, 0 ≤ X_i ≤ D_i, 1 ≤ Y_i ≤ 10 000
### スコア
各条件 (D_i, X_i, Y_i) について
for s = 0 .. N-D_i
if popcount(S[s..s+D_i-1]) == X_i
score += Y_i
### テストケース生成
乱数生成器 rng, seed 固定
```
D_i ← rand_int(rng, 1, 30)
X_i ← rand_int(rng, 0, D_i)
base ← 10000 / (N - D_i + 1)
Y_i ← round(base * (0.8 + 0.4*rng())) // ±20 %
```
D_i 昇順に並べる。
</pre>
</details>
<div>
<label>Seed: <input type="text" id="seed" value="0"></label>
<button id="gen">Generate Random Instance</button>
</div>
<label class="label">Problem instance</label>
<textarea id="instance" rows="16"></textarea>
<label class="label">Your solution bit string(s) — one 100‑char 0/1 string per line</label>
<textarea id="solution" rows="6"></textarea>
<div id="controls">
<button id="prev">◀ Prev</button>
<input type="range" id="seek" min="1" value="1" style="flex:1;" hidden>
<span id="seekLbl" hidden>1/1</span>
<button id="next">Next ▶</button>
<button id="play">▶ Play</button>
<label id="speedLbl">Speed: <input type="range" id="speed" min="100" max="2000" value="500" step="100"> <span id="speedVal">0.5s</span></label>
<button id="run">Load Solutions</button>
</div>
<p id="status" style="font-size: 1.1em; font-weight: bold;"></p>
<canvas id="canvas" width="1400" height="800"></canvas>
<script>
// constants
const DEFAULT_N = 100;
const DEFAULT_M = 30;
const LEFT_MARGIN = 120; // increased for labels
const CELL_SIZE = 10;
const ROW_H = 18;
// global state
let instGlobal = null;
let solutionsArr = [];
let currentIdx = 0;
let playTimer = null;
// PRNG utilities
function mulberry32(a){return function(){var t=a+=0x6D2B79F5;t=Math.imul(t^(t>>>15),t|1);t^=t+Math.imul(t^(t>>>7),t|61);return((t^(t>>>14))>>>0)/4294967296;}}
function stringToSeed(str){let h=0;for(let i=0;i<str.length;i++){h=Math.imul(31,h)+str.charCodeAt(i)|0;}return h>>>0;}
function randInt(rng,min,max){return Math.floor(rng()*(max-min+1))+min;}
// instance generator
function generateInstance(){
const seedStr=document.getElementById("seed").value.trim();
const seedNum=seedStr===""?Math.floor(Math.random()*2**32):(/^\d+$/.test(seedStr)?parseInt(seedStr,10):stringToSeed(seedStr));
const rng=mulberry32(seedNum);
const N=DEFAULT_N, M=DEFAULT_M;
const conds=[];
for(let i=0;i<M;i++){
const D=randInt(rng,1,30);
const X=randInt(rng,0,D);
const baseY=10000/(N-D+1);
let Y=Math.round(baseY*(0.8+0.4*rng()));
Y=Math.min(10000,Math.max(1,Y));
conds.push({D,X,Y});
}
conds.sort((a,b)=>a.D-b.D);
const instTxt=N+" "+M+"\n"+conds.map(c=>c.D+" "+c.X+" "+c.Y).join("\n");
document.getElementById("instance").value=instTxt;
let sol=""; for(let i=0;i<N;i++) sol+=rng()<0.5?"0":"1";
document.getElementById("solution").value=sol;
}
// parsing
function parseInstance(text){
text=text.trim(); if(text===""){alert("Instance empty");return null;}
const lines=text.split(/\n+/).map(l=>l.trim()).filter(l=>l!=="");
let N=DEFAULT_N,M=lines.length,idx=0;
if(/^\d+\s+\d+/.test(lines[0])){const p=lines[0].split(/\s+/).map(Number);N=p[0];M=p[1];idx=1;}
const conds=[];
for(let i=idx;i<lines.length;i++){const [D,X,Y]=lines[i].split(/\s+/).map(Number);conds.push({D,X,Y});}
if(conds.length!==M){alert("M mismatch");return null;}
return{N,M,conds};
}
function computeScore(N, bits, conds){
let total=0; const met=conds.map(()=>[]);
conds.forEach((c,i)=>{
for(let s=0;s<=N-c.D;s++){
let cnt=0; for(let k=0;k<c.D;k++) cnt+=bits[s+k];
if(cnt===c.X){total+=c.Y; met[i].push(s);}
}
});
return{total,met};
}
// drawing
function draw(N,bits,conds,met){
const canvas=document.getElementById("canvas");
const ctx=canvas.getContext("2d");
canvas.width=Math.max(LEFT_MARGIN+CELL_SIZE*N+50,1400);
canvas.height=(conds.length+2)*ROW_H+20;
ctx.clearRect(0,0,canvas.width,canvas.height);
ctx.font="12px monospace"; ctx.textBaseline="middle";
// bits row
for(let i=0;i<N;i++){
ctx.fillStyle=bits[i]?"#0066ff":"#e0e0e0";
ctx.fillRect(LEFT_MARGIN+i*CELL_SIZE,10,CELL_SIZE,ROW_H-4);
}
ctx.fillStyle="#000";
ctx.fillText("bits",5,10+(ROW_H-4)/2);
// conditions
conds.forEach((c,idx)=>{
const y=10+(idx+1)*ROW_H;
ctx.fillStyle="#000";
const label=`D=${c.D} X=${c.X} Y=${c.Y}`;
ctx.fillText(label,5,y+(ROW_H-4)/2);
const valid=N-c.D+1;
ctx.fillStyle="#f7f7f7";
ctx.fillRect(LEFT_MARGIN,y,valid*CELL_SIZE,ROW_H-4);
ctx.fillStyle="#00cc66";
met[idx].forEach(s=>{
ctx.fillRect(LEFT_MARGIN+s*CELL_SIZE,y,CELL_SIZE,ROW_H-4);
});
});
ctx.strokeStyle="#cccccc";
for(let i=0;i<=N;i+=10){
ctx.beginPath();
ctx.moveTo(LEFT_MARGIN+i*CELL_SIZE,10);
ctx.lineTo(LEFT_MARGIN+i*CELL_SIZE,10+(conds.length+1)*ROW_H);
ctx.stroke();
}
}
// solution utilities
function parseSolutions(text,N){
return text.split(/\n+/).map(l=>l.replace(/\s+/g,"")).filter(b=>b.length===N&&/^[01]+$/.test(b));
}
function updateSeekUI(){
const seek=document.getElementById("seek");
const lbl=document.getElementById("seekLbl");
if(solutionsArr.length<=1){
seek.hidden=true; lbl.hidden=true;
}else{
seek.hidden=false; lbl.hidden=false;
seek.max=solutionsArr.length;
seek.value=currentIdx+1;
lbl.textContent=`${currentIdx+1}/${solutionsArr.length}`;
}
}
function load(idx){
if(!instGlobal||solutionsArr.length===0)return;
currentIdx=(idx+solutionsArr.length)%solutionsArr.length;
const bits=[...solutionsArr[currentIdx]].map(ch=>ch==="1"?1:0);
const {total,met}=computeScore(instGlobal.N,bits,instGlobal.conds);
document.getElementById("status").textContent=`Solution ${currentIdx+1}/${solutionsArr.length} — Score: ${total}`;
draw(instGlobal.N,bits,instGlobal.conds,met);
updateSeekUI();
}
// playback
function stopPlay(){if(playTimer){clearInterval(playTimer);playTimer=null;document.getElementById("play").textContent="▶ Play";}}
// event bindings
document.getElementById("gen").onclick=generateInstance;
document.getElementById("run").onclick=()=>{
stopPlay();
instGlobal=parseInstance(document.getElementById("instance").value);
if(!instGlobal)return;
solutionsArr=parseSolutions(document.getElementById("solution").value,instGlobal.N);
if(solutionsArr.length===0){alert("No valid solutions");return;}
load(0);
};
document.getElementById("prev").onclick=()=>{stopPlay(); if(solutionsArr.length) load(currentIdx-1);};
document.getElementById("next").onclick=()=>{stopPlay(); if(solutionsArr.length) load(currentIdx+1);};
document.getElementById("seek").oninput=e=>{stopPlay(); load(parseInt(e.target.value,10)-1);};
document.getElementById("play").onclick=()=>{
if(solutionsArr.length<=1)return;
if(!playTimer){
const speed=parseInt(document.getElementById("speed").value,10);
playTimer=setInterval(()=>load(currentIdx+1),speed);
document.getElementById("play").textContent="⏸ Pause";
}else stopPlay();
};
document.getElementById("speed").oninput=e=>{
document.getElementById("speedVal").textContent=(e.target.value/1000).toFixed(1)+"s";
if(playTimer){stopPlay(); document.getElementById("play").click();}
};
// initial
generateInstance();
document.getElementById("run").click();
</script>
</body>
</html>