Last active
December 6, 2018 14:13
-
-
Save nicolas-cherel/0cc5a10b0bf422c72dc53c99d50bbd4b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 1.1 | |
data.split(/\n/g).map(e => parseInt(e, 10)).reduce((m, v) => m+v, 0) | |
// 1.2 | |
Array.from({ | |
[Symbol.iterator]() { | |
const list = data.split(/\n/g).map(e => parseInt(e, 10)); | |
let index = 0; | |
let sum = 0; | |
const seen = { 0: 0 }; | |
let done = false; | |
return { | |
next() { | |
const current = list[index]; | |
sum += current; | |
Object.assign(seen, { [sum]: (seen[sum] || 0) + 1 }) | |
try { | |
return { | |
value: { sum, count: seen[sum] }, | |
done, | |
} | |
} finally { | |
index = ((index+1) % list.length) | |
done = seen[sum] > 1; | |
} | |
} | |
} | |
} | |
}).slice(-1)[0].sum | |
// 2.1 | |
Object.values( | |
data | |
.split(/\n/g) | |
.map((e) => Array.from(e).sort().join('').match(/((\w)\2+)/g)) | |
.reduce((memo, t) => ( | |
Object.assign(memo, | |
Object.keys( | |
t.map(t => t.length).reduce( | |
(m, i) => Object.assign(m, { [i]: 1 }), {} | |
) | |
).reduce( | |
(m3, v) => Object.assign(m3, { [v]: (m3[v] || 0) + 1 }), memo | |
) | |
)), {}) | |
).reduce((m, v) => m*v, 1) | |
// 2.2 | |
data | |
.split(/\n/g) | |
.map((t1, index, list) => ( | |
[ | |
list.find( | |
(t2) => ( | |
1 === Array.from(t2).reduce( | |
(count, c, index) => ( | |
count += c === t1[index] ? 0 : 1 | |
), | |
0 | |
) | |
) | |
), | |
t1 | |
] | |
)) | |
.filter(([a, b]) => Boolean(a)) | |
.map(([a, b]) => { | |
const ary = Array.from(a); | |
ary.splice(Array.from(a).findIndex((c, index) => c !== b[index]), 1); | |
return ary.join(''); | |
}) | |
; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment