-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
75 lines (62 loc) · 1.42 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
70
71
72
73
74
75
package main
import (
"fmt"
"github.com/davidporos92/aoc-2020/utils"
"math"
"sort"
)
const (
lower = iota
upper
front = "F"
back = "B"
left = "L"
right = "R"
maxRows int = 127
maxColumns int = 7
)
func main() {
maxSeatID := 0.0
seatIDs := make([]float64, 0)
utils.NewReader("./input-1.dat").MustReadFile(func(line string) {
seatID := float64(calculateSeatID(calculateRowAndColumn(line)))
seatIDs = append(seatIDs, seatID)
maxSeatID = math.Max(seatID, maxSeatID)
})
fmt.Printf("Max SeatID: %0.f\n", maxSeatID)
sort.Float64s(seatIDs)
for i := 1; i < len(seatIDs)-1; i++ {
next := seatIDs[i+1]
actual := seatIDs[i]
if actual+1 != next && actual+2 == next {
fmt.Printf("My seat: %0.f\n", actual+1)
return
}
}
}
func calculateRowAndColumn(seat string) (int, int) {
rowRange := map[uint]int{
lower: 0,
upper: maxRows,
}
columnRange := map[uint]int{
lower: 0,
upper: maxColumns,
}
for _, char := range []rune(seat) {
switch string(char) {
case back:
rowRange[lower] += (rowRange[upper]-rowRange[lower])/2 + 1
case front:
rowRange[upper] -= (rowRange[upper]-rowRange[lower])/2 + 1
case right:
columnRange[lower] += (columnRange[upper]-columnRange[lower])/2 + 1
case left:
columnRange[upper] -= (columnRange[upper]-columnRange[lower])/2 + 1
}
}
return rowRange[upper], columnRange[upper]
}
func calculateSeatID(row, column int) int {
return (row * 8) + column
}