-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
69 lines (57 loc) · 1.09 KB
/
main.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package main
import (
"bufio"
"fmt"
"os"
"sort"
)
func main() {
var input []string
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
input = append(input, scanner.Text())
}
fmt.Printf("Part 1: %d\n", highestSeatID(input))
fmt.Printf("Part 2: %d\n", findMySeatID(input))
}
func highestSeatID(specs []string) int {
var highestID int
for _, spec := range specs {
_, _, id := parseSpec(spec)
if id > highestID {
highestID = id
}
}
return highestID
}
func findMySeatID(specs []string) int {
ids := make([]int, 0, len(specs))
for _, spec := range specs {
_, _, id := parseSpec(spec)
ids = append(ids, id)
}
return findMissing(ids)
}
func findMissing(s []int) int {
sort.Ints(s)
for i := 1; i < len(s); i++ {
if s[i]-s[i-1] > 1 {
return s[i] - 1
}
}
return 0
}
func parseSpec(spec string) (row, col, id int) {
parseBase2 := func(spec string, one rune) (dec int) {
for _, r := range spec {
dec *= 2
if r == one {
dec++
}
}
return dec
}
row = parseBase2(spec[:7], 'B')
col = parseBase2(spec[7:], 'R')
return row, col, row*8 + col
}