-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path05-a.js
53 lines (51 loc) · 1.2 KB
/
05-a.js
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
class List {
constructor (string) {
this.list = {
value: 'head',
next: null
}
let node = this.list
for (let i = 0; i < string.length; i++) {
node.next = { value: units[i], next: null, previous: node }
node = node.next
}
}
react () {
let node = this.list.next
while (true) {
if (!node.next) {
break
}
if (node.value.toUpperCase() === node.next.value.toUpperCase()) {
if (node.value !== node.next.value) {
node = node.previous
this.remove(node.next)
this.remove(node.next)
continue
}
}
node = node.next
}
}
remove (node) {
node.previous.next = node.next
if (node.next) {
node.next.previous = node.previous
}
}
toString () {
const result = []
let node = this.list.next
while (node) {
result.push(node.value)
node = node.next
}
return result.join('')
}
}
let units = require('fs').readFileSync(`inputData/05-polymer.txt`, `utf-8`)
const list = new List(units)
console.log(`Starting length: ${list.toString().length}`)
list.react()
console.log(list.toString())
console.log(`Ending length: ${list.toString().length}`)