-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path4-kyu-the-observed-pin.js
176 lines (156 loc) · 4.68 KB
/
4-kyu-the-observed-pin.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
// 4 kyu | The observed PIN
// https://www.codewars.com/kata/the-observed-pin/
/*
Alright, detective, one of our colleagues successfully observed our target
person, Robby the robber. We followed him to a secret warehouse, where we
assume to find all the stolen stuff. The door to this warehouse is secured
by an electronic combination lock. Unfortunately our spy isn't sure about
the PIN he saw, when Robby entered it.
The keypad has the following layout:
+---+---+---+
│ 1 │ 2 │ 3 │
+---+---+---+
│ 4 │ 5 │ 6 │
+---+---+---+
│ 7 │ 8 │ 9 │
+---+---+---+
│ 0 │
+---+
He noted the PIN 1357, but he also said, it is possible that each of the
digits he saw could actually be another adjacent digit (horizontally or
vertically, but not diagonally). E.g. instead of the 1 it could also
be the 2 or 4. And instead of the 5 it could also be the 2, 4, 6 or 8.
He also mentioned, he knows this kind of locks. You can enter an unlimited
amount of wrong PINs, they never finally lock the system or sound the alarm.
That's why we can try out all possible (*) variations.
* possible in sense of: the observed PIN itself and all variations considering
the adjacent digits
Can you help us to find all those variations? It would be nice to have a
function, that returns an array (or a list in Java and C#) of all variations
for an observed PIN with a length of 1 to 8 digits. We could name the
function getPINs (get_pins in python, GetPINs in C#). But please note that
all PINs, the observed one and also the results, must be strings, because
of potentially leading '0's. We already prepared some test cases for you.
Detective, we count on you!
*/
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// const combinations = a =>
// a.reduce((acc, curr) =>
// acc
// .map(head => curr.map(next => `${head}${next}`))
// .reduce((flattened, item) => flattened.concat(item)),
// );
// const getPINs = observed =>
// combinations(
// [...observed].map(
// d =>
// [
// ['0', '8'],
// ['1', '4', '2'],
// ['2', '5', '1', '3'],
// ['3', '6', '2'],
// ['4', '1', '7', '5'],
// ['5', '2', '8', '4', '6'],
// ['6', '3', '9', '5'],
// ['7', '4', '8'],
// ['8', '5', '0', '7', '9'],
// ['9', '6', '8'],
// ][d],
// ),
// );
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
const getPINs = observed =>
[...observed]
.map(
d =>
[
['0', '8'],
['1', '4', '2'],
['2', '5', '1', '3'],
['3', '6', '2'],
['4', '1', '7', '5'],
['5', '2', '8', '4', '6'],
['6', '3', '9', '5'],
['7', '4', '8'],
['8', '5', '0', '7', '9'],
['9', '6', '8'],
][d],
)
.reduce((acc, curr) =>
acc
.map(head => curr.map(next => head + next))
.reduce((flattened, arr) => flattened.concat(arr)),
);
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
import { deepStrictEqual } from 'assert';
// console.log(getPINs('8'));
deepStrictEqual(getPINs('8').sort(), ['5', '7', '8', '9', '0'].sort());
// console.log(getPINs('11'));
deepStrictEqual(
getPINs('11').sort(),
['11', '22', '44', '12', '21', '14', '41', '24', '42'].sort(),
);
// console.log(getPINs('369'));
deepStrictEqual(
getPINs('369').sort(),
[
'339',
'366',
'399',
'658',
'636',
'258',
'268',
'669',
'668',
'266',
'369',
'398',
'256',
'296',
'259',
'368',
'638',
'396',
'238',
'356',
'659',
'639',
'666',
'359',
'336',
'299',
'338',
'696',
'269',
'358',
'656',
'698',
'699',
'298',
'236',
'239',
].sort(),
);
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// const getPossibilities = () => {
// const pad = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [, 0]];
// const moves = [[-1, 0], [1, 0], [0, -1], [0, 1]];
// const possibilities = new Array(10).fill().map(u => []);
// for (let i = 0; i < 4; i++) {
// for (let j = 0; j < 3; j++) {
// if ('undefined' === typeof pad[i][j]) continue;
// possibilities[pad[i][j]].push(pad[i][j]);
// for (let k = 0; k < 4; k++) {
// if (
// 'undefined' !== typeof pad[i + moves[k][0]] &&
// 'undefined' !== typeof pad[i + moves[k][0]][j + moves[k][1]]
// ) {
// possibilities[pad[i][j]].push(pad[i + moves[k][0]][j + moves[k][1]]);
// }
// }
// }
// }
// return possibilities;
// };
// console.log(getPossibilities());