-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
59 lines (55 loc) · 1.23 KB
/
index.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
var ps_map_html = function(map) {
var html = '';
html += '<div style="margin:10px;">';
for(var y=0; y<map.length; y++) {
for(var x=0; x<map[0].length; x++) {
var name = map[y][x].type + '' + map[y][x].orientation;
if(x==0 && y==0) name='S';
html += '<img src="./img/pipe-'+name+'.png" alt="'+name+'" style="width:10%;" />';
}
html += '<br />';
}
html += '</div>';
return html;
};
var ps_solve = function() {
var prob = $('textarea#problem').val().trim().split('\n');
try {
var ps = new PlumberSolver(prob);
} catch(e) {
alert(e.message);
return;
}
$('#image-prob').html(ps_map_html(ps.map));
var ans = ps.solve();
console.log(ans);
if(ans.length == 0) {
alert('Oops, we chouldn\'t find your answer :(');
}
var html = '';
for(var i=0; i<ans.length; i++) {
html += '<h3>Distance: '+ans[i].distance+' ('+(i+1)+' of '+ans.length+')</h3>';
html += ps_map_html(ans[i].map);
}
$('#image-answer').html(html);
};
$(document).ready(function() {
var default_prob = [
'_LL_ILIL',
'LLI_LILL',
'IILLIILL',
'LLLLLLLI',
'ILILLIIL',
'LILLLILL',
/*
'_LILL_IL',
'ILLLIIL_',
'IILIIILI',
'LLLLLLIL',
'LILLIILL',
'LIIIIIIL',
*/
];
$('textarea#problem').val(default_prob.join('\n'));
ps_solve();
});