-
Notifications
You must be signed in to change notification settings - Fork 0
/
5.go
74 lines (65 loc) · 1.17 KB
/
5.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
70
71
72
73
74
package main
import "fmt"
type seat struct {
row int
column int
id int
}
func main5() {
input := getSeats()
var seats []seat
for _, identifier := range input {
seats = append(seats, findSeat(identifier))
}
biggestSeatId := 0
for _, s := range seats {
if s.id > biggestSeatId {
biggestSeatId = s.id
}
}
fmt.Println("part 1: biggest seat id:", biggestSeatId)
var lastSeat seat
for i := 0; i < 128; i++ {
for j := 0; j < 8; j++ {
for _, s := range seats {
if s.row == i && s.column == j {
if lastSeat.id != 0 && lastSeat.id != s.id-1 {
fmt.Println("part 2: my seat must be", lastSeat.id+1)
}
lastSeat = s
}
}
}
}
}
/**
* 128x8
* returns seat
*/
func findSeat(identifier string) seat {
rows := 128
rowsUp := 127
rowsLo := 0
columns := 8
columnsUp := 7
columnsLo := 0
index := 0
for index < len(identifier) {
switch string(identifier[index]) {
case "F":
rows /= 2
rowsUp -= rows
case "B":
rows /= 2
rowsLo += rows
case "L":
columns /= 2
columnsUp -= columns
case "R":
columns /= 2
columnsLo += columns
}
index++
}
return seat{rowsLo, columnsLo, rowsLo*8 + columnsLo}
}