From 42fc6f545f62c2f0db5427853c8b52fc7f00fe5a Mon Sep 17 00:00:00 2001 From: VicRen Date: Fri, 27 Nov 2020 09:04:10 +0800 Subject: [PATCH] 20201127 --- 20201127/main.go | 70 ++++++++++++++++++ 20201127/main_test.go | 162 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 232 insertions(+) create mode 100644 20201127/main.go create mode 100644 20201127/main_test.go diff --git a/20201127/main.go b/20201127/main.go new file mode 100644 index 0000000..b3f20ed --- /dev/null +++ b/20201127/main.go @@ -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 + } +} diff --git a/20201127/main_test.go b/20201127/main_test.go new file mode 100644 index 0000000..6bb3be9 --- /dev/null +++ b/20201127/main_test.go @@ -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) + } + }) + } +}