-
Notifications
You must be signed in to change notification settings - Fork 0
/
getPINs.js
77 lines (57 loc) · 1.87 KB
/
getPINs.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
/*
key pad
┌───┬───┬───┐
│ 1 │ 2 │ 3 │
├───┼───┼───┤
│ 4 │ 5 │ 6 │
├───┼───┼───┤
│ 7 │ 8 │ 9 │
└───┼───┼───┘
│ 0 │
└───┘
hmm this is a data structure question
how to organize the un symetrical relationship between possible outcomes?
also a combinatorics issue (n chose k), can I do something with binary?
- [ ] solve again without using flatmap
- [ ] solve in the stupid order the kata prefers
eg:
'8': ['5', '7', '8', '9', '0']
'11': ['11', '22', '44', '12', '21', '14', '41', '24', '42']
*/
// const getNumbers = pin => [...pin].map(n => numbers[Number(n)].map(i => i));
// const getPins0 = pin => {
// const numbers = [
// [0, 8], [1, 2, 4], [2, 1, 3, 5], [3, 2, 6], [4, 1, 5, 7],
// [5, 2, 4, 6, 8], [6, 3, 5, 9], [7, 4, 8], [8, 0, 5, 7, 9], [9, 6, 8],
// ];
// const possibleKeys = [...pin].map(n => numbers[Number(n)]);
// const totalComb = possibleKeys.reduce((product, current) => product * current.length, 1);
// const combinations = [...Array(totalComb).keys()].map(i => possibleKeys
// .reduce((s, c) => {
// console.log(c[~~(i/(totalComb/c.length))])
// return s.concat(c[ ~~(i/(totalComb/c.length)) ])
// }, [])
// );
// return combinations;
// };
const getPins = pin => {
const numbers = [
[0, 8],
[1, 2, 4],
[2, 1, 3, 5],
[3, 2, 6],
[4, 1, 5, 7],
[5, 2, 4, 6, 8],
[6, 3, 5, 9],
[7, 4, 8],
[8, 0, 5, 7, 9],
[9, 6, 8],
];
const possibleKeys = [...pin].map(n => numbers[Number(n)]);
const calc = (comb = [''], values) => values.length ? calc(comb.flatMap(i => values[0].map(j => i + j)), values.slice(1)) : comb;
return calc([''], possibleKeys);
};
// console.log(getPins('8'));
// console.log(getPins('10'));
// console.log(getPins('11'));
console.log(getPins0('369'));