File tree 1 file changed +60
-0
lines changed
1 file changed +60
-0
lines changed Original file line number Diff line number Diff line change
1
+ const fs = require("fs")
2
+ let input = fs.readFileSync("index.txt").toString().split("\n")
3
+
4
+ class Queue {
5
+ constructor() {
6
+ this._arr = []
7
+ }
8
+ enqueue(item) {
9
+ this._arr.push(item)
10
+ }
11
+ dequeue() {
12
+ return this._arr.shift()
13
+ }
14
+ getLength() {
15
+ return this._arr.length
16
+ }
17
+ }
18
+
19
+
20
+ let [s, t] = input[0].split(" ").map(Number)
21
+
22
+
23
+ let queue=new Queue();
24
+
25
+ queue.enqueue([s,""]);
26
+
27
+ let visited=new Set([s]);
28
+ let found=false;
29
+
30
+ if(s==t){
31
+ return console.log(0)
32
+ }
33
+
34
+ while(queue.getLength() !=0){
35
+ let [value,opers]=queue.dequeue();
36
+
37
+ if(value>1e9){
38
+ continue;
39
+ }
40
+
41
+ if(value==t){
42
+ found=true;
43
+ return console.log(opers)
44
+ }
45
+ for(let oper of ["*","+","-","/"]){
46
+ let nextValue=value;
47
+ if(oper=="*") nextValue*=value;
48
+ if(oper=="+") nextValue+=value;
49
+ if(oper=="-") nextValue-=value;
50
+ if(oper=="/"&&value !=0) nextValue=1;
51
+ if(!visited.has(nextValue)){
52
+ queue.enqueue([nextValue,opers+oper])
53
+ visited.add(nextValue)
54
+ }
55
+
56
+ }
57
+
58
+ }
59
+
60
+ if(!found)console.log(-1)
You can’t perform that action at this time.
0 commit comments