-
Notifications
You must be signed in to change notification settings - Fork 0
/
nine.js
178 lines (159 loc) · 4.06 KB
/
nine.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
/*
* Javascript solution to the nine wives riddle found in Sacred Odyssey.
*
* There are 9 fire pits which needs all to be set on fire. Initially only
* the middle one is set on fire. If you set fire on a pit all horizontally
* and vertically ones will toggle their state (only adjacent).
*
* One solution is [1,3,7,5,8,6,0,2]
*/
"use strict";
var board =
[ [{position: 0, status: "off"}, {position: 1, status : "off"}, {position: 2, status: "off"}],
[{position: 3, status: "off"}, {position: 4, status : "on"}, {position: 5, status: "off"}],
[{position: 6, status: "off"}, {position: 7, status : "off"}, {position: 8, status: "off"}]];
function verify(board, moves) {
for (var i=0; i<moves.length; i++) {
fireOn(board, moves[i]);
printBoard(board);
}
}
/**
* This simply runs the solve methods and just keeps
* the solution with the lowest moves.
* @param board intial board
* @param how many runy to try
*/
function minimum(board, runs) {
var m = solve(copy(board));
console.log("Starting to run " + runs + " times");
for (var i=0; i<runs; i++) {
var b = copy(board);
var mnew = solve(b);
if (mnew.length < m.length) m = mnew;
}
console.log("Local minimum found " + m);
}
/**
* Copy the board.
* @param board
* @return new board
*/
function copy(board) {
return JSON.parse(JSON.stringify(board));
}
/**
* Solves the riddle by trial.
* @param board
* @return array of moves of the solution
*/
function solve(board) {
var moves = [];
var candidates = cells(board).filter(candidate);
while (candidates.length > 0) {
var index = getRandomInt(candidates.length - 1);
var nextMoveCell = candidates[index];
// console.log("Found #candidates " + c + " index " + index + ", trying position " + nextMove);
moves.push(nextMoveCell.position);
fireOn(board, nextMoveCell.position);
candidates = cells(board).filter(candidate);
}
console.log("Solution found");
console.log("Moves " + moves);
printBoard(board);
return moves;
}
function fireOn(board, position) {
if (position === undefined) return;
switch (position) {
case 0:
return toggle(board, [0,1,3]);
case 1:
return toggle(board, [0,1,2,4]);
case 2:
return toggle(board, [1,2,5]);
case 3:
return toggle(board, [3,4,0,6]);
case 4:
return toggle(board, [3,4,5,1,7]);
case 5:
return toggle(board, [4,5,2,8]);
case 6:
return toggle(board, [6,7,3]);
case 7:
return toggle(board, [6,7,8,4]);
case 8:
return toggle(board, [7,8,5]);
default:
return console.log("switch default");
}
};
// Returns a random integer between min and max
// Using Math.round() will give you a non-uniform distribution!
function getRandomInt(max) {
return Math.floor(Math.random() * (max + 1));
}
/**
* How many pits are not on fire.
* @param board
* @return array of positions where the fire is off
*/
function candidatess(board) {
var c = [];
cells(board).forEach(isCandidate);
for (var i=0; i<3; i++) {
for (var j=0; j<3; j++) {
if (board[i][j].status === "off") c.push(board[i][j].position);
}
}
return c;
}
function candidate(cell) {
return (cell.status === "off");
}
function position2index(position) {
var i = Math.floor(position / 3);
var j = position % 3;
return { i:i, j:j };
}
function toggle(board, positions) {
for (var i=0; i<positions.length; i++) {
var index = position2index(positions[i]);
onOff(board[index.i][index.j]);
}
}
/**
* Changes the state of the cell.
* @param cell
*/
function onOff(cell) {
if (cell.status === "off") {
cell.status = "on";
} else {
cell.status = "off";
}
};
/**
* Prints the board in a human readable format.
* @param board
*/
function printBoard(board) {
var b = "";
for (var i=0; i<3; i++) {
for (var j=0; j<3; j++) {
b += board[i][j].status;
if (j < 2 ) b += ",";
}
b += '\n';
}
console.log(b);
}
function cells(board) {
var cells = [];
for (var i=0; i<3; i++) {
for (var j=0; j<3; j++) {
cells.push(board[i][j]);
}
}
return cells;
}