Skip to content

Instantly share code, notes, and snippets.

@kana-sama
Created June 15, 2016 18:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kana-sama/2c1b944bef3f5d133e415078203fb480 to your computer and use it in GitHub Desktop.
Save kana-sama/2c1b944bef3f5d133e415078203fb480 to your computer and use it in GitHub Desktop.
function getSize(field) {
return Math.sqrt(field.length)
}
function new2D(size, filler = () => 0) {
return new Array(size).fill()
.map((_, i) => new Array(size).fill()
.map((_, j) => filler(i, j)))
}
function getCommands(field, power) {
const size = getSize(field)
const map = new2D(size, (i, j) => field[i * size + j])
const powers = new2D(size, () => -Infinity)
function goTo([i, j], [pI, pJ], power) {
if (power < 0) {
return { path: false, a: 1 }
}
if (i < 0 || j < 0 || i >= size || j >= size) {
return { path: false, a: 2 }
}
if (map[i][j] === '#') {
return { path: false, a: 3 }
}
if (powers[i][j] >= power) {
return { path: false, a: 4 }
}
powers[i][j] = power
if (map[i][j] === 'T') {
return { path: [[i, j]], power: 0 }
}
return { path: getBestPath([i, j], [pI, pJ], power), power }
}
function getBestPath([i, j], [pI, pJ] = [0, 0], power) {
let pows = [
power - 1 - (pJ !== j - 1 ? 1 : 0) - (pJ === j + 1 ? 1 : 0),
power - 1 - (pJ !== j + 1 ? 1 : 0) - (pJ === j - 1 ? 1 : 0),
power - 1 - (pI !== i - 1 ? 1 : 0) - (pI === i + 1 ? 1 : 0),
power - 1 - (pI !== i + 1 ? 1 : 0) - (pI === i - 1 ? 1 : 0),
]
let paths = [
goTo([i, j + 1], [i, j], pows[0]),
goTo([i, j - 1], [i, j], pows[1]),
goTo([i + 1, j], [i, j], pows[2]),
goTo([i - 1, j], [i, j], pows[3]),
]
paths = paths
.filter(({ path }) => path !== false)
.sort((a, b) => a.power - b.power)
if (paths.length === 0) {
return false
}
return [[i, j], ...paths[0].path]
}
function getStart() {
for (let i in map) {
for (let j in map[i]) {
if (map[i][j] === 'S') {
return [i, j].map(Number)
}
}
}
return false
}
function toCommands(path, dir) {
console.log(path)
const right = {
[[ 1, 0]]: [ 0, -1],
[[ 0, -1]]: [-1, 0],
[[-1, 0]]: [ 0, 1],
[[ 0, 1]]: [ 1, 0],
}
let current = path.shift()
const commands = []
while (path.length > 0) {
let next = path.shift()
const changeI = next[0] - current[0]
const changeJ = next[1] - current[1]
const change = Array.from([changeI, changeJ]).toString()
if (changeI === dir[0] && changeJ === dir[1]) {
commands.push('f')
} else if (changeI === -dir[0] && changeJ === -dir[1]) {
commands.push('r', 'r', 'f')
} else if (right[dir].toString() === change) {
commands.push('r', 'f')
} else {
commands.push('l', 'f')
}
current = next
dir = [changeI, changeJ]
}
return commands
}
const start = getStart()
if (!start) {
return []
}
const [startI, startJ] = start
const path = getBestPath([startI, startJ], [startI + 1, startJ], power)
if (path === false) {
return []
}
return toCommands(path, [-1, 0])
}
'S..'
'...'
'..T'
console.log(getCommands('S.......T', 10))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment