-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
1188 lines (1028 loc) · 36.8 KB
/
script.js
File metadata and controls
1188 lines (1028 loc) · 36.8 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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Simple diff visualization implementation
class DiffVisualizer {
constructor() {
this.currentTimestep = 0;
this.currentPhase = 'loading';
this.isRunning = false;
// DOM elements
this.libraryContent = document.getElementById('libraryContent');
this.solutionContent = document.getElementById('solutionContent');
this.libraryFileName = document.getElementById('libraryFileName');
this.solutionFileName = document.getElementById('solutionFileName');
this.currentPhaseEl = document.getElementById('currentPhase');
this.timestepCountEl = document.getElementById('timestepCount');
this.phaseIndicator = document.querySelector('.phase-indicator');
this.maxTimesteps = 3; // We have time0, time1, time2
// Real data from Librarian viz_data
this.sampleData = {
time0: {
library_prev: `
import heapq
from collections import deque
import sys`,
library_next: `
from collections import deque
import heapq
import sys
def read_tree():
n = int(input())
edges = []
degrees = [0] * n
for _ in range(n - 1):
(u, v) = map(int, input().split())
u -= 1
v -= 1
edges.append((u, v))
degrees[u] += 1
degrees[v] += 1
return (n, edges, degrees)
def find_node_by_degree(degrees, threshold):
for (i, d) in enumerate(degrees):
if d >= threshold:
return i
return None
def label_edges_at_node(edges, node, fin, start):
lab = start
for (idx, (u, v)) in enumerate(edges):
if u == node or v == node:
fin[idx] = lab
lab += 1
return lab
def find_leaves(degrees):
return [i for (i, d) in enumerate(degrees) if d == 1]
def label_edges_at_leaves(edges, leaves, fin, start):
lab = start
cnt = 0
for leaf in leaves:
if cnt >= 2:
break
for (idx, (u, v)) in enumerate(edges):
if leaf == u or leaf == v:
fin[idx] = lab
lab += 1
cnt += 1
break
return lab
def label_remaining(fin, start):
lab = start
for i in range(len(fin)):
if fin[i] == -1:
fin[i] = lab
lab += 1
return fin
`,
solution_prev: `n = int(input())
occ = [0 for i in range(n)]
graph = [[0,0] for i in range(n-1)]
for i in range(n-1):
x, y = map(int,input().split())
occ[x-1]+=1
occ[y-1]+=1
graph[i][0] = x-1
graph[i][1] = y-1
fin = [-1 for i in range(n-1)]
for i in range(n):
if occ[i] >= 3 :
var = 0
for j in range(n-1):
if graph[j][0] == i or graph[j][1] == i:
fin[j] = var
var += 1
break
else:
var = 0
for i in range(n):
if var > 1:
break
if occ[i] == 1:
for j in range(n-1):
if graph[j][0] == i or graph[j][1] == i:
fin[j] = var
var += 1
break
for i in fin:
if n == 2:
print(0)
break
if i == -1:
print(var)
var += 1
else:
print(i)`,
solution_next: `from library import *
def main():
import sys
data = sys.stdin
n = int(data.readline())
edges = []
for _ in range(n-1):
u, v = map(int, data.readline().split())
edges.append((u-1, v-1))
labels = [-1] * (n-1)
occ = compute_degrees(n, edges)
center = find_node_with_degree_at_least(occ, 3)
var = 0
if center != -1:
var = assign_incident_labels(edges, center, labels, var)
else:
leaves = find_leaves(occ)
for node in leaves[:2]:
var = assign_incident_labels(edges, node, labels, var)
labels = fill_remaining_labels(labels, var)
print('\\n'.join(str(x) for x in labels))
if __name__ == "__main__":
main()`
},
time1: {
library_prev: `
from collections import deque
import heapq
import sys
def read_tree():
n = int(input())
edges = []
degrees = [0] * n
for _ in range(n - 1):
(u, v) = map(int, input().split())
u -= 1
v -= 1
edges.append((u, v))
degrees[u] += 1
degrees[v] += 1
return (n, edges, degrees)
def find_node_by_degree(degrees, threshold):
for (i, d) in enumerate(degrees):
if d >= threshold:
return i
return None
def label_edges_at_node(edges, node, fin, start):
lab = start
for (idx, (u, v)) in enumerate(edges):
if u == node or v == node:
fin[idx] = lab
lab += 1
return lab
def find_leaves(degrees):
return [i for (i, d) in enumerate(degrees) if d == 1]
def label_edges_at_leaves(edges, leaves, fin, start):
lab = start
cnt = 0
for leaf in leaves:
if cnt >= 2:
break
for (idx, (u, v)) in enumerate(edges):
if leaf == u or leaf == v:
fin[idx] = lab
lab += 1
cnt += 1
break
return lab
def label_remaining(fin, start):
lab = start
for i in range(len(fin)):
if fin[i] == -1:
fin[i] = lab
lab += 1
return fin
`,
library_next: `
import heapq
from collections import deque
import sys
def read_tree():
n = int(input())
edges = []
degrees = [0] * n
for _ in range(n - 1):
(u, v) = map(int, input().split())
u -= 1
v -= 1
edges.append((u, v))
degrees[u] += 1
degrees[v] += 1
return (n, edges, degrees)
def find_node_by_degree(degrees, threshold):
for (i, d) in enumerate(degrees):
if d >= threshold:
return i
return None
def label_edges_at_node(edges, node, fin, start):
lab = start
for (idx, (u, v)) in enumerate(edges):
if u == node or v == node:
fin[idx] = lab
lab += 1
return lab
def find_leaves(degrees):
return [i for (i, d) in enumerate(degrees) if d == 1]
def label_edges_at_leaves(edges, leaves, fin, start):
lab = start
cnt = 0
for leaf in leaves:
if cnt >= 2:
break
for (idx, (u, v)) in enumerate(edges):
if leaf == u or leaf == v:
fin[idx] = lab
lab += 1
cnt += 1
break
return lab
def label_remaining(fin, start):
lab = start
for i in range(len(fin)):
if fin[i] == -1:
fin[i] = lab
lab += 1
return fin
def read_sequence():
n = int(input())
return list(map(int, input().split()))
def compute_kill_steps(seq):
stack = []
max_steps = 0
for x in seq:
steps = 0
while stack and x >= stack[-1][0]:
steps = max(steps, stack[-1][1])
stack.pop()
if stack:
steps += 1
else:
steps = 0
max_steps = max(max_steps, steps)
stack.append((x, steps))
return max_steps
`,
solution_prev: `n, t = int(input()), list(map(int, input().split()))
p, s, r = [0] * n, [0] * n, t[0]
for i in range(n - 1):
j = i + 1
x = t[j]
if x > r: r = x
else:
while t[i] < x: s[j], i = max(s[j], s[i]), p[i]
p[j] = i
s[j] += 1
print(max(s))`,
solution_next: `from library import *
def main():
n = int(input())
arr = list(map(int, input().split()))
print(compute_steps(arr))
if __name__ == "__main__":
main()`
},
time2: {
library_prev: `
import heapq
from collections import deque
import sys
def read_tree():
n = int(input())
edges = []
degrees = [0] * n
for _ in range(n - 1):
(u, v) = map(int, input().split())
u -= 1
v -= 1
edges.append((u, v))
degrees[u] += 1
degrees[v] += 1
return (n, edges, degrees)
def find_node_by_degree(degrees, threshold):
for (i, d) in enumerate(degrees):
if d >= threshold:
return i
return None
def label_edges_at_node(edges, node, fin, start):
lab = start
for (idx, (u, v)) in enumerate(edges):
if u == node or v == node:
fin[idx] = lab
lab += 1
return lab
def find_leaves(degrees):
return [i for (i, d) in enumerate(degrees) if d == 1]
def label_edges_at_leaves(edges, leaves, fin, start):
lab = start
cnt = 0
for leaf in leaves:
if cnt >= 2:
break
for (idx, (u, v)) in enumerate(edges):
if leaf == u or leaf == v:
fin[idx] = lab
lab += 1
cnt += 1
break
return lab
def label_remaining(fin, start):
lab = start
for i in range(len(fin)):
if fin[i] == -1:
fin[i] = lab
lab += 1
return fin
def read_sequence():
n = int(input())
return list(map(int, input().split()))
def compute_kill_steps(seq):
stack = []
max_steps = 0
for x in seq:
steps = 0
while stack and x >= stack[-1][0]:
steps = max(steps, stack[-1][1])
stack.pop()
if stack:
steps += 1
else:
steps = 0
max_steps = max(max_steps, steps)
stack.append((x, steps))
return max_steps
`,
library_next: `
from collections import deque
from collections import defaultdict
import heapq
import sys
def read_tree():
n = int(input())
edges = []
degrees = [0] * n
for _ in range(n - 1):
(u, v) = map(int, input().split())
u -= 1
v -= 1
edges.append((u, v))
degrees[u] += 1
degrees[v] += 1
return (n, edges, degrees)
def find_node_by_degree(degrees, threshold):
for (i, d) in enumerate(degrees):
if d >= threshold:
return i
return None
def label_edges_at_node(edges, node, fin, start):
lab = start
for (idx, (u, v)) in enumerate(edges):
if u == node or v == node:
fin[idx] = lab
lab += 1
return lab
def find_leaves(degrees):
return [i for (i, d) in enumerate(degrees) if d == 1]
def label_edges_at_leaves(edges, leaves, fin, start):
lab = start
cnt = 0
for leaf in leaves:
if cnt >= 2:
break
for (idx, (u, v)) in enumerate(edges):
if leaf == u or leaf == v:
fin[idx] = lab
lab += 1
cnt += 1
break
return lab
def label_remaining(fin, start):
lab = start
for i in range(len(fin)):
if fin[i] == -1:
fin[i] = lab
lab += 1
return fin
def read_sequence():
n = int(input())
return list(map(int, input().split()))
def compute_kill_steps(seq):
stack = []
max_steps = 0
for x in seq:
steps = 0
while stack and x >= stack[-1][0]:
steps = max(steps, stack[-1][1])
stack.pop()
if stack:
steps += 1
else:
steps = 0
max_steps = max(max_steps, steps)
stack.append((x, steps))
return max_steps
def build_adj_list(n, edges):
adj = [[] for _ in range(n)]
for (u, v) in edges:
adj[u].append(v)
adj[v].append(u)
return adj
def dfs_subtree_size(node, parent, adj, sizes):
total = 1
for nei in adj[node]:
if nei != parent:
total += dfs_subtree_size(nei, node, adj, sizes)
sizes[node] = total
return total
def compute_subtree_sizes(n, adj):
sizes = [0] * n
dfs_subtree_size(0, -1, adj, sizes)
return sizes
`,
solution_prev: `from collections import defaultdict
import threading
from sys import stdin,setrecursionlimit
setrecursionlimit(300000)
input=stdin.readline
def dfs(node,g,par,sz):
for i in g[node]:
if i!=par:
sz[node]+=dfs(i,g,node,sz)
return sz[node]+1
def main():
n=int(input())
if n%2!=0:
print(-1)
exit(0)
g=defaultdict(list)
for i in range(n-1):
x,y=map(int,input().strip().split())
g[x-1].append(y-1)
g[y-1].append(x-1)
sz=[0]*(n)
tt=[]
dfs(0,g,-1,sz)
res=0
for i in range(1,n):
if sz[i]%2!=0:
res+=1
print(res)
threading.stack_size(10 ** 8)
t = threading.Thread(target=main)
t.start()
t.join()`,
solution_next: `from library import *
import sys
def main():
sys.setrecursionlimit(10**7)
input = sys.stdin.readline
n = int(input())
if n & 1:
print(-1)
return
edges = []
for _ in range(n-1):
u, v = map(int, input().split())
edges.append((u-1, v-1))
graph = build_adj_list(n, edges)
sizes = compute_subtree_sizes(n, graph)
res = 0
for i in range(1, n):
if sizes[i] % 2 != 0:
res += 1
print(res)
if __name__ == "__main__":
main()`
}
};
}
async start() {
this.isRunning = true;
this.updateStatus('loading', 'Initializing...');
while (this.isRunning && this.currentTimestep < this.maxTimesteps) {
await this.processTimestep(this.currentTimestep);
this.currentTimestep++;
await this.delay(1000); // 1 second pause between timesteps
}
this.updateStatus('complete', 'Animation Complete');
}
async processTimestep(timestep) {
this.timestepCountEl.textContent = timestep + 1;
const timestepKey = `time${timestep}`;
// Load initial content for both panels
this.updateStatus('loading', 'Loading timestep files...');
const libraryPrev = this.sampleData[timestepKey].library_prev;
const libraryNext = this.sampleData[timestepKey].library_next;
const solutionPrev = this.sampleData[timestepKey].solution_prev;
const solutionNext = this.sampleData[timestepKey].solution_next;
// Update file names
this.libraryFileName.textContent = 'library.py';
this.solutionFileName.textContent = `solution${timestep + 1}.py`;
// Show initial state (prev versions)
this.displayCode(libraryPrev, this.libraryContent);
this.displayCode(solutionPrev, this.solutionContent);
await this.delay(500);
// Phase 1: Animate library changes
this.updateStatus('library', 'Updating library.py');
await this.animateDiff(libraryPrev, libraryNext, this.libraryContent);
await this.delay(500);
// Phase 2: Animate solution changes
this.updateStatus('solution', `Updating solution${timestep + 1}.py`);
await this.animateDiff(solutionPrev, solutionNext, this.solutionContent);
await this.delay(200);
}
displayCode(content, container, highlightedLines = []) {
const lines = content.split('\n');
container.innerHTML = lines.map((line, i) => {
const lineNum = i + 1;
const isHighlighted = highlightedLines.includes(lineNum);
return `
<div class="code-line ${isHighlighted ? 'deleted' : ''}" data-line="${lineNum}">
<div class="line-number">${lineNum}</div>
<div class="line-content">${this.applySyntaxHighlighting(line)}</div>
</div>
`;
}).join('');
}
applySyntaxHighlighting(code) {
const escaped = this.escapeHtml(code);
const tokens = this.tokenize(escaped);
return this.renderTokens(tokens);
}
tokenize(code) {
const tokens = [];
let i = 0;
while (i < code.length) {
let matched = false;
// Check for triple-quoted strings first (highest priority)
const tripleQuoteMatch = code.slice(i).match(/^("""[\s\S]*?"""|'''[\s\S]*?''')/);
if (tripleQuoteMatch) {
tokens.push({ type: 'docstring', value: tripleQuoteMatch[1] });
i += tripleQuoteMatch[1].length;
matched = true;
continue;
}
// Check for comments
const commentMatch = code.slice(i).match(/^#.*$/m);
if (commentMatch) {
tokens.push({ type: 'comment', value: commentMatch[0] });
i += commentMatch[0].length;
matched = true;
continue;
}
// Check for string literals
const stringMatch = code.slice(i).match(/^(["'])((?:[^\\]|\\.)*)(\1)/);
if (stringMatch) {
tokens.push({ type: 'string', value: stringMatch[0] });
i += stringMatch[0].length;
matched = true;
continue;
}
// Check for numbers
const numberMatch = code.slice(i).match(/^\d+\.?\d*/);
if (numberMatch) {
tokens.push({ type: 'number', value: numberMatch[0] });
i += numberMatch[0].length;
matched = true;
continue;
}
// Check for keywords
const keywordMatch = code.slice(i).match(/^(def|class|import|from|if|else|elif|for|while|return|try|except|finally|with|as|in|and|or|not|is|lambda|yield|async|await|pass|break|continue|raise|assert|del|global|nonlocal|True|False|None)\b/);
if (keywordMatch) {
tokens.push({ type: 'keyword', value: keywordMatch[1] });
i += keywordMatch[1].length;
matched = true;
continue;
}
// Check for function calls
const functionMatch = code.slice(i).match(/^([a-zA-Z_][a-zA-Z0-9_]*)(?=\s*\()/);
if (functionMatch) {
tokens.push({ type: 'function', value: functionMatch[1] });
i += functionMatch[1].length;
matched = true;
continue;
}
// If no pattern matched, add the character as plain text
if (!matched) {
tokens.push({ type: 'plain', value: code[i] });
i++;
}
}
return tokens;
}
renderTokens(tokens) {
return tokens.map(token => {
switch (token.type) {
case 'docstring':
return `<span class="docstring">${token.value}</span>`;
case 'string':
return `<span class="string">${token.value}</span>`;
case 'comment':
return `<span class="comment">${token.value}</span>`;
case 'keyword':
return `<span class="keyword">${token.value}</span>`;
case 'number':
return `<span class="number">${token.value}</span>`;
case 'function':
return `<span class="function">${token.value}</span>`;
default:
return token.value;
}
}).join('');
}
async animateDiff(prevContent, nextContent, container) {
const prevLines = prevContent.split('\n');
const nextLines = nextContent.split('\n');
// Animate the diff with insertions and deletions
await this.animateFullDiff(prevLines, nextLines, container);
}
async animateFullDiff(prevLines, nextLines, container) {
// Start by showing the previous version
this.displayCode(prevLines.join('\n'), container);
await this.delay(1000);
// Two-sweep algorithm: delete all non-matching lines, then add all new lines
let currentLines = [...prevLines];
const nextSet = new Set(nextLines);
// SWEEP 1: Delete all lines that don't appear in next (from end to start)
// Process deletions one by one to maintain correct indexing
const linesToDelete = [];
for (let i = 0; i < currentLines.length; i++) {
if (!nextSet.has(currentLines[i])) {
linesToDelete.push(i);
}
}
// Delete from end to start to maintain correct indices
for (let idx = linesToDelete.length - 1; idx >= 0; idx--) {
const lineIndex = linesToDelete[idx];
const lineContent = currentLines[lineIndex];
const tokens = this.tokenizeLine(lineContent);
// Scroll to the line being deleted (adjust for already deleted lines)
const displayLineNum = lineIndex + 1;
this.scrollToLine(displayLineNum, container);
// Highlight line for deletion
const lineEl = container.querySelector(`[data-line="${displayLineNum}"]`);
if (lineEl) {
lineEl.classList.add('deleted');
}
// Delete tokens from end to start
for (let tokenIdx = tokens.length - 1; tokenIdx >= 0; tokenIdx--) {
// Rebuild the line without the last tokens
const remainingTokens = tokens.slice(0, tokenIdx);
currentLines[lineIndex] = remainingTokens.join('');
// Update display with highlighted line
const highlightedLines = [displayLineNum];
this.displayCode(currentLines.join('\n'), container, highlightedLines);
// Token delay (same as insertion)
const delay = this.getTokenDelay(tokens[tokenIdx]);
await this.delay(delay);
}
// Remove the empty line
currentLines.splice(lineIndex, 1);
this.displayCode(currentLines.join('\n'), container);
await this.delay(25);
}
// Brief pause between sweeps
await this.delay(300);
// SWEEP 2: Add all lines from next in their correct positions
// Build the final result line by line
for (let targetIdx = 0; targetIdx < nextLines.length; targetIdx++) {
const targetLine = nextLines[targetIdx];
// Check if this line already exists at the correct position
if (targetIdx < currentLines.length && currentLines[targetIdx] === targetLine) {
// Line is already in the right place, skip
continue;
}
// Find if this line exists elsewhere and remove it
for (let i = targetIdx + 1; i < currentLines.length; i++) {
if (currentLines[i] === targetLine) {
currentLines.splice(i, 1);
break;
}
}
// Now insert the line at the correct position
const tokens = this.tokenizeLine(targetLine);
let currentLine = '';
// Insert empty line at position
currentLines.splice(targetIdx, 0, '');
this.displayCode(currentLines.join('\n'), container);
// Animate each token
for (const token of tokens) {
currentLine += token;
currentLines[targetIdx] = currentLine;
// Update display
this.displayCode(currentLines.join('\n'), container);
// Highlight the line being written
const lineEl = container.querySelector(`[data-line="${targetIdx + 1}"]`);
if (lineEl) {
lineEl.classList.add('added');
this.scrollToLine(targetIdx + 1, container);
}
// Token delay
const delay = this.getTokenDelay(token);
await this.delay(delay);
}
// Remove highlight after line is complete
const lineEl = container.querySelector(`[data-line="${targetIdx + 1}"]`);
if (lineEl) {
setTimeout(() => {
lineEl.classList.remove('added');
}, 400);
}
await this.delay(25);
}
}
async writeNewContent(lines, container) {
let currentLines = [];
for (let i = 0; i < lines.length; i++) {
const targetLine = lines[i];
const tokens = this.tokenizeLine(targetLine);
let currentLine = '';
// Add empty line placeholder
currentLines.push('');
// Write tokens one by one
for (const token of tokens) {
currentLine += token;
currentLines[i] = currentLine;
// Update display
this.displayCode(currentLines.join('\n'), container);
// Highlight the line being written
const lineEl = container.querySelector(`[data-line="${i + 1}"]`);
if (lineEl) {
lineEl.classList.add('added');
this.scrollToLine(i + 1, container);
}
// Token delay
const delay = this.getTokenDelay(token);
await this.delay(delay);
}
// Remove highlight after line is complete
const lineEl = container.querySelector(`[data-line="${i + 1}"]`);
if (lineEl) {
setTimeout(() => {
lineEl.classList.remove('added');
}, 300);
}
// Pause between lines
await this.delay(25);
}
}
countCommonLines(prevLines, nextLines) {
const nextSet = new Set(nextLines);
return prevLines.filter(line => nextSet.has(line)).length;
}
async animateTokenByToken(prevLines, nextLines, container) {
// Use LCS (Longest Common Subsequence) based approach for proper diff
const diff = this.computeDiff(prevLines, nextLines);
let currentLines = [...prevLines];
let lineOffset = 0;
for (const change of diff) {
if (change.type === 'insert') {
// Calculate actual insertion position with offset
const insertPos = change.position + lineOffset;
const targetLine = change.line;
const tokens = this.tokenizeLine(targetLine);
let currentLine = '';
// Insert empty line at the position
currentLines.splice(insertPos, 0, '');
lineOffset++;
// Animate each token
for (const token of tokens) {
currentLine += token;
currentLines[insertPos] = currentLine;
// Update display
this.displayCode(currentLines.join('\n'), container);
// Highlight the line being written
const lineEl = container.querySelector(`[data-line="${insertPos + 1}"]`);
if (lineEl) {
lineEl.classList.add('added');
this.scrollToLine(insertPos + 1, container);
}
// Token delay
const delay = this.getTokenDelay(token);
await this.delay(delay);
}
// Remove highlight after line is complete
const lineEl = container.querySelector(`[data-line="${insertPos + 1}"]`);
if (lineEl) {
setTimeout(() => {
lineEl.classList.remove('added');
}, 400);
}
// Pause between lines
await this.delay(25);
}
}
}
computeDiff(prevLines, nextLines) {
// Simple diff algorithm that finds insertions
const changes = [];
let prevIdx = 0;
let nextIdx = 0;
while (nextIdx < nextLines.length) {
if (prevIdx < prevLines.length && prevLines[prevIdx] === nextLines[nextIdx]) {
// Lines match, advance both
prevIdx++;
nextIdx++;
} else {
// Check if this line exists later in prev
let found = false;
for (let searchIdx = prevIdx; searchIdx < prevLines.length; searchIdx++) {
if (prevLines[searchIdx] === nextLines[nextIdx]) {
// Found it later - insert all new lines before it
found = true;
break;
}
}
if (!found) {
// This is a new line to insert
changes.push({
type: 'insert',
position: prevIdx,
line: nextLines[nextIdx]
});
nextIdx++;
} else {
// Skip lines in prev that don't match current next
prevIdx++;
}
}
}
return changes;
}
tokenizeLine(line) {
// Split line into meaningful tokens
const tokens = [];
const regex = /(\s+|[a-zA-Z_]\w*|[0-9]+|\S)/g;
let match;
while ((match = regex.exec(line)) !== null) {
tokens.push(match[0]);
}
return tokens;
}
getTokenDelay(token) {
// Variable delays for different token types (faster speed)
if (token.match(/^\s+$/)) return 5; // Whitespace
if (token.match(/^[(){}\[\]]$/)) return 12; // Brackets
if (token.match(/^[a-zA-Z_]\w*$/)) return 10; // Identifiers
if (token.match(/^[0-9]+$/)) return 10; // Numbers
return 8; // Default
}
calculateLineDiff(prevLines, nextLines) {
const changes = [];
const prevSet = new Set(prevLines);
// Find where to insert new lines by comparing sequences
let prevIndex = 0;
for (let nextIndex = 0; nextIndex < nextLines.length; nextIndex++) {
const line = nextLines[nextIndex];