-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy path4.js
More file actions
82 lines (71 loc) · 2.61 KB
/
4.js
File metadata and controls
82 lines (71 loc) · 2.61 KB
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
Number.prototype.mod = function (n) {
'use strict'
return ((this % n) + n) % n
}
/**
* @param {string} firstA A first sentence (containing every letter from A to Z) in the alphabet A
* @param {string} firstB The same sentence as firstA, translated in the alphabet B
* @param {string} secondB A second sentence (containing every letter from A to Z) in the alphabet B
* @param {string} secondC The same sentence as secondB, translated in the alphabet C
* @param {string} secretC The secret message in alphabet C to be deciphered
* @return {string} The message secretC translated to the alphabet A
*/
function decrypt(firstA, firstB, secondB, secondC, secretC) {
// Write your code here
const atob = {}
const btoc = {}
for (let i = 0; i < firstA.length; i++) {
const a = firstA.charAt(i)
const b = firstB.charAt(i)
atob[b] = a.charCodeAt() - b.charCodeAt()
}
for (let i = 0; i < secondB.length; i++) {
const b = secondB.charAt(i)
const c = secondC.charAt(i)
btoc[c] = b.charCodeAt() - c.charCodeAt()
}
// console.log(atob)
// console.log(btoc)
// const cChar = secretC.charAt(0)
// console.log(cChar)
// console.log(btoc[cChar])
// console.log(atob[cChar])
// console.log(String.fromCharCode(cChar.charCodeAt() + btoc[cChar]))
// console.log(String.fromCharCode(cChar.charCodeAt() + btoc[cChar] + atob[cChar]))
// console.log(String.fromCharCode(cChar.charCodeAt() + btoc[cChar] + atob[cChar] - 26))
// let val = cChar.charCodeAt() - 65 + btoc[cChar] + atob[cChar]
// val = val.mod(26)
// console.log(val)
// console.log(String.fromCharCode(val + 65))
// console.log(String.fromCharCode((firstB.charAt(0).charCodeAt() - 65 + atob[firstB.charAt(0)]).mod(26) + 65))
// console.log(String.fromCharCode((secondC.charAt(0).charCodeAt() - 65 + btoc[secondC.charAt(0)]).mod(26) + 65))
let result = []
for (let i = 0; i < secretC.length; i++) {
if (secretC.charAt(i) === ' ') {
result.push(' ')
continue
}
let code = (secretC.charAt(i).charCodeAt() - 65 + btoc[secretC.charAt(i)]).mod(26) + 65
code = (code - 65 + atob[String.fromCharCode(code)]).mod(26) + 65
result.push(String.fromCharCode(code))
}
return result.join('')
}
/* Ignore and do not change the code below */
/**
* Try a solution
* @param message The message secretC translated to the alphabet A
*/
function trySolution(message) {
console.log('' + JSON.stringify(message))
}
trySolution(
decrypt(
JSON.parse(readline()),
JSON.parse(readline()),
JSON.parse(readline()),
JSON.parse(readline()),
JSON.parse(readline())
)
)
/* Ignore and do not change the code above */