This commit is contained in:
VicRen
2020-11-27 09:04:10 +08:00
parent 21a2b9cd69
commit 42fc6f545f
2 changed files with 232 additions and 0 deletions

70
20201127/main.go Normal file
View File

@@ -0,0 +1,70 @@
package main
func main() {
}
func hasCross(orders string) bool {
pm := map[[2]int]struct{}{}
current := [2]int{0, 0}
pm[current] = struct{}{}
for _, o := range orders {
np := nextPos(current, string(o))
if _, found := pm[np]; found {
return true
}
pm[np] = struct{}{}
}
return false
}
func nextPos(current [2]int, order string) [2]int {
switch order {
case "E":
current[0] += 1
case "S":
current[1] -= 1
case "W":
current[0] -= 1
case "N":
current[1] += 1
}
return current
}
func hasCross2(orders string) bool {
pm := map[pos]struct{}{}
p := &pos{0, 0}
pm[*p] = struct{}{}
for _, o := range orders {
p.Move(string(o))
if _, found := pm[*p]; found {
return true
}
pm[*p] = struct{}{}
}
return false
}
type pos struct {
x int
y int
}
func (p *pos) Equals(p2 *pos) bool {
return p.x == p2.x && p.y == p2.y
}
func (p *pos) Move(order string) {
switch order {
case "E":
p.x += 1
case "S":
p.y -= 1
case "W":
p.x -= 1
case "N":
p.y += 1
}
}

162
20201127/main_test.go Normal file
View File

@@ -0,0 +1,162 @@
package main
import (
"reflect"
"testing"
)
func Test_hasCross(t *testing.T) {
type args struct {
orders string
}
tests := []struct {
name string
args args
want bool
}{
{
"test1",
args{"NES"},
false,
},
{
"test2",
args{"NESWW"},
true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := hasCross(tt.args.orders); got != tt.want {
t.Errorf("hasCross() = %v, want %v", got, tt.want)
}
})
}
}
func Test_nextPos(t *testing.T) {
type args struct {
current [2]int
order string
}
tests := []struct {
name string
args args
want [2]int
}{
{
"test_E",
args{[2]int{0, 0}, "E"},
[2]int{1, 0},
},
{
"test_S",
args{[2]int{0, 0}, "S"},
[2]int{0, -1},
},
{
"test_W",
args{[2]int{0, 0}, "W"},
[2]int{-1, 0},
},
{
"test_N",
args{[2]int{0, 0}, "N"},
[2]int{0, 1},
},
{
"test1",
args{[2]int{0, 0}, "N"},
[2]int{0, 1},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := nextPos(tt.args.current, tt.args.order); !reflect.DeepEqual(got, tt.want) {
t.Errorf("nextPos() = %v, want %v", got, tt.want)
}
})
}
}
func Test_pos_Move(t *testing.T) {
type fields struct {
x int
y int
}
type args struct {
order string
}
tests := []struct {
name string
fields fields
args args
want *pos
}{
{
"test_E",
fields{0, 0},
args{"E"},
&pos{1, 0},
},
{
"test_S",
fields{0, 0},
args{"S"},
&pos{0, -1},
},
{
"test_W",
fields{0, 0},
args{"W"},
&pos{-1, 0},
},
{
"test_N",
fields{0, 0},
args{"N"},
&pos{0, 1},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
p := &pos{
x: tt.fields.x,
y: tt.fields.y,
}
p.Move(tt.args.order)
if !p.Equals(tt.want) {
t.Errorf("pos Move()=%v, want %v", p, tt.want)
}
})
}
}
func Test_hasCross2(t *testing.T) {
type args struct {
orders string
}
tests := []struct {
name string
args args
want bool
}{
{
"test1",
args{"NES"},
false,
},
{
"test2",
args{"NESWW"},
true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := hasCross2(tt.args.orders); got != tt.want {
t.Errorf("hasCross2() = %v, want %v", got, tt.want)
}
})
}
}