Skip to content

Commit fa5d5f6

Browse files
committed
Add there-is-no-spoon-episode-1
1 parent 9a42fb2 commit fa5d5f6

File tree

7 files changed

+151
-13
lines changed

7 files changed

+151
-13
lines changed

.vscode/workspace.code-workspace

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"commands": [
1010
{
1111
"match": "\\.ts$",
12-
"cmd": "node ${fileDirname}/_copy.js --js",
12+
"cmd": "node ${fileDirname}/_copy.js --js --dist",
1313
"useShortcut": false,
1414
"silent": false
1515
}

_templates/ts-minimal/_copy.js

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,22 +28,12 @@ const run = async () => {
2828
if (process.argv[2] === '--js') {
2929
console.log('Compile TypeScript and copy output')
3030
script = typescript.transpileModule(script, require('./tsconfig.json')).outputText
31-
32-
await clipboardy.write(script)
33-
console.log('Copied script to clipboard.')
3431
}
3532

3633
console.log(`Script SHA256: ${sha256(script)}.`)
3734

38-
// Output file (for CodinGame IDE sync)
39-
if (
40-
!(await fsp.access(r(__dirname, 'dist')).then(
41-
() => true,
42-
() => false
43-
))
44-
)
45-
await fsp.mkdir(r(__dirname, 'dist'))
46-
await fsp.writeFile(r(__dirname, 'dist', 'index.js'), script)
35+
await clipboardy.write(script)
36+
console.log('Copied script to clipboard.')
4737
}
4838

4939
run()
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# codingame-there-is-no-spoon-episode-1
2+
https://www.codingame.com/training/medium/there-is-no-spoon-episode-1
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: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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 [width, height] = [readline(), readline()].map(x => +x)
8+
9+
let mp: string[] = []
10+
for (let i = 0; i < height; i++) mp.push(...readline().split(''))
11+
12+
// @ts-ignore
13+
const nodesIndexes = mp.flatMap((x, i) => (x === '0' ? i : []))
14+
15+
console.error(width, '*', height)
16+
console.error(mp)
17+
console.error('found active nodes', nodesIndexes)
18+
19+
const _1dToCoords = (pos: number) => [pos % width, pos / width].map(Math.floor)
20+
21+
nodesIndexes.forEach(nodePos => {
22+
// Current node
23+
let node = _1dToCoords(nodePos)
24+
console.error('\nnode', node)
25+
26+
// find nearest right
27+
let right = [-1, -1]
28+
for (let i = nodePos + 1; i % width !== 0; i++) {
29+
console.error('i', i)
30+
console.error('i % width', i % width)
31+
if (mp[i] === '0') {
32+
right = _1dToCoords(i)
33+
break
34+
}
35+
}
36+
console.error('right', right)
37+
38+
// find nearest bottom
39+
let bottom = [-1, -1]
40+
for (let i = nodePos + width; i < mp.length; i += width) {
41+
console.error('i', i)
42+
if (mp[i] === '0') {
43+
bottom = _1dToCoords(i)
44+
break
45+
}
46+
}
47+
console.error('bottom', bottom)
48+
49+
// Three coordinates: a node, its right neighbor, its bottom neighbor
50+
console.log([...node, ...right, ...bottom].join(' '))
51+
})
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"name": "codingame-there-is-no-spoon-episode-1",
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)