-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
67 lines (53 loc) · 924 Bytes
/
main.go
File metadata and controls
67 lines (53 loc) · 924 Bytes
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
package main
import (
"encoding/csv"
"fmt"
"io"
"os"
"path"
)
func solve(r io.Reader) {
csvReader := csv.NewReader(r)
rows, err := csvReader.ReadAll()
if err != nil {
panic(err)
}
max := 0
for _, row := range rows {
if getId(row[0]) > max {
max = getId(row[0])
}
}
fmt.Println(max)
}
func getId(pass string) int {
min, max := 0, 128
for i := 0; i < 7; i++ {
if pass[i] == 'F' {
max -= (max - min) / 2
} else {
min += (max - min) / 2
}
}
row := min
colMin, colMax := 0, 8
for i := 0; i < 3; i++ {
if pass[7+i] == 'L' {
colMax -= (colMax - colMin) / 2
} else {
colMin += (colMax - colMin) / 2
}
}
col := colMin
return row*8 + col
}
func main() {
input, err := os.Open(path.Join("2020", "5", "input.txt"))
if err != nil {
panic(err)
}
solve(input)
fmt.Println(getId("BFFFBBFRRR"))
fmt.Println(getId("FFFBBBFRRR"))
fmt.Println(getId("BBFFBBFRLL"))
}