-
Notifications
You must be signed in to change notification settings - Fork 0
/
lightBulbs.js
executable file
·98 lines (77 loc) · 3.38 KB
/
lightBulbs.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
let bulbs = [];
/* Integer SIZE. How many things will be in the array.
// Thing TYPE is string, boolean, null, whatever. It's what you want stuck into each member.
// Function POPULATEBULBS(size, type) . Returns an array. */
let populateBulbs =(size, type)=>{
let things = [];
for( ; size > 0; size--){
// offset b/c zero indexed
things[size -1] = type;
}
return things;
}
// bulbsList Array of booleans. A list of the incoming bulbs
// startedFrom Integer. The square the person started from, also the interval they advance by when they walk to next bulb
// gauntletWalk(bulbsList, startedFrom) Function. Returns the NEW array of 100 falses/trues representing the bulbs after they've been walked.
const gauntletWalk =(bulbsList, startedFrom)=>{
for(let i = startedFrom; i < bulbsList.length; i += startedFrom){
bulbsList[i] = !bulbsList[i];
}
return bulbsList;
}
// Array BULBSLIST. Array of booleans. A list of the incoming bulbs, whether lit or not.
// Function TELLNUMBERLIT(BULBSLIST). Returns number of TRUE
const tellNumberLit =(themBulbs)=>{
let onTotal =0;
for (let member of themBulbs){
if (member===true){
onTotal++;
}
}
return onTotal;
};
let showBulbs =(themBulbs)=>{
console.log(`Array is ${foo.length} long and looks like this:`)
console.log(foo);
}
// Integer WHICH choose a bulb
// Array THEMBULBS the current bulb states
// Function PEEK(THEMBULBS). Returns boolean.
let peek =(themBulbs, which)=> {
return themBulbs[which];
}
// Integer GOAL. How many bulbs you want on.
// Function REPEATSTILLSUCCESS(array, goal). Returns integer for number of gauntlet walks.
const repeatsUntilSuccess =(themBulbs, goal)=> {
//test if already true
console.log(`Started at ${new Date}`);
// Integer INVENTORY. Number of bulbs now on.
let numberOfWalks = 0;
let inventory = tellNumberLit(themBulbs);
while (inventory !== goal){
themBulbs = gauntletWalk(themBulbs, ++numberOfWalks);
inventory = tellNumberLit(themBulbs);
}
console.log(`Done at ${new Date}`);
return numberOfWalks;
}
let foo = populateBulbs(100, false);
foo = gauntletWalk(foo, 3);
showBulbs(foo);
console.log(`We found this many on ${tellNumberLit(foo)}`);
/*--------------------------------*/
const WEWANT = 51;
let cumulativeWalks = 0;
cumulativeWalks = repeatsUntilSuccess(foo, WEWANT);
console.log(`To get ${WEWANT} it took ${cumulativeWalks}...`);
/**
SWITCHING LIGHTBULBS PROBLEM
There are 100 light bulbs, labeled from 1 to 100, lined up all set to off initially. There are also 100 people each numbered 1 to 100 as well. Person 1 will go through all the light bulbs and flip the switch turning all of them on. Then person number 2 will go through all the light bulbs and flip the switch on each 2nd element turning them off, namely: light bulbs #2, #4, #6, #8, etc. Then person 3 will go and do the same for the 3rd ligh bulb, 6th, 9th, etc. Then questions are usually asked about the light bulbs, for example:
• How many light bulbs will be on after 100 people have gone through them?
• What is the status of the Nth light bulb (34th, 62nd, etc.)? Is it on or off?
• How many people need to go through the line of light bulbs until exactly K light bulbs are set
to on?
// maxFolks Integer. Final walker that will walk. Starts with the 'two walker' and increments succesive walkers by 1.
bulbsList Array of booleans. A list of the bulbs, whether lit or not.
startWalking(bulbsList, folks) Function.
*/