-
Notifications
You must be signed in to change notification settings - Fork 0
/
carpet.js
94 lines (85 loc) · 2.38 KB
/
carpet.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
//2d array for room
const roomSize = 6;
let room = new Array(roomSize).fill(null).map(() => new Array(roomSize).fill(null));
room[0][0] = 6;
room[1][3] = 4;
room[2][3] = 6;
room[3][4] = 6;
room[4][0] = 4;
room[4][1] = 4;
room[5][5] = 6;
//List of available figures
const figures = [
{letter: 'a', color: '#6eacaa', w: 2, h: 2},
{letter: 'b', color: '#bc7b33', w: 1, h: 6},
{letter: 'c', color: '#a6a623', w: 1, h: 4},
{letter: 'd', color: '#6a679e', w: 3, h: 2},
{letter: 'e', color: '#965186', w: 2, h: 3},
{letter: 'f', color: '#b5313f', w: 4, h: 1},
{letter: 'g', color: '#6e9b5b', w: 1, h: 6},
];
function rotateFigure(figure){
return Object.assign(Object.assign({}, figure), {w: figure.h, h: figure.w});
}
function printRoom(room){
console.log(room.map(row => row.map(item => item || '.').join('')).join("\n"));
}
function deepCopy(room){
return JSON.parse(JSON.stringify(room));
}
function canPlace(room, figure, at){
const area = figure.w * figure.h;
let row, col;
if(at.row + figure.h > roomSize || at.col + figure.w > roomSize){
return false;
}
for(row = at.row; row < at.row + figure.h; row++){
for(col = at.col; col < at.col + figure.w; col++){
if(![null, area].includes(room[row][col])){
return false;
}
}
}
return true;
}
function placeFigure(room, figure, at){
let newRoom = deepCopy(room);
for(row = at.row; row < at.row + figure.h; row++){
for(col = at.col; col < at.col + figure.w; col++){
newRoom[row][col] = figure.letter;
}
}
return newRoom;
}
let count = 0;
function main(room, figures){
let i, row, col;
//printRoom(room);
//console.log('');
count++;
if(!figures.length){
printRoom(room);
return;
}
for(let i = 0; i < figures.length; i++){
let fig = figures[i];
for(row = 0; row <= roomSize - fig.h; row++){
for(col = 0; col <= roomSize - fig.w; col++){
let at = {row, col};
if(canPlace(room, fig, at)){
let newRoom = placeFigure(room, fig, at);
main(newRoom, figures.filter((item, index) => index != i));
}
if(fig.w != fig.h){
let rotated = rotateFigure(fig);
if(canPlace(room, rotated, at)){
let newRoom = placeFigure(room, rotated, at);
main(newRoom, figures.filter((item, index) => index != i));
}
}
}
}
}
}
main(room, figures);
console.log(count);