diff --git a/Problems/Bingo-Check/README.md b/Problems/Bingo-Check/README.md new file mode 100644 index 0000000..cef1cd6 --- /dev/null +++ b/Problems/Bingo-Check/README.md @@ -0,0 +1,43 @@ +# Bingo-Check + +Create a function that takes a 5x5 2D list and returns True if it has at least one Bingo, and False If it doesn't. + +## Infos which are maybe needed + +A board has a bingo, when a horizontal, vertical or diagonal 5 `x` line exists. + +## Documentation + +### Solution Idea + +The board is represented as a 2D array (an array containing other arrays). + +First, we check whether any inner array is completely filled with `"x"`. +If such an array exists, it means there is a horizontal line and we return `true`. + +If not, we check for vertical lines. +For each possible index (0 → 4), we check the same index in every inner array. +If all arrays contain `"x"` at that index, we have a vertical line and return `true`. + +Finally, we check the two diagonals: + +- positions `[i, i]` +- positions `[i, 4 - i]` + +For each index `i`, we verify whether all elements on one of these diagonals are `"x"`. +If either diagonal contains only `"x"`, we return `true`. + +If none of these checks succeed, no winning line exists and the function returns `false`. + +--- + +### [Implementation](./solver.ts) + +It`s really simple. There are just 3 different for-loops. I don't think there is a description necessary :) + +--- + +### Information's + +This problem comes from the newsletter [Sloth Bytes](https://slothbytes.beehiiv.com). +[Post](https://slothbytes.beehiiv.com/p/managing-data-is-complicated) from January 27, 2026. diff --git a/Problems/Bingo-Check/bingo-check.test.ts b/Problems/Bingo-Check/bingo-check.test.ts new file mode 100644 index 0000000..68474c0 --- /dev/null +++ b/Problems/Bingo-Check/bingo-check.test.ts @@ -0,0 +1,79 @@ +import { describe, expect, it } from "vitest"; +import type { FixedArray } from "../../utilities/types/fixedArray"; +import bingoCheck from "./solver"; + +type CardType = FixedArray, 5>; + +describe("Validates correctly if a Bingo-Card has a bingo on it or not", () => { + const firstCard: CardType = [ + [45, "x", 31, 74, 87], + [64, "x", 47, 32, 90], + [37, "x", 68, 83, 54], + [67, "x", 98, 39, 44], + [21, "x", 24, 30, 52], + ]; + + it("should return true for the horizontal bingo", () => { + expect(bingoCheck(firstCard)).toEqual(true); + }); + + const secondCard: CardType = [ + ["x", 43, 31, 74, 87], + [64, "x", 47, 32, 90], + [37, 65, "x", 83, 54], + [67, 98, 39, "x", 44], + [21, 59, 24, 30, "x"], + ]; + + it("should return true for the diagonal bingo", () => { + expect(bingoCheck(secondCard)).toEqual(true); + }); + + const thirdCard: CardType = [ + ["x", "x", "x", "x", "x"], + [64, 12, 47, 32, 90], + [37, 16, 68, 83, 54], + [67, 19, 98, 39, 44], + [21, 75, 24, 30, 52], + ]; + + it("should return true for the diagonal bingo", () => { + expect(bingoCheck(thirdCard)).toEqual(true); + }); + + const forthCard: CardType = [ + [45, "x", 31, 74, 87], + [64, 78, 47, "x", 90], + [37, "x", 68, 83, 54], + [67, "x", 98, "x", 44], + [21, "x", 24, 30, 52], + ]; + + it("should return false for no bingo", () => { + expect(bingoCheck(forthCard)).toEqual(false); + }); + + const fifthCard: CardType = [ + [45, 43, 31, 74, 87], + ["x", 29, 47, 32, 90], + [37, "x", 41, 83, 54], + [67, 98, "x", 58, 44], + [21, 59, 24, "x", 59], + ]; + + it("should return false for a wrong vertical bingo", () => { + expect(bingoCheck(fifthCard)).toEqual(false); + }); + + const sixthCard: CardType = [ + [21, 59, 24, 30, "x"], + [67, 98, 39, "x", 44], + [37, 65, "x", 83, 54], + [64, "x", 47, 32, 90], + ["x", 43, 31, 74, 87], + ]; + + it("should return true for the other diagonal bingo", () => { + expect(bingoCheck(sixthCard)).toEqual(true); + }); +}); diff --git a/Problems/Bingo-Check/solver.ts b/Problems/Bingo-Check/solver.ts new file mode 100644 index 0000000..bd91055 --- /dev/null +++ b/Problems/Bingo-Check/solver.ts @@ -0,0 +1,22 @@ +import type { FixedArray } from "../../utilities/types/fixedArray"; + +export default function bingoCheck(card: FixedArray, 5>): boolean { + if ( + card.reduce( + (preVal, row) => (preVal ? true : row.reduce((p, element) => (p ? element === "x" : false), true)), + false + ) + ) + return true; + + let leftDiagonal = true; + let rightDiagonal = true; + for (let i = 0; i < card.length; i++) { + if (card.reduce((preVal, val) => (preVal ? val[i] === "x" : false), true)) return true; + + if (!(leftDiagonal && card[i][i] === "x")) leftDiagonal = false; + if (!(rightDiagonal && card[i][4 - i] === "x")) rightDiagonal = false; + } + + return rightDiagonal || leftDiagonal; +} diff --git a/README.md b/README.md index 9671fab..4386135 100644 --- a/README.md +++ b/README.md @@ -8,9 +8,9 @@ This repository is about solving coding-problems. It contains famous (or not tha ## Problems -Solved: **23 problems** +Solved: **24 problems** -🟢 Easy: 17 +🟢 Easy: 18 🟠 Medium: 5 🔴 Hard: 1 @@ -39,6 +39,7 @@ Solved: **23 problems** | [One Two Skip a Few](./Problems/One-Two-Skip-A-Few/README.md) | 🟢 Easy | | [Day number of year](./Problems/Day-Number-Of-Year/README.md) | 🟢 Easy | | [Remove the last vowel](./Problems/Remove-The-Last-Vowel/README.md) | 🟢 Easy | +| [Bingo Check](./Problems/Bingo-Check/README.md) | 🟢 Easy | diff --git a/package-lock.json b/package-lock.json index afcb255..9c8d4bc 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "problem-solving", - "version": "CP-22", + "version": "CP-23", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "problem-solving", - "version": "CP-22", + "version": "CP-23", "license": "ISC", "dependencies": { "decimal.js": "^10.6.0", diff --git a/package.json b/package.json index 1f7ce5e..dfa8f6c 100644 --- a/package.json +++ b/package.json @@ -19,7 +19,8 @@ "test:file": "vitest", "test:ci": "vitest --run --passWithNoTests --reporter=json > vitest-report.json", "build": "tsc", - "normalize": "biome check --write --unsafe", + "clear": "rm -rf ./build", + "normalize": "npm run clear && biome check --write --unsafe", "prepare": "husky install" }, "dependencies": { diff --git a/utilities/types/fixedArray.ts b/utilities/types/fixedArray.ts new file mode 100644 index 0000000..119ea4b --- /dev/null +++ b/utilities/types/fixedArray.ts @@ -0,0 +1,11 @@ +// Source - https://stackoverflow.com/a/60762482 +// Posted by Tomasz Gawel, modified by community. See post 'Timeline' for change history +// Retrieved 2026-03-14, License - CC BY-SA 4.0 + +type Grow> = ((x: T, ...xs: A) => void) extends (...a: infer X) => void ? X : never; +type GrowToSize, N extends number> = { + 0: A; + 1: GrowToSize, N>; +}[A["length"] extends N ? 0 : 1]; + +export type FixedArray = GrowToSize;