diff --git a/.husky/commit-msg b/.husky/commit-msg deleted file mode 100755 index e810522..0000000 --- a/.husky/commit-msg +++ /dev/null @@ -1,4 +0,0 @@ -#!/usr/bin/env sh -. "$(dirname -- "$0")/_/husky.sh" - -npx --no -- commitlint --edit $1 diff --git a/.husky/pre-commit b/.husky/pre-commit deleted file mode 100755 index 74d7b0d..0000000 --- a/.husky/pre-commit +++ /dev/null @@ -1,4 +0,0 @@ -#!/usr/bin/env sh -. "$(dirname -- "$0")/_/husky.sh" - -npm run ci-check diff --git a/.versionrc b/.versionrc deleted file mode 100644 index 4e9cade..0000000 --- a/.versionrc +++ /dev/null @@ -1,42 +0,0 @@ -{ - "types": [ - { - "type": "feat", - "section": "✨ Features" - }, - { - "type": "fix", - "section": "🐛 Bug Fixes" - }, - { - "type": "chore", - "hidden": false, - "section": "🚚 Chores" - }, - { - "type": "docs", - "hidden": false, - "section": "📝 Documentation" - }, - { - "type": "style", - "hidden": false, - "section": "💄 Styling" - }, - { - "type": "refactor", - "hidden": false, - "section": "♻️ Code Refactoring" - }, - { - "type": "perf", - "hidden": false, - "section": "⚡️ Performance Improvements" - }, - { - "type": "test", - "hidden": false, - "section": "✅ Testing" - } - ] -} \ No newline at end of file diff --git a/commitlint.config.cjs b/commitlint.config.cjs deleted file mode 100644 index a4f4369..0000000 --- a/commitlint.config.cjs +++ /dev/null @@ -1,3 +0,0 @@ -module.exports = { - extends: ['@commitlint/config-conventional'] -} diff --git a/git-post-push-function.sh b/git-post-push-function.sh new file mode 100755 index 0000000..f1d2db1 --- /dev/null +++ b/git-post-push-function.sh @@ -0,0 +1,16 @@ + +# git() { +# echo "In git function>>>>>>>>>>>>>>" +# ROOT="$(/usr/bin/git rev-parse --show-toplevel)" +# LOCATION="/.git/hooks/post-push" +# if [ "$1" == "push" ] && [ -f "$ROOT$LOCATION" ]; then +# /usr/bin/git $* && eval $ROOT$LOCATION +# else +# /usr/bin/git $* +# fi +# } + +while IFS= read -r line +do + echo "LINe: $line" +done < /dev/stdout \ No newline at end of file diff --git a/gitclear_message.txt b/gitclear_message.txt new file mode 100644 index 0000000..c25a9c0 --- /dev/null +++ b/gitclear_message.txt @@ -0,0 +1 @@ +Open in GitClear >>>>> diff --git a/package-lock.json b/package-lock.json index 9ba2d5b..1755055 100644 --- a/package-lock.json +++ b/package-lock.json @@ -15,6 +15,7 @@ "@commitlint/cli": "18.0.0", "@commitlint/config-conventional": "18.0.0", "@commitlint/cz-commitlint": "18.0.0", + "@types/node": "20.8.10", "@types/react": "18.2.31", "@types/react-dom": "18.2.14", "@typescript-eslint/eslint-plugin": "6.9.0", @@ -23,21 +24,21 @@ "@vitest/coverage-c8": "0.28.5", "autoprefixer": "10.4.16", "c8": "8.0.1", - "commitizen": "4.3.0", - "daisyui": "^3.9.3", + "daisyui": "3.9.3", "eslint": "8.52.0", "eslint-config-prettier": "9.0.0", "eslint-plugin-import": "2.29.0", "eslint-plugin-jsx-a11y": "6.7.1", "eslint-plugin-prettier": "5.0.1", "eslint-plugin-react": "7.33.2", - "husky": "8.0.3", "inquirer": "8.2.6", "jsdom": "22.1.0", "postcss": "8.4.31", "prettier": "3.0.3", "standard-version": "9.5.0", "tailwindcss": "3.3.4", + "ts-node": "10.9.1", + "tslib": "2.6.2", "typescript": "5.2.2", "vite": "4.5.0", "vite-plugin-eslint": "1.8.1", @@ -1470,10 +1471,13 @@ "dev": true }, "node_modules/@types/node": { - "version": "20.5.1", - "resolved": "https://registry.npmjs.org/@types/node/-/node-20.5.1.tgz", - "integrity": "sha512-4tT2UrL5LBqDwoed9wZ6N3umC4Yhz3W3FloMmiiG4JwmUJWpie0c7lcnUNd4gtMKuDEO4wRVS8B6Xa0uMRsMKg==", - "dev": true + "version": "20.8.10", + "resolved": "https://registry.npmjs.org/@types/node/-/node-20.8.10.tgz", + "integrity": "sha512-TlgT8JntpcbmKUFzjhsyhGfP2fsiz1Mv56im6enJ905xG1DAYesxJaeSbGqQmAw8OWPdhyJGhGSQGKRNJ45u9w==", + "dev": true, + "dependencies": { + "undici-types": "~5.26.4" + } }, "node_modules/@types/normalize-package-data": { "version": "2.4.3", @@ -2485,6 +2489,7 @@ "resolved": "https://registry.npmjs.org/at-least-node/-/at-least-node-1.0.0.tgz", "integrity": "sha512-+q/t7Ekv1EDY2l6Gda6LLiX14rU9TV20Wa3ofeQmwPFZbOMo9DXrLbOjFaaclkXKWidIaopwAObQDqwWtGUjqg==", "dev": true, + "peer": true, "engines": { "node": ">= 4.0.0" } @@ -2771,6 +2776,7 @@ "resolved": "https://registry.npmjs.org/cachedir/-/cachedir-2.3.0.tgz", "integrity": "sha512-A+Fezp4zxnit6FanDmv9EqXNAi3vt9DWp51/71UEhXukb7QUuvtv9344h91dyAxuTLoSYJFU299qzR3tzwPAhw==", "dev": true, + "peer": true, "engines": { "node": ">=6" } @@ -3127,6 +3133,7 @@ "resolved": "https://registry.npmjs.org/commitizen/-/commitizen-4.3.0.tgz", "integrity": "sha512-H0iNtClNEhT0fotHvGV3E9tDejDeS04sN1veIebsKYGMuGscFaswRoYJKmT3eW85eIJAs0F28bG2+a/9wCOfPw==", "dev": true, + "peer": true, "dependencies": { "cachedir": "2.3.0", "cz-conventional-changelog": "3.3.0", @@ -3157,6 +3164,7 @@ "resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-9.1.0.tgz", "integrity": "sha512-hcg3ZmepS30/7BSFqRvoo3DOMQu7IjqxO5nCDt+zM9XWjb33Wg7ziNT+Qvqbuc3+gWpzO02JubVyk2G4Zvo1OQ==", "dev": true, + "peer": true, "dependencies": { "at-least-node": "^1.0.0", "graceful-fs": "^4.2.0", @@ -3172,6 +3180,7 @@ "resolved": "https://registry.npmjs.org/inquirer/-/inquirer-8.2.5.tgz", "integrity": "sha512-QAgPDQMEgrDssk1XiwwHoOGYF9BAbUcc1+j+FhEvaOt8/cKRqyLn0U5qA6F74fGhTMGxf92pOvPBeh29jQJDTQ==", "dev": true, + "peer": true, "dependencies": { "ansi-escapes": "^4.2.1", "chalk": "^4.1.1", @@ -3198,6 +3207,7 @@ "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.7.tgz", "integrity": "sha512-bzfL1YUZsP41gmu/qjrEk0Q6i2ix/cVeAhbCbqH9u3zYutS1cLg00qhrD0M2MVdCcx4Sc0UpP2eBWo9rotpq6g==", "dev": true, + "peer": true, "funding": { "url": "https://github.com/sponsors/ljharb" } @@ -3207,6 +3217,7 @@ "resolved": "https://registry.npmjs.org/wrap-ansi/-/wrap-ansi-7.0.0.tgz", "integrity": "sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==", "dev": true, + "peer": true, "dependencies": { "ansi-styles": "^4.0.0", "string-width": "^4.1.0", @@ -3637,7 +3648,8 @@ "version": "3.0.0", "resolved": "https://registry.npmjs.org/conventional-commit-types/-/conventional-commit-types-3.0.0.tgz", "integrity": "sha512-SmmCYnOniSsAa9GqWOeLqc179lfr5TRu5b4QFDkbsrJ5TZjPJx85wtOr3zn+1dbeNiXDKGPbZ72IKbPhLXh/Lg==", - "dev": true + "dev": true, + "peer": true }, "node_modules/conventional-commits-filter": { "version": "2.0.7", @@ -3875,6 +3887,7 @@ "resolved": "https://registry.npmjs.org/cz-conventional-changelog/-/cz-conventional-changelog-3.3.0.tgz", "integrity": "sha512-U466fIzU5U22eES5lTNiNbZ+d8dfcHcssH4o7QsdWaCcRs/feIPCxKYSWkYBNs5mny7MvEfwpTLWjvbm94hecw==", "dev": true, + "peer": true, "dependencies": { "chalk": "^2.4.1", "commitizen": "^4.0.3", @@ -3895,6 +3908,7 @@ "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-3.2.1.tgz", "integrity": "sha512-VT0ZI6kZRdTh8YyJw3SMbYm/u+NqfsAxEpWO0Pf9sq8/e94WxxOpPKx9FR1FlyCtOVDNOQ+8ntlqFxiRc+r5qA==", "dev": true, + "peer": true, "dependencies": { "color-convert": "^1.9.0" }, @@ -3907,6 +3921,7 @@ "resolved": "https://registry.npmjs.org/chalk/-/chalk-2.4.2.tgz", "integrity": "sha512-Mti+f9lpJNcwF4tWV8/OrTTtF1gZi+f8FqlyAdouralcFWFQWF2+NgCHShjkCb+IFBLq9buZwE1xckQU4peSuQ==", "dev": true, + "peer": true, "dependencies": { "ansi-styles": "^3.2.1", "escape-string-regexp": "^1.0.5", @@ -3921,6 +3936,7 @@ "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-1.9.3.tgz", "integrity": "sha512-QfAUtd+vFdAtFQcC8CCyYt1fYWxSqAiK2cSD6zDB8N3cpsEBAvRxp9zOGg6G/SHHJYAT88/az/IuDGALsNVbGg==", "dev": true, + "peer": true, "dependencies": { "color-name": "1.1.3" } @@ -3929,13 +3945,15 @@ "version": "1.1.3", "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.3.tgz", "integrity": "sha512-72fSenhMw2HZMTVHeCA9KCmpEIbzWiQsjN+BHcBbS9vr1mtt+vJjPdksIBNUmKAW8TFUDPJK5SUU3QhE9NEXDw==", - "dev": true + "dev": true, + "peer": true }, "node_modules/cz-conventional-changelog/node_modules/escape-string-regexp": { "version": "1.0.5", "resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz", "integrity": "sha512-vbRorB5FUQWvla16U8R/qgaFIya2qGzwDrNmCZuYKrbdSUMG6I1ZCGQRefkRVhuOkIGVne7BQ35DSfo1qvJqFg==", "dev": true, + "peer": true, "engines": { "node": ">=0.8.0" } @@ -3945,6 +3963,7 @@ "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-3.0.0.tgz", "integrity": "sha512-sKJf1+ceQBr4SMkvQnBDNDtf4TXpVhVGateu0t918bl30FnbE2m4vNLX+VWe/dpjlb+HugGYzW7uQXH98HPEYw==", "dev": true, + "peer": true, "engines": { "node": ">=4" } @@ -3954,6 +3973,7 @@ "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-5.5.0.tgz", "integrity": "sha512-QjVjwdXIt408MIiAqCX4oUKsgU2EqAGzs2Ppkm4aQYbjm+ZEWEcW4SfFNTr4uMNZma0ey4f5lgLrkB0aX0QMow==", "dev": true, + "peer": true, "dependencies": { "has-flag": "^3.0.0" }, @@ -4080,7 +4100,8 @@ "version": "0.7.0", "resolved": "https://registry.npmjs.org/dedent/-/dedent-0.7.0.tgz", "integrity": "sha512-Q6fKUPqnAHAyhiUgFU7BUzLiv0kd8saH9al7tnu5Q/okj6dnupxyTgFIBjVzJATdfIAm9NAsvXNzjaKa+bxVyA==", - "dev": true + "dev": true, + "peer": true }, "node_modules/deep-eql": { "version": "4.1.3", @@ -4322,6 +4343,7 @@ "resolved": "https://registry.npmjs.org/detect-file/-/detect-file-1.0.0.tgz", "integrity": "sha512-DtCOLG98P007x7wiiOmfI0fi3eIKyWiLTGJ2MDnVi/E04lWGbf+JzrRHMm0rgIIZJGtHpKpbVgLWHrv8xXpc3Q==", "dev": true, + "peer": true, "engines": { "node": ">=0.10.0" } @@ -5170,6 +5192,7 @@ "resolved": "https://registry.npmjs.org/expand-tilde/-/expand-tilde-2.0.2.tgz", "integrity": "sha512-A5EmesHW6rfnZ9ysHQjPdJRni0SRar0tjtG5MNtm9n5TUvsYU8oozprtRD4AqHxcZWWlVuAmQo2nWKfN9oyjTw==", "dev": true, + "peer": true, "dependencies": { "homedir-polyfill": "^1.0.1" }, @@ -5311,6 +5334,7 @@ "resolved": "https://registry.npmjs.org/find-node-modules/-/find-node-modules-2.1.3.tgz", "integrity": "sha512-UC2I2+nx1ZuOBclWVNdcnbDR5dlrOdVb7xNjmT/lHE+LsgztWks3dG7boJ37yTS/venXw84B/mAW9uHVoC5QRg==", "dev": true, + "peer": true, "dependencies": { "findup-sync": "^4.0.0", "merge": "^2.1.1" @@ -5320,7 +5344,8 @@ "version": "1.1.0", "resolved": "https://registry.npmjs.org/find-root/-/find-root-1.1.0.tgz", "integrity": "sha512-NKfW6bec6GfKc0SGx1e07QZY9PE99u0Bft/0rzSD5k3sO/vwkVUpDUKVm5Gpp5Ue3YfShPFTX2070tDs5kB9Ng==", - "dev": true + "dev": true, + "peer": true }, "node_modules/find-up": { "version": "5.0.0", @@ -5343,6 +5368,7 @@ "resolved": "https://registry.npmjs.org/findup-sync/-/findup-sync-4.0.0.tgz", "integrity": "sha512-6jvvn/12IC4quLBL1KNokxC7wWTvYncaVUYSoxWw7YykPLuRrnv4qdHcSOywOI5RpkOVGeQRtWM8/q+G6W6qfQ==", "dev": true, + "peer": true, "dependencies": { "detect-file": "^1.0.0", "is-glob": "^4.0.0", @@ -5778,6 +5804,7 @@ "resolved": "https://registry.npmjs.org/global-modules/-/global-modules-1.0.0.tgz", "integrity": "sha512-sKzpEkf11GpOFuw0Zzjzmt4B4UZwjOcG757PPvrfhxcLFbq0wpsgpOqxpxtxFiCG4DtG93M6XRVbF2oGdev7bg==", "dev": true, + "peer": true, "dependencies": { "global-prefix": "^1.0.1", "is-windows": "^1.0.1", @@ -5792,6 +5819,7 @@ "resolved": "https://registry.npmjs.org/global-prefix/-/global-prefix-1.0.2.tgz", "integrity": "sha512-5lsx1NUDHtSjfg0eHlmYvZKv8/nVqX4ckFbM+FrGcQ+04KWcWFo9P5MxPZYSzUvyzmdTbI7Eix8Q4IbELDqzKg==", "dev": true, + "peer": true, "dependencies": { "expand-tilde": "^2.0.2", "homedir-polyfill": "^1.0.1", @@ -5808,6 +5836,7 @@ "resolved": "https://registry.npmjs.org/which/-/which-1.3.1.tgz", "integrity": "sha512-HxJdYWq1MTIQbJ3nw0cqssHoTNU267KlrDuGZ1WYlxDStUtKUhOaJmh112/TZmHxxUfuJqPXSOm7tDyas0OSIQ==", "dev": true, + "peer": true, "dependencies": { "isexe": "^2.0.0" }, @@ -6014,6 +6043,7 @@ "resolved": "https://registry.npmjs.org/homedir-polyfill/-/homedir-polyfill-1.0.3.tgz", "integrity": "sha512-eSmmWE5bZTK2Nou4g0AI3zZ9rswp7GRKoKXS1BLUkvPviOqs4YTN1djQIqrXy9k5gEtdLPy86JjRwsNM9tnDcA==", "dev": true, + "peer": true, "dependencies": { "parse-passwd": "^1.0.0" }, @@ -6087,21 +6117,6 @@ "node": ">=10.17.0" } }, - "node_modules/husky": { - "version": "8.0.3", - "resolved": "https://registry.npmjs.org/husky/-/husky-8.0.3.tgz", - "integrity": "sha512-+dQSyqPh4x1hlO1swXBiNb2HzTDN1I2IGLQx1GrBuiqFJfoMrnZWwVmatvSiO+Iz8fBUnf+lekwNo4c2LlXItg==", - "dev": true, - "bin": { - "husky": "lib/bin.js" - }, - "engines": { - "node": ">=14" - }, - "funding": { - "url": "https://github.com/sponsors/typicode" - } - }, "node_modules/iconv-lite": { "version": "0.4.24", "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.24.tgz", @@ -6661,7 +6676,8 @@ "version": "0.2.1", "resolved": "https://registry.npmjs.org/is-utf8/-/is-utf8-0.2.1.tgz", "integrity": "sha512-rMYPYvCzsXywIsldgLaSoPlw5PfoB/ssr7hY4pLfcodrA5M/eArza1a9VmTiNIBNMjOGr1Ow9mTyU2o69U6U9Q==", - "dev": true + "dev": true, + "peer": true }, "node_modules/is-weakmap": { "version": "2.0.1", @@ -6702,6 +6718,7 @@ "resolved": "https://registry.npmjs.org/is-windows/-/is-windows-1.0.2.tgz", "integrity": "sha512-eXK1UInq2bPmjyX6e3VHIzMLobc4J94i4AWn+Hpq3OU5KkrRC96OAcR3PRJ/pGu6m8TRnBHP9dkXQVsT/COVIA==", "dev": true, + "peer": true, "engines": { "node": ">=0.10.0" } @@ -7142,7 +7159,8 @@ "version": "4.6.0", "resolved": "https://registry.npmjs.org/lodash.map/-/lodash.map-4.6.0.tgz", "integrity": "sha512-worNHGKLDetmcEYDvh2stPCrrQRkP20E4l0iIS7F8EvzMqBBi7ltvFN5m1HvTf1P7Jk1txKhvFcmYsCr8O2F1Q==", - "dev": true + "dev": true, + "peer": true }, "node_modules/lodash.merge": { "version": "4.6.2", @@ -7201,6 +7219,7 @@ "resolved": "https://registry.npmjs.org/longest/-/longest-2.0.1.tgz", "integrity": "sha512-Ajzxb8CM6WAnFjgiloPsI3bF+WCxcvhdIG3KNA2KN962+tdBsHcuQ4k4qX/EcS/2CRkcc0iAkR956Nib6aXU/Q==", "dev": true, + "peer": true, "engines": { "node": ">=0.10.0" } @@ -7323,7 +7342,8 @@ "version": "2.1.1", "resolved": "https://registry.npmjs.org/merge/-/merge-2.1.1.tgz", "integrity": "sha512-jz+Cfrg9GWOZbQAnDQ4hlVnQky+341Yk5ru8bZSe6sIDTCIg8n9i/u7hSQGSVOF3C7lH6mGtqjkiT9G4wFLL0w==", - "dev": true + "dev": true, + "peer": true }, "node_modules/merge-stream": { "version": "2.0.0", @@ -7850,6 +7870,7 @@ "resolved": "https://registry.npmjs.org/parse-passwd/-/parse-passwd-1.0.0.tgz", "integrity": "sha512-1Y1A//QUXEZK7YKz+rD9WydcE1+EuPr6ZBgKecAB8tmoW6UFv0NREVJe1p+jRxtThkcbbKkfwIbWJe/IeE6m2Q==", "dev": true, + "peer": true, "engines": { "node": ">=0.10.0" } @@ -8550,6 +8571,7 @@ "resolved": "https://registry.npmjs.org/resolve-dir/-/resolve-dir-1.0.1.tgz", "integrity": "sha512-R7uiTjECzvOsWSfdM0QKFNBVFcK27aHOUwdvK53BcW8zqnGdYp0Fbj82cy54+2A4P2tFM22J5kRfe1R+lM/1yg==", "dev": true, + "peer": true, "dependencies": { "expand-tilde": "^2.0.0", "global-modules": "^1.0.0" @@ -9316,6 +9338,7 @@ "resolved": "https://registry.npmjs.org/strip-bom/-/strip-bom-4.0.0.tgz", "integrity": "sha512-3xurFv5tEgii33Zi8Jtp55wEIILR9eh34FAW00PZf+JnSsTmV/ioewSgQl97JHvgjoRGwPShsWm+IdrxB35d0w==", "dev": true, + "peer": true, "engines": { "node": ">=8" } @@ -9905,6 +9928,12 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/undici-types": { + "version": "5.26.5", + "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-5.26.5.tgz", + "integrity": "sha512-JlCMO+ehdEIKqlFxk6IfVoAUVmgz7cU7zD/h9XZ0qzeosSHmUJVOzSQvvYSYWXkFXC+IfLKSIffhv0sVZup6pA==", + "dev": true + }, "node_modules/universalify": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/universalify/-/universalify-2.0.0.tgz", diff --git a/package.json b/package.json index 4acfb91..a89d187 100644 --- a/package.json +++ b/package.json @@ -2,9 +2,14 @@ "name": "matrix-lapse", "private": true, "version": "0.0.2", - "type": "module", "scripts": { "dev": "vite", + "alg-dijkstra": "ts-node --project tsconfig.dev.json src/algorithms/dijkstra.ts", + "alg-dfs": "ts-node --project tsconfig.dev.json src/algorithms/dfs.ts", + "alg-maze": "ts-node --project tsconfig.dev.json src/algorithms/search-maze.ts", + "alg-paint": "ts-node --project tsconfig.dev.json src/algorithms/paint-group.ts", + "alg-shortest-path": "ts-node --project tsconfig.dev.json src/algorithms/bfs-shortest-path.ts", + "alg-a-star": "ts-node --project tsconfig.dev.json src/algorithms/a-star-search.ts", "build": "tsc && vite build", "build:test": "tsc && vite build --mode testing", "preview": "vite preview", @@ -27,6 +32,7 @@ "@commitlint/cli": "18.0.0", "@commitlint/config-conventional": "18.0.0", "@commitlint/cz-commitlint": "18.0.0", + "@types/node": "20.8.10", "@types/react": "18.2.31", "@types/react-dom": "18.2.14", "@typescript-eslint/eslint-plugin": "6.9.0", @@ -35,7 +41,6 @@ "@vitest/coverage-c8": "0.28.5", "autoprefixer": "10.4.16", "c8": "8.0.1", - "commitizen": "4.3.0", "daisyui": "3.9.3", "eslint": "8.52.0", "eslint-config-prettier": "9.0.0", @@ -43,13 +48,14 @@ "eslint-plugin-jsx-a11y": "6.7.1", "eslint-plugin-prettier": "5.0.1", "eslint-plugin-react": "7.33.2", - "husky": "8.0.3", "inquirer": "8.2.6", "jsdom": "22.1.0", "postcss": "8.4.31", "prettier": "3.0.3", "standard-version": "9.5.0", "tailwindcss": "3.3.4", + "ts-node": "10.9.1", + "tslib": "2.6.2", "typescript": "5.2.2", "vite": "4.5.0", "vite-plugin-eslint": "1.8.1", diff --git a/src/algorithms/a-star-search.ts b/src/algorithms/a-star-search.ts new file mode 100644 index 0000000..c907979 --- /dev/null +++ b/src/algorithms/a-star-search.ts @@ -0,0 +1,191 @@ +import util from 'util'; +import { EIGHT_DIRECTIONS, Position, getValueAtPos, isInBoundaries, isSamePos, move } from './helpers'; + +class Cell { + parent: Position = { + row: 0, + col: 0, + }; + f: number = 0; + g: number = 0; + h: number = 0; + + constructor({ parent, f, g, h }: { parent: Position; f: number; g: number; h: number }) { + this.parent = parent; + this.f = f; + this.g = g; + this.h = h; + } +} + +const isAllowed = (matrix: number[][], next: Position): boolean => getValueAtPos(matrix, next) === 1; + +const isValidMove = (matrix: number[][], visited: boolean[][], next: Position) => { + return !visited[next.row][next.col] && isAllowed(matrix, next); +}; + +const isDestination = (next: Position, end: Position) => isSamePos(next, end); + +const computeHValue = (next: Position, end: Position) => { + const rowDiff = next.row - end.row; + const colDiff = next.col - end.col; + + return Math.sqrt(rowDiff * rowDiff + colDiff * colDiff); +}; + +const tracePath = (cellDetails: Cell[][], end: Position) => { + console.log('The Path is '); + + const path = []; + let currentNode = end; + let parent = cellDetails[currentNode.row][currentNode.col].parent; + + // console.log(util.inspect(cellDetails, { showHidden: false, depth: null, colors: true })); + + while (!(parent.row === currentNode.row && parent.col === currentNode.col)) { + path.push(currentNode); + currentNode = parent; + parent = cellDetails[currentNode.row][currentNode.col].parent; + } + + path.push(currentNode); + + while (path.length) { + const p = path.shift() as Position; + + console.log(`-> (${p.row}, ${p.col})`); + } + + return; +}; + +const aStarSearch = (matrix: number[][], start: Position, end: Position) => { + if (!isInBoundaries(matrix, start)) { + console.log('Source is invalid\n'); + return; + } + + if (!isInBoundaries(matrix, end)) { + console.log('Destination is invalid\n'); + return; + } + + if (!isAllowed(matrix, start) || !isAllowed(matrix, end)) { + console.log('MATRIX', matrix); + console.log('START', start, getValueAtPos(matrix, start)); + console.log('END', end, getValueAtPos(matrix, end)); + console.log('Source or the destination is blocked\n'); + return; + } + + if (isDestination(start, end)) { + console.log('We are already at the destination\n'); + return; + } + + const closedList: boolean[][] = matrix.map((row) => row.map(() => false)); + const cellDetails: Cell[][] = matrix.map((row) => + row.map( + () => + new Cell({ + parent: { + row: -1, + col: -1, + }, + f: Number.MAX_SAFE_INTEGER, + g: Number.MAX_SAFE_INTEGER, + h: Number.MAX_SAFE_INTEGER, + }), + ), + ); + + cellDetails[start.row][start.col] = new Cell({ + parent: { + row: start.row, + col: start.col, + }, + f: 0, + g: 0, + h: 0, + }); + + const openList: Map = new Map(); + + openList.set(0, start); + + let foundDest = false; + + let gNew, hNew, fNew; + + console.log('OPEN LIST', openList); + + while (openList.size) { + console.log('OPEN LIST', openList); + const [key, current] = openList.entries().next().value as [number, Position]; + + openList.delete(key); + + closedList[current.row][current.col] = true; + + for (const direction of Object.values(EIGHT_DIRECTIONS)) { + const next = move(current, direction); + + if (!isInBoundaries(matrix, next)) { + continue; + } + + if (isDestination(next, end)) { + cellDetails[next.row][next.col].parent = current; + console.log(`The destination cell is found ${next}\n`); + tracePath(cellDetails, end); + foundDest = true; + + return; + } else if (isValidMove(matrix, closedList, next)) { + const currentCell = cellDetails[current.row][current.col]; + + gNew = currentCell.g + 1; + hNew = computeHValue(next, end); + fNew = gNew + hNew; + + if (cellDetails[next.row][next.col].f === Number.MAX_SAFE_INTEGER || cellDetails[next.row][next.col].f > fNew) { + openList.set(fNew, next); + + cellDetails[next.row][next.col].f = fNew; + cellDetails[next.row][next.col].g = gNew; + cellDetails[next.row][next.col].h = hNew; + cellDetails[next.row][next.col].parent = current; + } + } + } + } + + if (!foundDest) { + console.log('Failed to find the Destination Cell\n'); + } + + return; +}; + +const matrix = [ + [1, 0, 1, 1, 1, 1, 0, 1, 1, 1], + [1, 1, 1, 0, 1, 1, 1, 0, 1, 1], + [1, 1, 1, 0, 1, 1, 0, 1, 0, 1], + [0, 0, 1, 0, 1, 0, 0, 0, 0, 1], + [1, 1, 1, 0, 1, 1, 1, 0, 1, 0], + [1, 0, 1, 1, 1, 1, 0, 1, 0, 0], + [1, 0, 0, 0, 0, 1, 0, 0, 0, 1], + [1, 0, 1, 1, 1, 1, 0, 1, 1, 1], + [1, 1, 1, 0, 0, 0, 1, 0, 0, 1], +]; + +const start = { + row: 8, + col: 0, +}; +const end = { + row: 0, + col: 0, +}; + +aStarSearch(matrix, start, end); diff --git a/src/algorithms/bfs-shortest-path.ts b/src/algorithms/bfs-shortest-path.ts new file mode 100644 index 0000000..95a21a0 --- /dev/null +++ b/src/algorithms/bfs-shortest-path.ts @@ -0,0 +1,81 @@ +import { FOUR_DIRECTIONS, Position, getValueAtPos, isInBoundaries, isSamePos, isVisited, move } from './helpers'; + +const isAllowed = (matrix: number[][], next: Position): boolean => { + return getValueAtPos(matrix, next) !== 1; +}; + +const isValidMove = (matrix: number[][], visited: boolean[][], next: Position) => { + return isInBoundaries(matrix, next) && !isVisited(visited, next) && isAllowed(matrix, next); +}; + +const BFS = (matrix: number[][], p1: Position, p2: Position) => { + const visited = matrix.map((row) => row.map(() => false)); + const prev: Map = new Map(); + const queue = [p1]; + + visited[p1.row][p1.col] = true; + + while (queue.length) { + const current = queue.shift() as Position; + + if (isSamePos(current, p2)) { + return { + isPath: true, + prev, + }; + } + + for (const direction of Object.values(FOUR_DIRECTIONS)) { + const next = move(current, direction); + + if (isValidMove(matrix, visited, next)) { + visited[next.row][next.col] = true; + prev.set(`${next.row}-${next.col}`, `${current.row}-${current.col}`); + queue.push(next); + } + } + } + + return { + isPath: false, + prev, + }; +}; + +const findShortestPath = (matrix: number[][], p1: Position, p2: Position) => { + const { prev, isPath } = BFS(matrix, p1, p2); + + if (!isPath) { + console.log('No path found', p1, p2); + + return; + } + + const path = []; + const p2Str = `${p2.row}-${p2.col}`; + + let current = p2Str; + + console.log('PREV', prev); + + while (current) { + path.push(current); + current = prev.get(current) || ''; + } + + path.reverse(); + + console.log('Path found:', path); +}; + +const matrix = [ + [0, 0, 1, 0, 0], + [0, 0, 0, 0, 0], + [0, 0, 0, 1, 0], + [1, 1, 0, 1, 1], + [0, 0, 0, 0, 0], +]; + +const result = findShortestPath(matrix, { row: 0, col: 0 }, { row: 2, col: 2 }); + +console.log('RESULT', result); diff --git a/src/algorithms/dfs.ts b/src/algorithms/dfs.ts new file mode 100644 index 0000000..264f5bc --- /dev/null +++ b/src/algorithms/dfs.ts @@ -0,0 +1,129 @@ +import util from 'util'; + +class Graph { + adjacencyList: { [vertex: string]: string[] } = {}; + + addVertex(vertex: string) { + if (!this.adjacencyList[vertex]) { + this.adjacencyList[vertex] = []; + } + + return this; + } + + addEdge(source: string, destination: string) { + if (!this.adjacencyList[source]) { + this.addVertex(source); + } + if (!this.adjacencyList[destination]) { + this.addVertex(destination); + } + this.adjacencyList[source].push(destination); + this.adjacencyList[destination].push(source); + + return this; + } + + removeEdge(source: string, destination: string) { + this.adjacencyList[source] = this.adjacencyList[source].filter((vertex) => vertex !== destination); + this.adjacencyList[destination] = this.adjacencyList[destination].filter((vertex) => vertex !== source); + + return this; + } + + removeVertex(vertex: string) { + while (this.adjacencyList[vertex].length) { + const adjacentVertex = this.adjacencyList[vertex].pop(); + adjacentVertex && this.removeEdge(vertex, adjacentVertex); + } + + delete this.adjacencyList[vertex]; + + return this; + } + bfs(start: string) { + const queue = [start]; + const result = []; + const visited: { [vertex: string]: boolean } = { + [start]: true, + }; + + while (queue.length) { + const current = queue.shift() || ''; + + result.push(current); + + for (const neighbour of this.adjacencyList[current] || []) { + if (!visited[neighbour]) { + visited[neighbour] = true; + queue.push(neighbour); + } + } + } + + return result; + } + + dfs(start: string) { + const stack = [start]; + const visited: { [v: string]: boolean } = { + [start]: true, + }; + const result = []; + + while (stack.length) { + const current = stack.pop() || ''; + + result.push(current); + + for (const next of this.adjacencyList[current] || []) { + if (!visited[next]) { + stack.push(next); + visited[next] = true; + } + } + } + + return result; + } + + dfsRecursive(start: string) { + const visited: { [v: string]: boolean } = {}; + const result: string[] = []; + const adjacencyList = this.adjacencyList; + + (function dfsAlg(node: string) { + if (!node) { + return null; + } + + visited[node] = true; + + result.push(node); + + adjacencyList[node].forEach((next) => { + if (!visited[next]) { + return dfsAlg(next); + } + }); + })(start); + + return result; + } +} + +const g = new Graph(); + +g.addEdge('1', '2') + .addEdge('1', '4') + .addEdge('2', '3') + .addEdge('4', '3') + .addEdge('3', '5') + .addEdge('5', '6') + .addEdge('5', '7'); + +console.log(util.inspect(g, { showHidden: false, depth: null, colors: true })); + +console.log('BFS', util.inspect(g.bfs('1'), { showHidden: false, depth: null, colors: true })); +console.log('DFS', util.inspect(g.dfs('1'), { showHidden: false, depth: null, colors: true })); +console.log('DFS RECURSIVE', util.inspect(g.dfsRecursive('1'), { showHidden: false, depth: null, colors: true })); diff --git a/src/algorithms/dijkstra.ts b/src/algorithms/dijkstra.ts new file mode 100644 index 0000000..59dd1c6 --- /dev/null +++ b/src/algorithms/dijkstra.ts @@ -0,0 +1,231 @@ +/* + + List of nodes with edges + Visited nodes + Adjacent nodes + + Nodes + Shortest Path + Previous Node + + 1. Dijkstra + 1.1 Dijkstra on Matrix + + 2. DFS + 3. BFS + + 4. A Star + +*/ + +import util from 'util'; + +type GraphEdge = { + neighbour: string; + weight: number; +}; + +type GraphNodes = Map; + +type NodesMetadata = Map< + string, + { + shortestDistance: number; + previousNode: string | null; + } +>; + +class Graph { + nodes: GraphNodes = new Map(); + + enqueue(nodeName: string, edges: GraphEdge[]) { + this.nodes.set(nodeName, edges); + } + + getNodeEdges(nodeName: string | undefined): GraphEdge[] { + if (!nodeName) { + console.log(`Cannot find node: ${nodeName}.`); + + return []; + } + + const edges = this.nodes.get(nodeName); + + if (!edges) { + console.log(`Cannot find any edges for node: ${nodeName}.`); + + return []; + } + + return edges; + } + + getShortestDistances(startNode: string): NodesMetadata { + const visitedNodes: { [node: string]: true } = {}; + let nextInLineNodes: string[] = [startNode]; + + const nodesMetadata: NodesMetadata = new Map( + [...this.nodes.keys()] + .sort((a, b) => a.localeCompare(b)) + .map((node) => [ + node, + { + shortestDistance: node === startNode ? 0 : Infinity, + previousNode: null, + }, + ]), + ); + + if (!this.nodes.get(startNode)) { + throw Error(`Invalid start node = ${startNode}`); + } + + let currentNode: string; + + do { + console.log('nextInLineNodes', nextInLineNodes); + currentNode = nextInLineNodes.shift() || ''; + console.log('CURRENT NODE', currentNode); + + const edges = this.getNodeEdges(currentNode).sort((a, b) => a.weight - b.weight); + const currentNodeShortestPath = nodesMetadata.get(currentNode)?.shortestDistance || 0; + + for (const edge of edges) { + const edgeNodeMetadata = nodesMetadata.get(edge.neighbour); + + if (edgeNodeMetadata && edgeNodeMetadata.shortestDistance > edge.weight + currentNodeShortestPath) { + const newEdgeNodeMetadata = { + shortestDistance: edge.weight + currentNodeShortestPath, + previousNode: currentNode, + }; + + nodesMetadata.set(edge.neighbour, newEdgeNodeMetadata); + } + } + + visitedNodes[currentNode] = true; + const nextInLineEdges = edges + .map((edge) => edge.neighbour) + .filter((node) => !visitedNodes[node] && node !== currentNode); + // nextInLineNodes = nextInLineNodes.filter((node) => node !== currentNode); + nextInLineNodes = [...nextInLineNodes, ...nextInLineEdges]; + nextInLineNodes = [...new Set(nextInLineNodes)]; + } while (nextInLineNodes.length); + + return nodesMetadata; + } + + getShortestPath(startNode: string, endNode: string) { + const nodesMetadata = this.getShortestDistances(startNode); + + console.log('NODES METADATA', nodesMetadata); + + let previousNode = nodesMetadata.get(endNode)?.previousNode; + let path = endNode; + + while (previousNode) { + path = `${previousNode}-${path}`; + + previousNode = nodesMetadata.get(previousNode)?.previousNode; + } + + return path; + } +} + +// export const findShortestPath = () => {}; + +const graph = new Graph(); + +graph.enqueue('A', [ + { + neighbour: 'B', + weight: 2, + }, + { + neighbour: 'D', + weight: 8, + }, +]); + +graph.enqueue('B', [ + { + neighbour: 'A', + weight: 2, + }, + { + neighbour: 'D', + weight: 5, + }, + { + neighbour: 'E', + weight: 6, + }, +]); + +graph.enqueue('D', [ + { + neighbour: 'A', + weight: 8, + }, + { + neighbour: 'B', + weight: 5, + }, + { + neighbour: 'E', + weight: 3, + }, + { + neighbour: 'F', + weight: 2, + }, +]); + +graph.enqueue('E', [ + { + neighbour: 'B', + weight: 6, + }, + { + neighbour: 'D', + weight: 3, + }, + { + neighbour: 'F', + weight: 1, + }, + { + neighbour: 'C', + weight: 9, + }, +]); + +graph.enqueue('F', [ + { + neighbour: 'D', + weight: 2, + }, + { + neighbour: 'E', + weight: 1, + }, + { + neighbour: 'C', + weight: 3, + }, +]); + +graph.enqueue('C', [ + { + neighbour: 'E', + weight: 9, + }, + { + neighbour: 'F', + weight: 3, + }, +]); + +console.log(util.inspect(graph, { showHidden: false, depth: null, colors: true })); + +// console.log('NODES METADATA', graph.getShortestDistances('C')); +console.log('NODES PATH', graph.getShortestPath('C', 'A')); diff --git a/src/algorithms/helpers.ts b/src/algorithms/helpers.ts new file mode 100644 index 0000000..7d57e20 --- /dev/null +++ b/src/algorithms/helpers.ts @@ -0,0 +1,122 @@ +type FourDirections = { + RIGHT: { + row: 0; + col: 1; + }; + DOWN: { + row: 1; + col: 0; + }; + LEFT: { + row: 0; + col: -1; + }; + UP: { + row: -1; + col: 0; + }; +}; + +type EightDirections = FourDirections & { + DOWN_RIGHT: { + row: 1; + col: 1; + }; + TOP_RIGHT: { + row: -1; + col: 1; + }; + DOWN_LEFT: { + row: 1; + col: -1; + }; + TOP_LEFT: { + row: -1; + col: -1; + }; +}; + +type ValueOf = T[keyof T]; +export type CrossDirection = ValueOf; +export type StarDirection = ValueOf; + +export type Direction = CrossDirection | StarDirection; + +export type Position = { + row: number; + col: number; +}; + +export const FOUR_DIRECTIONS = { + RIGHT: { + row: 0, + col: 1, + }, + DOWN: { + row: 1, + col: 0, + }, + LEFT: { + row: 0, + col: -1, + }, + UP: { + row: -1, + col: 0, + }, +} satisfies FourDirections; + +export const EIGHT_DIRECTIONS = { + RIGHT: { + row: 0, + col: 1, + }, + DOWN: { + row: 1, + col: 0, + }, + LEFT: { + row: 0, + col: -1, + }, + UP: { + row: -1, + col: 0, + }, + DOWN_RIGHT: { + row: 1, + col: 1, + }, + TOP_RIGHT: { + row: -1, + col: 1, + }, + DOWN_LEFT: { + row: 1, + col: -1, + }, + TOP_LEFT: { + row: -1, + col: -1, + }, +} satisfies EightDirections; + +export const move = (pos: Position, direction: Direction): Position => ({ + row: pos.row + direction.row, + col: pos.col + direction.col, +}); + +export const isSamePos = (p1: Position, p2: Position): boolean => p1.row === p2.row && p1.col === p2.col; + +export const isInBoundaries = (matrix: T[][], current: Position): boolean => { + const maxRows = matrix.length; + const maxCols = matrix[0].length; + + return 0 <= current.row && current.row < maxRows && 0 <= current.col && current.col < maxCols; +}; + +export const isVisited = (visited: boolean[][], current: Position) => { + return visited[current.row][current.col]; +}; + +export const getValueAtPos = (matrix: T[][], position: Position): T => matrix[position.row][position.col]; diff --git a/src/algorithms/paint-group.ts b/src/algorithms/paint-group.ts new file mode 100644 index 0000000..d48e2be --- /dev/null +++ b/src/algorithms/paint-group.ts @@ -0,0 +1,53 @@ +import { FOUR_DIRECTIONS, Position, move } from './helpers'; + +const isInBoundaries = (matrix: number[][], current: Position): boolean => { + const maxRows = matrix.length; + const maxCols = matrix[0].length; + + return 0 <= current.row && current.row < maxRows && 0 <= current.col && current.col < maxCols; +}; + +const isVisited = (visited: boolean[][], current: Position) => { + return visited[current.row][current.col]; +}; + +const isAllowed = (matrix: number[][], current: Position): boolean => { + return matrix[current.row][current.col] === 1; +}; + +const isValidMove = (matrix: number[][], visited: boolean[][], current: Position): boolean => { + return isInBoundaries(matrix, current) && !isVisited(visited, current) && isAllowed(matrix, current); +}; + +const paintGroup = (matrix: number[][], start: Position) => { + const visited = matrix.map((row) => row.map(() => false)); + const queue = [start]; + const group = []; + visited[start.row][start.col] = true; + + while (queue.length) { + console.log('QUEUE', queue); + const p = queue.shift() as Position; + + group.push(p); + + for (const direction of Object.values(FOUR_DIRECTIONS)) { + const next = move(p, direction); + + if (isValidMove(matrix, visited, next)) { + visited[next.row][next.col] = true; + queue.push(next); + } + } + } + + console.log('GROUP', group); +}; + +const matrix = [ + [1, 1, 1], + [1, 1, 0], + [1, 0, 1], +]; + +paintGroup(matrix, { row: 1, col: 1 }); diff --git a/src/algorithms/search-maze.ts b/src/algorithms/search-maze.ts new file mode 100644 index 0000000..83c971e --- /dev/null +++ b/src/algorithms/search-maze.ts @@ -0,0 +1,64 @@ +import { FOUR_DIRECTIONS, Position, isSamePos, move } from './helpers'; + +const isInBoundaries = (matrix: number[][], current: Position): boolean => { + const maxRows = matrix.length; + const maxCols = matrix[0].length; + + return 0 <= current.row && current.row < maxRows && 0 <= current.col && current.col < maxCols; +}; + +const isAllowed = (matrix: number[][], current: Position): boolean => { + console.log('MATRIX', matrix); + console.log('CURRENT', current); + return matrix[current.row][current.col] !== 1; +}; + +const isValidMode = (matrix: number[][], current: Position): boolean => { + return isInBoundaries(matrix, current) && isAllowed(matrix, current); +}; + +const searchMazeHelper = (matrix: number[][], start: Position, end: Position, path: Position[]) => { + if (isSamePos(start, end)) { + return true; + } + + for (const direction of Object.values(FOUR_DIRECTIONS)) { + const next = move(start, direction); + + if (isValidMode(matrix, next)) { + matrix[next.row][next.col] = 1; + + if (searchMazeHelper(matrix, next, end, path)) { + path.push(next); + return true; + } + } + } + + return false; +}; + +const hasPath = (matrix: number[][], start: Position, end: Position) => { + matrix[start.row][start.col] = 1; + + const path: Position[] = []; + + const result = searchMazeHelper(matrix, start, end, path); + path.push(start); + path.reverse(); + + console.log('IS THERE A PATH', result); + console.log('PATH', path); +}; + +const maze = [ + [0, 0, 1, 0, 0], + [0, 0, 0, 0, 0], + [0, 0, 0, 1, 0], + [1, 1, 0, 1, 1], + [0, 0, 0, 0, 0], +]; + +hasPath(maze, { row: 0, col: 4 }, { row: 3, col: 0 }); + +// console.log('RESULT', result); diff --git a/src/components/Controls/Range/index.tsx b/src/components/Controls/Range/index.tsx index 9e2902c..887acc0 100644 --- a/src/components/Controls/Range/index.tsx +++ b/src/components/Controls/Range/index.tsx @@ -6,6 +6,7 @@ type Props = { }; const Range = ({ value, onChange, min = 0, max }: Props): JSX.Element => { + console.log('pula13'); return (
diff --git a/src/problems/toptal-1.ts b/src/problems/toptal-1.ts new file mode 100644 index 0000000..39171e6 --- /dev/null +++ b/src/problems/toptal-1.ts @@ -0,0 +1,72 @@ +const checkMirror = (idxArr: number[], val: number, B: number[]): boolean => { + for (const nIdx of idxArr) { + if (B[nIdx] === val) { + return true; + } + } + + return false; +}; + +const p1 = (A: number[], B: number[]): number => { + const m: Map = new Map(); + + A.forEach((n, i) => { + const prev = m.get(n); + + if (!prev) { + m.set(n, [i]); + } else { + prev.push(i); + } + }); + + console.log('M', m); + console.log('\n'); + + const maxInt = Math.max(...A); + + for (let i = 1; i <= maxInt; i += 1) { + const current = m.get(i); + + if (!current) { + return i; + } + + if (!checkMirror(current, i, B)) { + return i; + } + } + + return maxInt + 1; +}; + +const A1 = [1, 2, 4, 3]; +const B1 = [1, 3, 2, 3]; + +const result1 = 2; + +const myResult1 = p1(A1, B1); + +console.log(`First test ${A1}, ${B1} Corect result = ${result1}`); +console.log(`My result ${myResult1} is correct ${myResult1 === result1}`); + +const A2 = [3, 2, 1, 6, 5]; +const B2 = [4, 2, 1, 3, 3]; + +const result2 = 3; + +const myResult2 = p1(A2, B2); + +console.log(`Second test ${A2}, ${B2} Corect result = ${result2}`); +console.log(`My result ${myResult2} is correct ${myResult2 === result2}`); + +const A3 = [1, 2]; +const B3 = [1, 2]; + +const result3 = 3; + +const myResult3 = p1(A3, B3); + +console.log(`Second test ${A3}, ${B3} Corect result = ${result3}`); +console.log(`My result ${myResult3} is correct ${myResult3 === result3}`); diff --git a/src/problems/toptal-2.ts b/src/problems/toptal-2.ts new file mode 100644 index 0000000..c9b1af2 --- /dev/null +++ b/src/problems/toptal-2.ts @@ -0,0 +1,66 @@ +const p2 = (T: number[]): number[] => { + const adj: { [k: number]: number[] } = {}; + let capital; + + T.forEach((val, idx) => { + if (val == idx) { + capital = val; + + return; + } + + if (adj[val]) { + adj[val].push(idx); + } else { + adj[val] = [idx]; + } + }); + + if (!capital) { + throw Error('no capital'); + } + + const queue: number[] = []; + + queue.push(capital); + + const result = Array.from({ length: T.length - 1 }).fill(0) as number[]; + let count = 0; + let resIdx = 0; + + while (queue.length) { + const size = queue.length; + for (let i = 0; i < size; i += 1) { + const current = queue.shift() as number; + + count += (adj[current] || []).length; + + queue.push(...(adj[current] || [])); + } + + result[resIdx] = count; + resIdx += 1; + count = 0; + } + + return result; +}; + +const inputs = [[9, 1, 4, 9, 0, 4, 8, 9, 0, 1]]; + +const results = [[1, 3, 2, 3, 0, 0, 0, 0, 0]]; + +const runTest = (input: number[], result: number[], solution: (input: number[]) => number[]) => { + const myResult = solution(input); + + console.log(`First test input = ${input} Corect result = ${result}`); + console.log(`My result ${myResult} is correct?\n${JSON.stringify(myResult) === JSON.stringify(result)}`); +}; + +const runTests = (inputs: number[][], result: number[][], solution: (input: number[]) => number[]) => { + for (const idx in inputs) { + runTest(inputs[idx], results[idx], solution); + } +}; + +runTests(inputs, results, p2); diff --git a/src/problems/toptal-3.ts b/src/problems/toptal-3.ts new file mode 100644 index 0000000..39484f6 --- /dev/null +++ b/src/problems/toptal-3.ts @@ -0,0 +1,41 @@ +const p3 = ({ A, B, C }: Input): number => { + const res = 0; + + return res < 1000000000 ? res : -1; +}; + +type Input = { + A: number[]; + B: number[]; + C: number[]; +}; + +const inputs1: Input[] = [ + { + A: [29, 50], + B: [37, 61], + C: [37, 70], + }, +]; + +// A: [29, 50, 70], +// B: [11, 37, 61], + +// 11, 29, 37, 50, 61, 70 + +const results1 = [3]; + +const runTest1 = (input: Input, result: number, solution: (input: Input) => number) => { + const myResult = solution(input); + + console.log(`First test input = ${JSON.stringify(input)} Corect result = ${result}`); + console.log(`My result ${myResult} is correct?\n${JSON.stringify(myResult) === JSON.stringify(result)}`); +}; + +const runTests1 = (inputs: Input[], results: number[], solution: (input: Input) => number) => { + for (const idx in inputs) { + runTest1(inputs[idx], results[idx], solution); + } +}; + +runTests1(inputs1, results1, p3); diff --git a/tsconfig.dev.json b/tsconfig.dev.json new file mode 100644 index 0000000..6f3b7f0 --- /dev/null +++ b/tsconfig.dev.json @@ -0,0 +1,15 @@ +{ + "extends": "ts-node/node16/tsconfig.json", + + "ts-node": { + "transpileOnly": true, + + "files": true, + + "compilerOptions": { + "moduleResolution": "NodeNext" + } + }, + "compilerOptions": { + } +} \ No newline at end of file