diff --git a/challenges/euler/constants.js b/challenges/euler/constants.js new file mode 100644 index 0000000..094850b --- /dev/null +++ b/challenges/euler/constants.js @@ -0,0 +1,6 @@ +const path = require('path'); + +module.exports = { + resources: path.join(__dirname, 'resources'), + src: path.join(__dirname, 'src') +}; diff --git a/challenges/euler/constants.ts b/challenges/euler/constants.ts deleted file mode 100644 index 88d5120..0000000 --- a/challenges/euler/constants.ts +++ /dev/null @@ -1,3 +0,0 @@ -import path from 'path'; - -export const resources = path.join(__dirname, 'resources'); diff --git a/challenges/euler/package.json b/challenges/euler/package.json index ad8ede7..ad24fc1 100644 --- a/challenges/euler/package.json +++ b/challenges/euler/package.json @@ -2,13 +2,16 @@ "name": "project-euler", "scripts": { "build": "gulp build", - "start": "node run.js" + "start": "node scripts/run", + "new": "node scripts/generate" }, "devDependencies": { "@swc/core": "^1.2.125", "@swc/helpers": "^0.3.2", "@types/node": "^16.11.6", + "axios": "^0.24.0", "chalk": "^4.1.2", + "cheerio": "^1.0.0-rc.10", "execution-time": "^1.4.1", "regenerator-runtime": "^0.13.9", "ts-node": "^10.4.0", diff --git a/challenges/euler/pnpm-lock.yaml b/challenges/euler/pnpm-lock.yaml index 2b59a05..bf851e9 100644 --- a/challenges/euler/pnpm-lock.yaml +++ b/challenges/euler/pnpm-lock.yaml @@ -4,7 +4,9 @@ specifiers: '@swc/core': ^1.2.125 '@swc/helpers': ^0.3.2 '@types/node': ^16.11.6 + axios: ^0.24.0 chalk: ^4.1.2 + cheerio: ^1.0.0-rc.10 execution-time: ^1.4.1 regenerator-runtime: ^0.13.9 ts-node: ^10.4.0 @@ -14,7 +16,9 @@ devDependencies: '@swc/core': 1.2.125 '@swc/helpers': 0.3.2 '@types/node': 16.11.6 + axios: 0.24.0 chalk: 4.1.2 + cheerio: 1.0.0-rc.10 execution-time: 1.4.1 regenerator-runtime: 0.13.9 ts-node: 10.4.0_626351e049b80b142acb2ce48a7f5656 @@ -311,6 +315,21 @@ packages: } dev: true + /axios/0.24.0: + resolution: + { + integrity: sha512-Q6cWsys88HoPgAaFAVUb0WpPk0O8iTeisR9IMqy9G8AbO4NlpVknrnQS03zzF9PGAWgO3cgletO3VjV/P7VztA== + } + dependencies: + follow-redirects: 1.14.6 + transitivePeerDependencies: + - debug + dev: true + + /boolbase/1.0.0: + resolution: { integrity: sha1-aN/1++YMUes3cl6p4+0xDcwed24= } + dev: true + /chalk/4.1.2: resolution: { @@ -322,6 +341,35 @@ packages: supports-color: 7.2.0 dev: true + /cheerio-select/1.5.0: + resolution: + { + integrity: sha512-qocaHPv5ypefh6YNxvnbABM07KMxExbtbfuJoIie3iZXX1ERwYmJcIiRrr9H05ucQP1k28dav8rpdDgjQd8drg== + } + dependencies: + css-select: 4.2.1 + css-what: 5.1.0 + domelementtype: 2.2.0 + domhandler: 4.3.0 + domutils: 2.8.0 + dev: true + + /cheerio/1.0.0-rc.10: + resolution: + { + integrity: sha512-g0J0q/O6mW8z5zxQ3A8E8J1hUgp4SMOvEoW/x84OwyHKe/Zccz83PVT4y5Crcr530FV6NgmKI1qvGTKVl9XXVw== + } + engines: { node: '>= 6' } + dependencies: + cheerio-select: 1.5.0 + dom-serializer: 1.3.2 + domhandler: 4.3.0 + htmlparser2: 6.1.0 + parse5: 6.0.1 + parse5-htmlparser2-tree-adapter: 6.0.1 + tslib: 2.3.1 + dev: true + /color-convert/2.0.1: resolution: { @@ -346,6 +394,27 @@ packages: } dev: true + /css-select/4.2.1: + resolution: + { + integrity: sha512-/aUslKhzkTNCQUB2qTX84lVmfia9NyjP3WpDGtj/WxhwBzWBYUV3DgUpurHTme8UTPcPlAD1DJ+b0nN/t50zDQ== + } + dependencies: + boolbase: 1.0.0 + css-what: 5.1.0 + domhandler: 4.3.0 + domutils: 2.8.0 + nth-check: 2.0.1 + dev: true + + /css-what/5.1.0: + resolution: + { + integrity: sha512-arSMRWIIFY0hV8pIxZMEfmMI47Wj3R/aWpZDDxWYCPEiOMv6tfOrnpDtgxBYPEQD4V0Y/958+1TdC3iWTFcUPw== + } + engines: { node: '>= 6' } + dev: true + /diff/4.0.2: resolution: { @@ -354,6 +423,52 @@ packages: engines: { node: '>=0.3.1' } dev: true + /dom-serializer/1.3.2: + resolution: + { + integrity: sha512-5c54Bk5Dw4qAxNOI1pFEizPSjVsx5+bpJKmL2kPn8JhBUq2q09tTCa3mjijun2NfK78NMouDYNMBkOrPZiS+ig== + } + dependencies: + domelementtype: 2.2.0 + domhandler: 4.3.0 + entities: 2.2.0 + dev: true + + /domelementtype/2.2.0: + resolution: + { + integrity: sha512-DtBMo82pv1dFtUmHyr48beiuq792Sxohr+8Hm9zoxklYPfa6n0Z3Byjj2IV7bmr2IyqClnqEQhfgHJJ5QF0R5A== + } + dev: true + + /domhandler/4.3.0: + resolution: + { + integrity: sha512-fC0aXNQXqKSFTr2wDNZDhsEYjCiYsDWl3D01kwt25hm1YIPyDGHvvi3rw+PLqHAl/m71MaiF7d5zvBr0p5UB2g== + } + engines: { node: '>= 4' } + dependencies: + domelementtype: 2.2.0 + dev: true + + /domutils/2.8.0: + resolution: + { + integrity: sha512-w96Cjofp72M5IIhpjgobBimYEfoPjx1Vx0BSX9P30WBdZW2WIKU0T1Bd0kz2eNZ9ikjKgHbEyKx8BB6H1L3h3A== + } + dependencies: + dom-serializer: 1.3.2 + domelementtype: 2.2.0 + domhandler: 4.3.0 + dev: true + + /entities/2.2.0: + resolution: + { + integrity: sha512-p92if5Nz619I0w+akJrLZH0MX0Pb5DX39XOwQTtXSdQQOaYH03S1uIQp4mhOZtAXrxq4ViO67YTiLBo2638o9A== + } + dev: true + /execution-time/1.4.1: resolution: { @@ -363,6 +478,19 @@ packages: pretty-hrtime: 1.0.3 dev: true + /follow-redirects/1.14.6: + resolution: + { + integrity: sha512-fhUl5EwSJbbl8AR+uYL2KQDxLkdSjZGR36xy46AO7cOMTrCMON6Sa28FmAnC2tRTDbd/Uuzz3aJBv7EBN7JH8A== + } + engines: { node: '>=4.0' } + peerDependencies: + debug: '*' + peerDependenciesMeta: + debug: + optional: true + dev: true + /has-flag/4.0.0: resolution: { @@ -371,6 +499,18 @@ packages: engines: { node: '>=8' } dev: true + /htmlparser2/6.1.0: + resolution: + { + integrity: sha512-gyyPk6rgonLFEDGoeRgQNaEUvdJ4ktTmmUh/h2t7s+M8oPpIPxgNACWa+6ESR57kXstwqPiCut0V8NRpcwgU7A== + } + dependencies: + domelementtype: 2.2.0 + domhandler: 4.3.0 + domutils: 2.8.0 + entities: 2.2.0 + dev: true + /make-error/1.3.6: resolution: { @@ -378,6 +518,31 @@ packages: } dev: true + /nth-check/2.0.1: + resolution: + { + integrity: sha512-it1vE95zF6dTT9lBsYbxvqh0Soy4SPowchj0UBGj/V6cTPnXXtQOPUbhZ6CmGzAD/rW22LQK6E96pcdJXk4A4w== + } + dependencies: + boolbase: 1.0.0 + dev: true + + /parse5-htmlparser2-tree-adapter/6.0.1: + resolution: + { + integrity: sha512-qPuWvbLgvDGilKc5BoicRovlT4MtYT6JfJyBOMDsKoiT+GiuP5qyrPCnR9HcPECIJJmZh5jRndyNThnhhb/vlA== + } + dependencies: + parse5: 6.0.1 + dev: true + + /parse5/6.0.1: + resolution: + { + integrity: sha512-Ofn/CTFzRGTTxwpNEs9PP93gXShHcTq255nzRYSKe8AkVpZY7e1fpmTfOyoIvjP5HG7Z2ZM7VS9PPhQGW2pOpw== + } + dev: true + /pretty-hrtime/1.0.3: resolution: { integrity: sha1-t+PqQkNaTJsnWdmeDyAesZWALuE= } engines: { node: '>= 0.8' } @@ -434,6 +599,13 @@ packages: yn: 3.1.1 dev: true + /tslib/2.3.1: + resolution: + { + integrity: sha512-77EbyPPpMz+FRFRuAFlWMtmgUWGe9UOG2Z25NqCwiIjRhOf5iKGuzSe5P2w1laq+FkRy4p+PCuVkJSGkzTEKVw== + } + dev: true + /typescript/4.4.4: resolution: { diff --git a/challenges/euler/scripts/generate.js b/challenges/euler/scripts/generate.js new file mode 100644 index 0000000..9109bc5 --- /dev/null +++ b/challenges/euler/scripts/generate.js @@ -0,0 +1,51 @@ +const inquirer = require('inquirer'); +const fs = require('fs'); +const path = require('path'); +const { src } = require('../constants'); +const axios = require('axios'); +const cheerio = require('cheerio'); + +const problems = fs + .readFileSync(path.join(__dirname, '..', 'readme.md')) + .toString() + .match(/^- \[.] (?:\[(.*)\]|(.*))/gm) + .map(res => { + const sanitised = res.substring(8).replace('[', '').replace(']', ''); + return sanitised.match(/[0-9]* - (.*)/)[1]; + }); + +inquirer + .prompt([ + { + name: 'problemNumber', + message: 'Which problem would you like to solve?', + type: 'number', + validate: input => + parseInt(input) > 100 + ? 'Please make sure you choose a number between 1 and 100!' + : true + } + ]) + .then(async ({ problemNumber }) => { + const problemName = problems[problemNumber - 1]; + + // Fetch the problem data off of projecteuler.net + axios.get(`https://projecteuler.net/problem=${problemNumber}`).then(({ data }) => { + const $ = cheerio.load(data); + const problemContent = $('.problem_content') + .text() + .trim() + .split('\n') + .map(r => `// ${r}`) + .join('\n'); + + fs.writeFileSync( + path.join(src, `${problemNumber} - ${problemName}.ts`), + `${problemContent} +export = {}; + +// Output +console.log();` + ); + }); + }); diff --git a/challenges/euler/run.js b/challenges/euler/scripts/run.js similarity index 100% rename from challenges/euler/run.js rename to challenges/euler/scripts/run.js diff --git a/challenges/euler/src/67 - Maximum path sum II.ts b/challenges/euler/src/67 - Maximum path sum II.ts index 9df1078..c363f31 100644 --- a/challenges/euler/src/67 - Maximum path sum II.ts +++ b/challenges/euler/src/67 - Maximum path sum II.ts @@ -8,7 +8,7 @@ import fs from 'fs'; import path from 'path'; -import { resources } from '../constants'; +import { resources } from '../constants.js'; export {}; // Same method as 18 - Maximum path sum I diff --git a/challenges/euler/tsconfig.json b/challenges/euler/tsconfig.json index 3284377..26e218d 100644 --- a/challenges/euler/tsconfig.json +++ b/challenges/euler/tsconfig.json @@ -3,7 +3,8 @@ "target": "es5", "module": "commonjs", "outDir": "build", - "esModuleInterop": true + "esModuleInterop": true, + "allowJs": true }, "include": ["src/**/*.ts"], "ts-node": {