diff --git a/lib/tiShortChoser/tiShortChoser.test.ts b/lib/tiShortChoser/tiShortChoser.test.ts index f0952a7..1d92814 100644 --- a/lib/tiShortChoser/tiShortChoser.test.ts +++ b/lib/tiShortChoser/tiShortChoser.test.ts @@ -51,16 +51,16 @@ describe("tiShortChoser", () => { it("tiShortChoser 1, 1, 4, 6", () => { expect(tiShortChoser(1, 1, 4, 6)).toEqual("2 1"); }); - it("___ 4, 4, 4, 6", () => { + it("4, 4, 4, 6", () => { expect(tiShortChoser(4, 4, 4, 6)).toEqual("5 1"); }); - it("____ 8 9 5 9", () => { + it("8 9 5 9", () => { expect(tiShortChoser(8, 9, 5, 9)).toEqual("10 1"); }); - it("____ 1000 1 1000 1", () => { - expect(tiShortChoser(1000, 1, 1000, 1)).toEqual("2 1"); + it("1000 1 1000 1", () => { + expect(tiShortChoser(1000, 1, 1000, 1)).toEqual("2 2"); }); - it("____ 1000, 1, 1, 1000 ", () => { + it("1000, 1, 1, 1000 ", () => { expect(tiShortChoser(1000, 1, 1, 1000)).toEqual("1001 1"); }); it("1, 0, 1, 0 ", () => { diff --git a/lib/tiShortChoser/tiShortChoser.ts b/lib/tiShortChoser/tiShortChoser.ts index 846b218..7d361d0 100644 --- a/lib/tiShortChoser/tiShortChoser.ts +++ b/lib/tiShortChoser/tiShortChoser.ts @@ -1,3 +1,54 @@ +// export function tiShortChoser( +// tB: number, +// tR: number, +// sB: number, +// sR: number, +// ) { +// let tishort = 0; +// let socks = 0; +// const blueIsGrater = tB + sB > tR + sR; +// const isEquaal = tB + sB === tR + sR; +// const tIsEquale = tB === tR && tB > 0; +// const sIsEquale = sB === sR && sB > 0; +// const isBlue = tB === sB && !tIsEquale && !sIsEquale; +// const isRed = tR === sR && !tIsEquale && !sIsEquale; + +// if (blueIsGrater) { +// tishort = sIsEquale && !isEquaal ? 1 : tR + 1; +// socks = tIsEquale || isEquaal ? 1 : sR + 1; +// } else { +// tishort = sIsEquale && !isEquaal ? 1 : tB + 1; +// socks = tIsEquale || isEquaal ? 1 : sB + 1; +// } + + +// if (tB === 0 || tR === 0) { +// tishort = 1; +// socks = tB === 0 ? sB + 1 : sR + 1; +// } +// if (sB === 0 || sR === 0) { +// socks = 1; +// tishort = sB === 0 ? tB + 1 : tR + 1; +// } + +// if (isRed) { +// tishort = tR + 1; +// socks = 1; +// } + +// if (isBlue) { +// tishort = tB + 1; +// socks = 1; +// } + +// if (isBlue && isRed && (tB !== 0 || sB !== 0)) { +// tishort = 2; +// socks = 1; +// } + +// return [tishort, socks].join(" "); +// } + export function tiShortChoser( tB: number, tR: number, @@ -6,46 +57,41 @@ export function tiShortChoser( ) { let tishort = 0; let socks = 0; - const blueIsGrater = tB + sB > tR + sR; - const isEquaal = tB + sB === tR + sR; - const tIsEquale = tB === tR && tB > 0; - const sIsEquale = sB === sR && sB > 0; - const isBlue = tB === sB && !tIsEquale && !sIsEquale; - const isRed = tR === sR && !tIsEquale && !sIsEquale; - - if (blueIsGrater) { - tishort = sIsEquale && !isEquaal ? 1 : tR + 1; - socks = tIsEquale || isEquaal ? 1 : sR + 1; - } else { - tishort = sIsEquale && !isEquaal ? 1 : tB + 1; - socks = tIsEquale || isEquaal ? 1 : sB + 1; - } + const ans: number[][] = []; - - if (tB === 0 || tR === 0) { - tishort = 1; - socks = tB === 0 ? sB + 1 : sR + 1; - } - if (sB === 0 || sR === 0) { - socks = 1; - tishort = sB === 0 ? tB + 1 : tR + 1; - } - - if (isRed) { + if (tB > 0 && sB > 0) { tishort = tR + 1; - socks = 1; + socks = sR + 1; + ans.push([tishort, socks]) } - if (isBlue) { + if (tR > 0 && sR > 0) { tishort = tB + 1; - socks = 1; + socks = sB + 1; + ans.push([tishort, socks]) } - if (isBlue && isRed && (tB !== 0 || sB !== 0)) { - tishort = 2; + if (tB > 0 && tR > 0) { + tishort = Math.max(tB, tR) + 1; socks = 1; + ans.push([tishort, socks]) } - return [tishort, socks].join(" "); + if (sB > 0 && sR > 0) { + tishort = 1; + socks = Math.max(sB, sR) + 1; + ans.push([tishort, socks]) + } + + const result = ans.reduce((acc, currentValue) => { + const [t, s] = currentValue; + const [minT, minS] = acc; + const minSum = minT + minS; + const curSum = t + s; + if (acc.length === 0) return currentValue; + return (curSum < minSum) ? currentValue : acc; + }, [] as number[]) + + return result.join(" "); }