diff --git a/20210114/main.go b/20210114/main.go new file mode 100644 index 0000000..88b395f --- /dev/null +++ b/20210114/main.go @@ -0,0 +1,37 @@ +package main + +import "fmt" + +func main() { + +} + +func readBinaryWatch(num int) []string { + var ret []string + for h := 0; h <= 12; h++ { + hCount := 0 + s := fmt.Sprintf("%#b", h) + for _, c := range s { + if c == '1' { + hCount++ + } + } + for m := 0; m <= 60; m++ { + mCount := 0 + s := fmt.Sprintf("%#b", m) + for _, c := range s { + if c == '1' { + mCount++ + } + } + if hCount+mCount == num { + if m > 10 { + ret = append(ret, fmt.Sprintf("%d:%d", h, m)) + } else { + ret = append(ret, fmt.Sprintf("%d:0%d", h, m)) + } + } + } + } + return ret +} diff --git a/20210114/main_test.go b/20210114/main_test.go new file mode 100644 index 0000000..5f0e5be --- /dev/null +++ b/20210114/main_test.go @@ -0,0 +1,36 @@ +package main + +import ( + "reflect" + "sort" + "testing" +) + +func Test_readBinaryWatch(t *testing.T) { + type args struct { + num int + } + tests := []struct { + name string + args args + want []string + }{ + { + "test1", + args{ + 1, + }, + []string{"1:00", "2:00", "4:00", "8:00", "0:01", "0:02", "0:04", "0:08", "0:16", "0:32"}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := readBinaryWatch(tt.args.num) + sort.Strings(got) + sort.Strings(tt.want) + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("readBinaryWatch() = %v, want %v", got, tt.want) + } + }) + } +}