Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
I don't remember the context in which I wrote this, but I did! Finished it Jan 2017
interface Boat {
size: number
direction: 'horizontal'|'vertical'
start: Point
}
interface Point {
x: number,
y: number
}
class Grid {
grid: boolean[][]
boats: Boat[]
constructor() {
this.grid = this._makeGrid()
this.boats = []
}
getCount (side: 'top'|'left') {
// iterate over, return array length 10 with sums for each
let vals: number[] = []
if (side === 'left') {
vals = this.grid.map(r => r.filter(i => i).length)
} else {
for (let i = 0; i < this.grid.length; i++) {
let col = this.grid.map(r => r[i])
vals.push(col.filter(j => j).length)
}
}
return vals
}
getBoatTally () {
let res: {[key: string]: number} = {
size1: 0,
size2: 0,
size3: 0,
size4: 0
}
this.boats.forEach(b => {
// ignore boats that are too big
if (res[`size${b.size}`]) {
res[`size${b.size}`] += 1
}
})
return res
}
getMin (points: Point[]) {
if (points.length === 0) {
return null
}
else if (points.length === 1) {
return points[0]
} else {
let sorted: Point[]
if (points[0].x === points[1].x) {
sorted = points.sort((a, b) => a.y - b.y)
} else {
sorted = points.sort((a, b) => a.x - b.x)
}
return sorted[0]
}
}
toggle (p: Point) {
this.grid[p.x][p.y] = !this.grid[p.x][p.y]
}
private _makeGrid () {
let size = 10, res: boolean[][]
res = [...Array(10)].map(x => Array(10).fill(false))
return res
}
}
let g = new Grid()
// console.log(g.grid)
// g.toggle({x: 1, y: 2})
// console.log(g.grid)
console.log(g.getMin([{x: 3, y: 3}, {x: 5, y: 3}, {x: 6, y: 3}, {x: 7, y: 2}, {x: 1, y: 3}]))
// b.size = 'd'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment