Skip to content

Commit bf480cc

Browse files
committed
Add codingame-ghost-legs
1 parent 839423f commit bf480cc

File tree

8 files changed

+186
-1
lines changed

8 files changed

+186
-1
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ This is a collection of some of my submissions to code challenges/contests.
2424
| [`brackets-extreme-edition`](./challenges/codingame-brackets-extreme-edition) | codingame |
2525
| [`defibrillators`](./challenges/codingame-defibrillators) | codingame |
2626
| [`dont-panic-episode-1`](./challenges/codingame-dont-panic-episode-1) | codingame |
27+
| [`ghost-legs`](./challenges/codingame-ghost-legs) | codingame |
2728
| [`horse-racing-duals`](./challenges/codingame-horse-racing-duals) | codingame |
2829
| [`mars-lander-episode-1`](./challenges/codingame-mars-lander-episode-1) | codingame |
2930
| [`mime-type`](./challenges/codingame-mime-type) | codingame |
@@ -71,6 +72,7 @@ ___
7172
| [`defibrillators`](./challenges/codingame-defibrillators) | Classic |
7273
| [`dont-panic-episode-1`](./challenges/codingame-dont-panic-episode-1) | Classic |
7374
| [`fall-challenge-2020`](./challenges/codingame-fall-challenge-2020) | Contest |
75+
| [`ghost-legs`](./challenges/codingame-ghost-legs) | Classic |
7476
| [`horse-racing-duals`](./challenges/codingame-horse-racing-duals) | Classic |
7577
| [`mars-lander-episode-1`](./challenges/codingame-mars-lander-episode-1) | Classic |
7678
| [`mime-type`](./challenges/codingame-mime-type) | Classic |
@@ -104,6 +106,7 @@ ___
104106
| [`defibrillators`](./challenges/codingame-defibrillators) | codingame | Classic |
105107
| [`dont-panic-episode-1`](./challenges/codingame-dont-panic-episode-1) | codingame | Classic |
106108
| [`fall-challenge-2020`](./challenges/codingame-fall-challenge-2020) | codingame | Contest |
109+
| [`ghost-legs`](./challenges/codingame-ghost-legs) | codingame | Classic |
107110
| [`horse-racing-duals`](./challenges/codingame-horse-racing-duals) | codingame | Classic |
108111
| [`mars-lander-episode-1`](./challenges/codingame-mars-lander-episode-1) | codingame | Classic |
109112
| [`mime-type`](./challenges/codingame-mime-type) | codingame | Classic |
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# codingame-ghost-legs
2+
https://www.codingame.com/training/easy/ghost-legs
3+
4+
**Type:** Classic
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import java.util.*;
2+
import java.io.*;
3+
import java.math.*;
4+
5+
class Solution {
6+
public static void main(String args[]) {
7+
Scanner in = new Scanner(System.in);
8+
int w = in.nextInt();
9+
int h = in.nextInt();
10+
if (in.hasNextLine()) {
11+
in.nextLine();
12+
}
13+
String[] topLabels = in.nextLine().split(" ");
14+
int labelsCount = topLabels.length;
15+
char[][] grid = new char[labelsCount][h - 2];
16+
for (int i = 0; i < h - 2; i++) {
17+
String line = in.nextLine();
18+
for (int j = 0; j < labelsCount; j++) {
19+
int pipePosition = j * 3;
20+
char direction = 'E';
21+
if (pipePosition > 0 && line.charAt(pipePosition - 1) == '-')
22+
direction = 'L';
23+
else if (pipePosition < w - 1 && line.charAt(pipePosition + 1) == '-')
24+
direction = 'R';
25+
grid[j][i] = direction;
26+
}
27+
}
28+
String[] bottomLabels = in.nextLine().split(" ");
29+
30+
// Find the output
31+
for (int i = 0; i < topLabels.length; i++) {
32+
int currentColumnIndex = i;
33+
for (int j = 0; j < grid[0].length; j++) {
34+
if (grid[currentColumnIndex][j] == 'R')
35+
currentColumnIndex++;
36+
else if (grid[currentColumnIndex][j] == 'L')
37+
currentColumnIndex--;
38+
}
39+
System.out.println(topLabels[i] + "" + bottomLabels[currentColumnIndex]);
40+
}
41+
System.err.println(grid);
42+
}
43+
}
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: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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 [w, h] = readline()
8+
.split(' ')
9+
.map(x => +x)
10+
11+
type Cell = 'E' | 'L' | 'R'
12+
13+
let topLabels = readline().split(' ')
14+
15+
const labelsCount = topLabels.length
16+
let grid: Cell[][] = Array.from({ length: labelsCount }, () => Array.from({ length: h - 2 }, () => 'E'))
17+
18+
for (let i = 0; i < h - 2; i++) {
19+
const line: string = readline()
20+
for (let j = 0; j < labelsCount; j++) {
21+
let pipePosition = j * 3
22+
let direction: Cell
23+
if (pipePosition > 0 && line.charAt(pipePosition - 1) === '-') direction = 'L'
24+
else if (pipePosition < w - 1 && line.charAt(pipePosition + 1) === '-') direction = 'R'
25+
else direction = 'E'
26+
grid[j][i] = direction
27+
}
28+
}
29+
30+
let bottomLabels = readline().split(' ')
31+
32+
// Find the output
33+
for (let i = 0; i < topLabels.length; i++) {
34+
let currentColumnIndex = i
35+
for (let j = 0; j < grid[0].length; j++) {
36+
if (grid[currentColumnIndex][j] === 'R') currentColumnIndex++
37+
else if (grid[currentColumnIndex][j] === 'L') currentColumnIndex--
38+
}
39+
console.log(`${topLabels[i]}${bottomLabels[currentColumnIndex]}`)
40+
}
41+
42+
console.error(grid)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"name": "codingame-ghost-legs",
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+
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "root",
2+
"name": "code-challenges",
33
"private": true,
44
"author": {
55
"name": "rigwild",

0 commit comments

Comments
 (0)