-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseats.go
48 lines (38 loc) · 1.07 KB
/
seats.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package main
import (
"fmt"
"strconv"
"strings"
)
type seatAssignment struct {
row int
column int
id int
binarySpacePartition string
}
func (s seatAssignment) String() string {
return fmt.Sprintf("seat row: %v, col: %v, id: %v", s.row, s.column, s.id)
}
func makeBinary(input string, zero rune, one rune) int {
input = strings.ReplaceAll(input, string(zero), "0")
input = strings.ReplaceAll(input, string(one), "1")
newInt, err := strconv.ParseInt(input, 2, 32)
check(err)
return int(newInt)
}
func makeSeatAssignment(binarySpacePartition string) seatAssignment {
seat := seatAssignment{binarySpacePartition: binarySpacePartition}
if len(binarySpacePartition) < 10 {
return seat
}
seat.row = makeBinary(binarySpacePartition[0:7], 'F', 'B')
seat.column = makeBinary(binarySpacePartition[7:10], 'L', 'R')
seat.id = (seat.row * 8) + seat.column
return seat
}
func quickSeatAssignment(inputs <-chan string, seats chan<- seatAssignment) {
for input := range inputs {
seats <- makeSeatAssignment(input)
}
close(seats)
}