Skip to content

Commit 6104f5e

Browse files
committed
Add codingame-brackets-extreme-edition and codingame-text-formatting
1 parent 599ee6e commit 6104f5e

File tree

10 files changed

+255
-0
lines changed

10 files changed

+255
-0
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# codingame-brackets-extreme-edition
2+
https://www.codingame.com/training/easy/brackets-extreme-edition
3+
4+
**Type:** Classic
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copy script to clipboard
2+
// Cuts code above `// ------ Everything above this line will get cut when running copy script`
3+
4+
const { promises: fsp } = require('fs')
5+
const { resolve: r } = require('path')
6+
const { createHmac } = require('crypto')
7+
const clipboardy = require('clipboardy')
8+
const typescript = require('typescript')
9+
10+
const file = r(__dirname, 'index.ts')
11+
const separator = '// ------ Everything above this line will get cut when running copy script'
12+
13+
const sha256 = str => createHmac('sha256', 'shitty-salt').update(str).digest('hex')
14+
15+
const run = async () => {
16+
const content = await fsp.readFile(file, { encoding: 'utf8' })
17+
18+
let script = ''
19+
if (content.includes(separator)) script = content.split(separator)[1].trim()
20+
else script = content.trim()
21+
22+
// Wrap in function
23+
script = `;(() => {\n\n${script}\n\n})()`
24+
25+
// Remove top-level TypeScript return ignore
26+
script = script.replace(/\/\/ \@ts-ignore\n\s*return/g, 'return')
27+
28+
if (process.argv.find(x => x === '--js')) {
29+
console.log('Compile TypeScript')
30+
script = typescript.transpileModule(script, require('./tsconfig.json')).outputText
31+
}
32+
if (process.argv.find(x => x === '--copy')) {
33+
await clipboardy.write(script)
34+
console.log('Copied script to clipboard.')
35+
}
36+
if (process.argv.find(x => x === '--dist')) {
37+
// Output file (for CodinGame IDE sync)
38+
if (
39+
!(await fsp.access(r(__dirname, 'dist')).then(
40+
() => true,
41+
() => false
42+
))
43+
)
44+
await fsp.mkdir(r(__dirname, 'dist'))
45+
await fsp.writeFile(r(__dirname, 'dist', 'index.js'), script)
46+
}
47+
48+
console.log(`Script SHA256: ${sha256(script)}.`)
49+
}
50+
51+
run()
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import readlineSync from 'readline-sync'
2+
3+
const readline = () => readlineSync.prompt({ encoding: 'utf-8', prompt: '' })
4+
5+
// ------ Everything above this line will get cut when running copy script
6+
7+
const str = readline()
8+
9+
const stack = []
10+
11+
const charsOpen = '[{('.split('')
12+
const charsClose = ']})'.split('')
13+
14+
let isGood = true
15+
16+
for (let i = 0; i < str.length; i++) {
17+
// console.error(str[i])
18+
19+
if (charsOpen.includes(str[i])) {
20+
stack.push(str[i])
21+
} else if (charsClose.includes(str[i])) {
22+
const lastOpen = stack.pop() as string
23+
console.error(lastOpen, str[i])
24+
if (charsClose.indexOf(str[i]) !== charsOpen.indexOf(lastOpen)) {
25+
isGood = false
26+
break
27+
}
28+
}
29+
console.error()
30+
}
31+
32+
console.error(stack)
33+
34+
console.log(isGood && stack.length === 0)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"name": "codingame-brackets-extreme-edition",
3+
"private": true,
4+
"version": "0.1.0",
5+
"author": {
6+
"name": "rigwild",
7+
"email": "me@rigwild.dev",
8+
"url": "https://rigwild.dev"
9+
},
10+
"scripts": {
11+
"start": "node dist/index.js",
12+
"build": "tsc",
13+
"dev": "tsc && node dist/index.js",
14+
"copy": "node _copy.js"
15+
},
16+
"dependencies": {
17+
"readline-sync": "^1.4.10"
18+
},
19+
"devDependencies": {
20+
"@types/node": "^13.13.5",
21+
"@types/readline-sync": "^1.4.3",
22+
"clipboardy": "^2.3.0",
23+
"ts-node": "^8.10.1",
24+
"typescript": "^3.8.3"
25+
}
26+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"compilerOptions": {
3+
"target": "ES2019",
4+
"module": "commonjs",
5+
"declaration": false,
6+
"declarationMap": false,
7+
"sourceMap": false,
8+
"outDir": "dist",
9+
"strict": true,
10+
"alwaysStrict": true,
11+
"esModuleInterop": true,
12+
"resolveJsonModule": true
13+
},
14+
"include": ["*"],
15+
"exclude": ["node_modules/**/*"]
16+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# codingame-text-formatting
2+
https://www.codingame.com/training/easy/text-formatting
3+
4+
**Type:** Classic
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copy script to clipboard
2+
// Cuts code above `// ------ Everything above this line will get cut when running copy script`
3+
4+
const { promises: fsp } = require('fs')
5+
const { resolve: r } = require('path')
6+
const { createHmac } = require('crypto')
7+
const clipboardy = require('clipboardy')
8+
const typescript = require('typescript')
9+
10+
const file = r(__dirname, 'index.ts')
11+
const separator = '// ------ Everything above this line will get cut when running copy script'
12+
13+
const sha256 = str => createHmac('sha256', 'shitty-salt').update(str).digest('hex')
14+
15+
const run = async () => {
16+
const content = await fsp.readFile(file, { encoding: 'utf8' })
17+
18+
let script = ''
19+
if (content.includes(separator)) script = content.split(separator)[1].trim()
20+
else script = content.trim()
21+
22+
// Wrap in function
23+
script = `;(() => {\n\n${script}\n\n})()`
24+
25+
// Remove top-level TypeScript return ignore
26+
script = script.replace(/\/\/ \@ts-ignore\n\s*return/g, 'return')
27+
28+
if (process.argv.find(x => x === '--js')) {
29+
console.log('Compile TypeScript')
30+
script = typescript.transpileModule(script, require('./tsconfig.json')).outputText
31+
}
32+
if (process.argv.find(x => x === '--copy')) {
33+
await clipboardy.write(script)
34+
console.log('Copied script to clipboard.')
35+
}
36+
if (process.argv.find(x => x === '--dist')) {
37+
// Output file (for CodinGame IDE sync)
38+
if (
39+
!(await fsp.access(r(__dirname, 'dist')).then(
40+
() => true,
41+
() => false
42+
))
43+
)
44+
await fsp.mkdir(r(__dirname, 'dist'))
45+
await fsp.writeFile(r(__dirname, 'dist', 'index.js'), script)
46+
}
47+
48+
console.log(`Script SHA256: ${sha256(script)}.`)
49+
}
50+
51+
run()
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import readlineSync from 'readline-sync'
2+
3+
const readline = () => readlineSync.prompt({ encoding: 'utf-8', prompt: '' })
4+
5+
// ------ Everything above this line will get cut when running copy script
6+
7+
let str: string = readline()
8+
9+
str = str.toLowerCase()
10+
11+
str = str.slice(0, 1).toUpperCase() + str.slice(1)
12+
13+
str = str.replace(/\s*\.+[\s\,]*/g, '. ')
14+
console.error(str)
15+
str = str.replace(/\s*\,+[\s\,]*/g, ', ')
16+
console.error(str)
17+
18+
str = str.replace(/\s\s/g, ' ')
19+
console.error(str)
20+
21+
str = str.replace(/\.\s([a-z])/g, (_, a) => `. ${a.toUpperCase()}`)
22+
console.error(str)
23+
24+
str = str.trim()
25+
console.error(str)
26+
27+
console.log(str)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"name": "codingame-text-formatting",
3+
"private": true,
4+
"version": "0.1.0",
5+
"author": {
6+
"name": "rigwild",
7+
"email": "me@rigwild.dev",
8+
"url": "https://rigwild.dev"
9+
},
10+
"scripts": {
11+
"start": "node dist/index.js",
12+
"build": "tsc",
13+
"dev": "tsc && node dist/index.js",
14+
"copy": "node _copy.js"
15+
},
16+
"dependencies": {
17+
"readline-sync": "^1.4.10"
18+
},
19+
"devDependencies": {
20+
"@types/node": "^13.13.5",
21+
"@types/readline-sync": "^1.4.3",
22+
"clipboardy": "^2.3.0",
23+
"ts-node": "^8.10.1",
24+
"typescript": "^3.8.3"
25+
}
26+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"compilerOptions": {
3+
"target": "ES2019",
4+
"module": "commonjs",
5+
"declaration": false,
6+
"declarationMap": false,
7+
"sourceMap": false,
8+
"outDir": "dist",
9+
"strict": true,
10+
"alwaysStrict": true,
11+
"esModuleInterop": true,
12+
"resolveJsonModule": true
13+
},
14+
"include": ["*"],
15+
"exclude": ["node_modules/**/*"]
16+
}

0 commit comments

Comments
 (0)