Skip to content

Instantly share code, notes, and snippets.

@suntong
Last active April 1, 2023 23:00
Show Gist options
  • Save suntong/b7fae6f3d212e0429003428c7180e961 to your computer and use it in GitHub Desktop.
Save suntong/b7fae6f3d212e0429003428c7180e961 to your computer and use it in GitHub Desktop.
func solution(cell string) int {
p, c := NewPos(cell), 0
for _, v := range p.Moves() {
if p.Move(v).isOn() { c++ }
}
return c
}
type pos struct {
x, y int
}
func NewPos(cell string) pos {
return pos{int(cell[0]-'a'),int(cell[1]-'1')}
}
func (p pos) isOn() bool {
return p.x >=0 && p.x <=7 && p.y >=0 && p.y <=7
}
func (p pos) Move(xy []int) pos {
r := p
r.x += xy[0]
r.y += xy[1]
return r
}
func (p *pos) Moves() [][]int {
return [][]int{
{-2, 1}, {-1, 2}, {1, 2}, {2, 1},
{-2, -1}, {-1, -2}, {1, -2}, {2, -1},
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment