알고리즘: 브루트포스 알고리즘
const fs = require('fs');
let inputData = fs.readFileSync('/dev/stdin').toString().trim().split('\n');
let N = inputData[0] * 1;
let broken_number = inputData[1] * 1;
let answel = [];
if (broken_number === 10) {
//전부 고장났을때 + -로 이동.
console.log(Math.abs(N - 100));
} else if (broken_number === 0) {
console.log(Math.min(inputData[0].length, Math.abs(N - 100)));
} else {
//한개 이상 고장났을때
let broken_arr = inputData[2].trim().split(' ').map(x => x * 1);
let notBroken_map = new Map();
for (let i = 0; i <= 9; i++) {
if (!broken_arr.includes(i)) {
notBroken_map.set(i, true);
}
}
if (N === 0) {
if (ck_num(N, notBroken_map)) {
answel.push(N.toString().length);
} else {
let find_plus = false;
let pw = 1;
while (!find_plus) {
let pn = N + pw;
if (!find_plus) {
if (ck_num(pn, notBroken_map)) {
answel.push(pn.toString().length + Math.abs(pn - N));;
find_plus = true;
} else {
pw += 1;
}
}
}
}
} else {
if (notBroken_map.size === 1 && notBroken_map.get(0)) {
answel.push(1 + Math.abs(N));
} else {
if (ck_num(N, notBroken_map)) {
answel.push(N.toString().length);
} else {
let find_plus = false;
let find_minus = false;
let pw = 1;
let mw = -1;
while (!find_plus || !find_minus) {
let pn = N + pw;
let mn = N + mw;
if (!find_plus) {
if (ck_num(pn, notBroken_map)) {
answel.push(pn.toString().length + Math.abs(pn - N));;
find_plus = true;
} else {
pw += 1;
}
}
if (!find_minus) {
if (ck_num(mn, notBroken_map)) {
answel.push(mn.toString().length + Math.abs(mn - N));
find_minus = true;
} else {
mw -= 1;
}
}
if(mn === 0) {
find_minus = true;
}
}
}
}
}
console.log(Math.min(...answel, Math.abs(N - 100)));
}
function ck_num(n, map) {
let n_arr = n.toString().split('').map(x => x * 1);
let all_pass = true;
for (let i = 0; i < n_arr.length; i++) {
if (map.get(n_arr[i]) === undefined) {
all_pass = false;
break;
}
}
return all_pass;
}