Last active
December 14, 2019 11:42
-
-
Save kakao-kong-j/bc491824b0d1f3bd9d80a9c2103ccdf9 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
const readline = require("readline"); | |
class Converter { | |
static NUMBER_CHART = { | |
"": 0, | |
일: 1, | |
이: 2, | |
삼: 3, | |
사: 4, | |
오: 5, | |
육: 6, | |
칠: 7, | |
팔: 8, | |
구: 9 | |
}; | |
static DIGIT_CHART_UNDER_THOUSAND = { | |
"": 0, | |
십: 1, | |
백: 2, | |
천: 3 | |
}; | |
static DIGIT_CHART_OVER_THOUSAND = { | |
만: 4, | |
억: 8, | |
조: 12 | |
}; | |
static DIGIT_CHART = { | |
...Converter.DIGIT_CHART_OVER_THOUSAND, | |
...Converter.DIGIT_CHART_UNDER_THOUSAND | |
}; | |
static DIGIT_CHART_REVERSE = Object.entries(Converter.DIGIT_CHART).reduce( | |
(obj, [key, value]) => ({ | |
...obj, | |
[value]: key | |
}), | |
{} | |
); | |
static NUMBER_CHART_REVERSE = Object.entries(Converter.NUMBER_CHART).reduce( | |
(obj, [key, value]) => ({ | |
...obj, | |
[value]: key | |
}), | |
{} | |
); | |
static koreanToNumber(str) { | |
if (Object.keys(Converter.DIGIT_CHART).includes(str[0])) { | |
str = "일" + str | |
} | |
const digitKeys = Object.keys(Converter.DIGIT_CHART_OVER_THOUSAND); | |
const arr = str.split(new RegExp(`(${digitKeys.join("|")})`, "g")); | |
let buffer = 0; | |
const result = arr.reduce((acc, cur) => { | |
let digit = 0; | |
if (Converter.DIGIT_CHART_OVER_THOUSAND[cur]) { | |
digit = Converter.DIGIT_CHART_OVER_THOUSAND[cur]; | |
} else { | |
buffer = Converter.koreanToNumberUnder1000(cur); | |
} | |
return digit ? acc + buffer * 10 ** digit : acc; | |
}, 0); | |
return arr % 2 === 0 ? result : result + buffer; | |
} | |
static koreanToNumberUnder1000(str) { | |
const digitChartUnderThousandKeys = Object.keys( | |
Converter.DIGIT_CHART_UNDER_THOUSAND | |
); | |
const arr = str.split( | |
new RegExp(digitChartUnderThousandKeys.join("|"), "g") | |
); | |
let buffer = 0; | |
const result = arr.reduce((acc, cur) => { | |
const digit = Converter.DIGIT_CHART_UNDER_THOUSAND[cur]; | |
if (Converter.NUMBER_CHART[cur]) { | |
buffer = Converter.NUMBER_CHART[cur]; | |
} | |
return digit ? acc + buffer * 10 ** digit : acc; | |
}, 0); | |
return digitChartUnderThousandKeys.includes(str[str.length - 1]) | |
? result | |
: result + buffer; | |
} | |
static numberToKorean(num) { | |
const arr = Array.from(num.toString()).map(Number); | |
return arr | |
.reduce((acc, cur, index) => { | |
const squared = arr.length - 1 - index; | |
let result = ""; | |
const value = Converter.NUMBER_CHART_REVERSE[cur]; | |
result += value; | |
switch (squared % 4) { | |
case 0: | |
const digit = Converter.DIGIT_CHART_REVERSE[squared]; | |
!result.includes(digit) && (result += digit); | |
break; | |
default: | |
result += value && Converter.DIGIT_CHART_REVERSE[squared % 4]; | |
break; | |
} | |
acc.push(result); | |
return acc; | |
}, []) | |
.join(""); | |
} | |
} | |
class Calculator { | |
static ARGS_SIZE = 2; | |
_args = []; | |
get args() { | |
return this._args; | |
} | |
set args(args) { | |
if (!Array.isArray(args)) { | |
throw Error("args is wrong format"); | |
} | |
this._args = args; | |
} | |
getSize() { | |
return this.args.length; | |
} | |
appendArg(arg) { | |
this.args = [...this.args, arg]; | |
} | |
calculate() { | |
return this.args.reduce((sum, n) => sum + n, 0); | |
} | |
} | |
const calculator = new Calculator(); | |
const r = readline.createInterface({ | |
input: process.stdin, | |
output: process.stdout | |
}); | |
r.prompt(); | |
r.on("line", arg => { | |
calculator.appendArg(Converter.koreanToNumber(arg)); | |
if (calculator.getSize() === Calculator.ARGS_SIZE) { | |
console.log(`${Converter.numberToKorean(calculator.calculate())}`); | |
r.close(); | |
} else { | |
} | |
r.prompt(); | |
}); | |
r.on("close", () => { | |
process.exit(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment