Skip to content

Instantly share code, notes, and snippets.

@phatnguyenuit
Created March 1, 2022 16:02
Show Gist options
  • Save phatnguyenuit/7bbc1d9f228b8007134c90485a91c91f to your computer and use it in GitHub Desktop.
Save phatnguyenuit/7bbc1d9f228b8007134c90485a91c91f to your computer and use it in GitHub Desktop.
Tính Tam Nguyên Cửu Vận dựa vào năm
const THE_1ST_YEAR = 1864;
const CYCLE_YEARS = 180;
const NUM_OF_FATES = 9;
const NUM_OF_YEARS_FOR_FATE = 20;
const ORIGINS = ['Thượng Nguyên', 'Trung Nguyên', 'Hạ Nguyên'];
const NINE_FATES_FACILITIES = [
'Khảm-Thủy',
'Khôn-Thổ',
'Chấn-Mộc',
'Tốn-Mộc',
'Trung cung-Thổ',
'Cấn-Kim',
'Đoài-Kim',
'Cấn-Thổ',
'Ly-Hỏa',
];
const calculateYearValue = (inputYear) => {
if (!inputYear || inputYear < THE_1ST_YEAR)
throw new Error(
`Wrong year provided: ${inputYear}.\nShould input year from ${THE_1ST_YEAR}.`
);
const yearValue = (inputYear - THE_1ST_YEAR) % CYCLE_YEARS;
return yearValue;
};
const calculateOriginAndFate = (inputYear) => {
const yearValue = calculateYearValue(inputYear);
const fateValue = Math.floor(yearValue / NUM_OF_YEARS_FOR_FATE) + 1;
const originIndex = Math.max(Math.ceil(fateValue / ORIGINS.length) - 1, 0);
const origin = ORIGINS[originIndex];
console.log(`
Year: ${inputYear}
- Origin: ${origin}
- Fate: ${fateValue}
- Fate Facility: ${NINE_FATES_FACILITIES[fateValue - 1]}
`);
};
const printFullThreeOriginsAndNineFates = (inputYear) => {
const currentYear = new Date().getFullYear();
const targetYear = inputYear || currentYear;
console.log(`Printing full 3 origins and nine fates for year: ${targetYear}`);
// If not provide input year use current year
const yearValue = calculateYearValue(targetYear);
let startYear = targetYear - yearValue;
console.log(`The 1st for this cycle is: ${startYear}`);
for (let originIndex in ORIGINS) {
console.log(`Origin: ${ORIGINS[originIndex]}`);
for (let fateIndex = 0; fateIndex < NUM_OF_FATES / 3; fateIndex++) {
const fromYear = startYear;
const endYear = startYear + NUM_OF_YEARS_FOR_FATE - 1;
// Assign new start year for next Fate
startYear = endYear + 1;
const fateNo = originIndex * (NUM_OF_FATES / 3) + fateIndex + 1;
console.log(`- Fate ${fateNo}: ${fromYear}-${endYear}`);
}
}
};
// calculateOriginAndFate();
calculateOriginAndFate(2022);
// printFullThreeOriginsAndNineFates();
// printFullThreeOriginsAndNineFates(2044);
// References:
// - https://blog.daothuba.com/2020/07/cuu-cung-phi-tinh-tam-nguyen-cuu-van.html
// - http://phongthuyhuyenkhonghoc.net/index.php/component/k2/item/33-tam-nguyen-cuu-van
// - Kim=>Thủy, Thủy=>Mộc, Mộc=>Hoả, Hoả=>Thổ, Thổ=>Kim
@phatnguyenuit
Copy link
Author

phatnguyenuit commented Mar 1, 2022

Year: 2022
  - Origin: Hạ Nguyên
  - Fate: 8
  - Fate Facility: Cấn-Thổ

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment