-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.js
55 lines (50 loc) · 1.49 KB
/
solution.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
const filePath = require('path').join(__dirname, 'input');
const [n, ...arr] = require('fs')
.readFileSync(filePath)
.toString()
.trim()
.split('\n');
function solution (n, clocks) {
const SWITCHES = 10;
const CLOCKS = 16;
const linkeds = [
'xxx.............',
'...x...x.x.x....',
'....x.....x...xx',
'x...xxxx........',
'......xxx.x.x...',
'x.x...........xx',
'...x..........xx',
'....xx.x......xx',
'.xxxxx..........',
'...xxx...x...x..'
]
const areAligned = (clocks) => clocks.every(v => v === 12);
const push = (clocks, swtch) => {
for (let clock = 0; clock < CLOCKS; clock++) {
if (linkeds[swtch][clock] === 'x') {
clocks[clock] += 3;
if (clocks[clock] === 15) {
clocks[clock] = 3;
}
}
}
}
const solve = (clocks, swtch) => {
if (swtch === SWITCHES) {
return areAligned(clocks) ? 0 : Infinity;
}
let result = Infinity;
for (let i = 0; i < 4; i ++) {
result = Math.min(result, i + solve(clocks, swtch + 1));
push(clocks, swtch);
}
return result
}
let answer = [];
for (let i = 0; i < clocks.length; i ++) {
answer.push(solve(clocks[i], 0));
}
return answer.map(v => v === Infinity ? -1 : v).join('\n');
}
console.log(solution(+n, arr.map(r => r.split(' ').map(c => +c))))