-
Notifications
You must be signed in to change notification settings - Fork 0
/
trans.ts
65 lines (59 loc) · 1.57 KB
/
trans.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/env node
import * as fs from 'fs'
import * as sb from 'structure-bytes'
import {Puzzle, puzzleType} from './types'
// Parse args
const {argv} = process
function Usage(): void {
Error.stackTraceLimit = 0
throw new Error('Usage: ' + argv[1].split('/').pop() + ' path/to/cagings.sbv [-id string]')
}
if (argv.length <= 2) Usage() // need the file
let id = argv[2]
for(let n=3; n<argv.length; n++) { // check for explicit ID
const arg = argv[n]
if (arg === '-id') {
id = argv[++n]
continue
}
Usage()
}
const readPuzzle = new Promise<Puzzle>((resolve, reject) => {
sb.readValue({
type: puzzleType,
inStream: fs.createReadStream(argv[2])
}, (err, value) => {
if (err) reject(err)
else resolve(value!)
})
})
readPuzzle.then((puzzle) => {
// Output the puzzle in readable format
const ltr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnop'
const {max, cages} = puzzle!
let grid: string[] =
[...Array(max).keys()].map(() => '.'.repeat(max))
let n = -1
for(const cage of cages) {
if (cage.boxes.length > 1) n++
for(const box of cage.boxes) {
const [r, c] = box
const chr = (cage.op === '=') ?
String(cage.val) : ltr.substr(n,1)
grid[r] = grid[r].substr(0,c) + chr +
grid[r].substr(c+1)
}
}
process.stdout.write('.KK "' + id + '"\n')
grid.map((s) => process.stdout.write(s + '\n'))
process.stdout.write('\n')
n = 0
for(const cage of cages) {
if (cage.boxes.length === 1) continue
const chr = ltr.substr(n,1)
let op = (cage.op !== '*') ? cage.op : 'x'
process.stdout.write(chr + ' ' + cage.val + op + '\n')
n++
}
})
.catch(console.error)