-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
180 lines (156 loc) · 5.32 KB
/
app.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
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
var named = require('named-regexp').named;
var QuitCommand = {
exec: function() {
Table.printOutput();
process.exit();
}
};
var MoveOntoCommand = {
exec: function(args) {
var a = parseInt(args[1]);
var b = parseInt(args[2]);
Table.moveOntoCommand(a, b);
}
};
var MoveOverCommand = {
exec: function(args) {
var a = parseInt(args[1]);
var b = parseInt(args[2]);
Table.moveOverCommand(a, b);
}
};
var PileOntoCommand = {
exec: function(args) {
var a = parseInt(args[1]);
var b = parseInt(args[2]);
Table.pileOntoCommand(a, b);
}
};
var PrintTableCommand = {
exec: function() {
Table.printOutput();
}
};
var Table = {
blocks: [],
index: {},
setLayout: function(blockCount) {
this.blocks = new Array(blockCount);
for(var i=0; i < this.blocks.length; i++) {
this.blocks[i] = [i];
this.index[i] = { blockIndex: i, stackIndex: 0 };
}
},
moveOntoCommand: function(a, b) {
var aBlockindex = this.fnBlockIndex(a);
var bBlockindex = this.fnBlockIndex(b);
var aStackIndex = this.fnStackIndex(a);
var bStackIndex = this.fnStackIndex(b);
var aBlock = this.blocks[aBlockindex];
var bBlock = this.blocks[bBlockindex];
if(a === b || aBlockindex === bBlockindex) return;
this.fnReset(aBlock, aStackIndex + 1);
this.fnReset(bBlock, bStackIndex + 1);
bBlock.push(a);
aBlock.pop();
this.index[a].stackIndex = bBlock.length-1;
this.index[a].blockIndex = bBlockindex;
},
moveOverCommand: function(a, b) {
var aBlockindex = this.fnBlockIndex(a);
var bBlockindex = this.fnBlockIndex(b);
var aStackIndex = this.fnStackIndex(a);
var bStackIndex = this.fnStackIndex(b);
var aBlock = this.blocks[aBlockindex];
var bBlock = this.blocks[bBlockindex];
if(a === b || aBlockindex === bBlockindex) return;
this.fnReset(aBlock, aStackIndex + 1);
bBlock.push(a);
aBlock.pop();
this.index[a].stackIndex = bBlock.length-1;
this.index[a].blockIndex = bBlockindex;
},
pileOntoCommand: function(a, b) {
var aBlockindex = this.fnBlockIndex(a);
var bBlockindex = this.fnBlockIndex(b);
var aStackIndex = this.fnStackIndex(a);
var bStackIndex = this.fnStackIndex(b);
var aBlock = this.blocks[aBlockindex];
var bBlock = this.blocks[bBlockindex];
if(a === b || aBlockindex === bBlockindex) return;
this.fnReset(bBlock, bStackIndex + 1);
for(var i = aStackIndex; i < aBlock.length; i++) {
var block = aBlock[i]
bBlock.push(block);
this.index[block].blockIndex = bBlockindex;
this.index[block].stackIndex = bBlock.length - 1;
}
for(var i = aStackIndex, len = aBlock.length; i < len; i++) {
aBlock.pop();
}
},
fnBlockIndex: function(block) {
return this.index[block].blockIndex;
},
fnStackIndex: function(block) {
return this.index[block].stackIndex;
},
fnReset: function(blocks, stackIndex) {
for(var i = stackIndex; i < blocks.length; i++) {
var block = blocks[i];
this.index[block] = { blockIndex: block, stackIndex: 0 };
this.blocks[block] = [block];
}
for(var i = stackIndex, len = blocks.length; i < len; i++) {
blocks.pop();
}
},
printOutput: function() {
for(var i=0; i < this.blocks.length; i++) {
console.log('%s%s', i, this.fnPrintStack(this.blocks[i]));
}
},
fnSeek: function(array, blockNumber) {
for(var i=0; i < array.length; i++) {
if(array[i] == blockNumber) return i;
}
},
fnPrintStack: function(stack) {
var s = ':';
for(var i=0; i < stack.length; i++) {
s = s + ' ' + stack[i];
}
return s;
}
};
var Program = {
readyToAcceptCommands: false,
commands: [{
cmd: QuitCommand , regex: /^quit/mi
},{ cmd: MoveOntoCommand, regex: /^move (:<block_a>[0-9]{1}[0-9]*) onto (:<block_b>[0-9]{1}[0-9]*)/mig
},{ cmd: MoveOverCommand, regex: /^move (:<block_a>[0-9]{1}[0-9]*) over (:<block_b>[0-9]{1}[0-9]*)/mig
},{ cmd: PileOntoCommand, regex: /^pile (:<block_a>[0-9]{1}[0-9]*) onto (:<block_b>[0-9]{1}[0-9]*)/mig
}],
run: function() {
var me = this;
var stdin = process.openStdin();
stdin.addListener("data", function(data) {
var cmdline = data.toString().trim();
if(!me.readyToAcceptCommands) {
var blockCount = parseInt(cmdline);
Table.setLayout(blockCount);
me.readyToAcceptCommands = true;
}
else {
for(var i = 0; i < me.commands.length; i++) {
var cmd = me.commands[i];
var args = named(cmd.regex).exec(cmdline);
if(args) {
cmd.cmd.exec(args);
}
}
}
});
}
};
Program.run();