add new solutions
This commit is contained in:
parent
489fa73880
commit
0f5f9e331c
11 changed files with 539 additions and 0 deletions
45
solutions/4/q401/solution.go
Normal file
45
solutions/4/q401/solution.go
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
package q401
|
||||
|
||||
import "fmt"
|
||||
|
||||
func bin2time(leds []uint8) (string, bool) {
|
||||
hour := leds[0]<<3 + leds[1]<<2 + leds[2]<<1 + leds[3]
|
||||
minute := leds[4]<<5 +
|
||||
leds[5]<<4 +
|
||||
leds[6]<<3 +
|
||||
leds[7]<<2 +
|
||||
leds[8]<<1 +
|
||||
leds[9]
|
||||
if hour >= 12 || minute >= 60 {
|
||||
return "", false
|
||||
}
|
||||
return fmt.Sprintf("%d:%02d", hour, minute), true
|
||||
}
|
||||
|
||||
func iterate(remaining, offset int, leds []uint8, ret *[]string) {
|
||||
if 10-offset < remaining {
|
||||
return
|
||||
}
|
||||
if remaining == 0 {
|
||||
if r, ok := bin2time(leds); ok {
|
||||
*ret = append(*ret, r)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
leds[offset] = 1
|
||||
iterate(remaining-1, offset+1, leds, ret)
|
||||
leds[offset] = 0
|
||||
iterate(remaining, offset+1, leds, ret)
|
||||
}
|
||||
|
||||
func readBinaryWatch(turnedOn int) []string {
|
||||
if turnedOn >= 9 {
|
||||
return nil
|
||||
}
|
||||
ret := []string{}
|
||||
iterate(turnedOn, 0, make([]uint8, 10), &ret)
|
||||
return ret
|
||||
}
|
||||
|
||||
var _ = readBinaryWatch
|
||||
Loading…
Add table
Add a link
Reference in a new issue