-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathb1107.js
More file actions
53 lines (40 loc) · 1.09 KB
/
b1107.js
File metadata and controls
53 lines (40 loc) · 1.09 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
var fs = require("fs");
var input = fs.readFileSync("/dev/stdin").toString().trim().split("\n");
const target = +input[0];
const broken = input[2] ? input[2].split(" ").map(Number) : [];
const available = Array(10)
.fill(0)
.map((_, i) => i)
.filter((v) => !broken.includes(v));
let cnt = Math.abs(100 - target);
for (let i = 0; i < 1_000_000; i++) {
const channel = i.toString().split("");
const isValid = channel.every((v) => available.includes(+v));
if (isValid) {
cnt = Math.min(cnt, channel.length + Math.abs(i - target));
}
}
console.log(cnt);
/**
* const target = +input[0];
const broken = input[2] ? input[2].split(" ").map(Number) : [];
const available = Array(10)
.fill(0)
.map((_, i) => i)
.filter((v) => !broken.includes(v));
let cnt = Math.abs(100 - target);
function dfs(channel) {
if (channel) {
cnt = Math.min(cnt, channel.length + Math.abs(channel - target));
}
if (channel.length > `${target}`.length) {
return;
}
for (const btn of available) {
if (channel === "0") continue;
dfs(channel + btn);
}
}
dfs("", 0);
console.log(cnt);
*/