-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
67 lines (61 loc) · 1.51 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
package main
import (
"bufio"
"fmt"
"os"
)
func main() {
seatingMap, _ := readLines("input.txt")
numrows := 128
numcols := 8
highestSeat := 0
for _, boardingpass := range seatingMap {
cols, rows := initializeRowcols(numcols), initializeRowcols(numrows)
for i := 0; i != len(boardingpass); i++ {
switch {
case string(boardingpass[i]) == "F" || string(boardingpass[i]) == "B":
rows = cutmap(rows, string(boardingpass[i]))
case string(boardingpass[i]) == "L" || string(boardingpass[i]) == "R":
cols = cutmap(cols, string(boardingpass[i]))
}
}
seatId := rows[0]*8 + cols[0]
fmt.Printf("Row: %d, Column: %d, Seatid: %d\n", rows[0], cols[0], seatId)
if seatId > highestSeat {
highestSeat = seatId
}
}
fmt.Println("Highest seat:", highestSeat)
}
func cutmap(rowcols []int, letter string) []int {
half := len(rowcols) / 2
switch {
case letter == "F" || letter == "L":
return rowcols[:half]
case letter == "B" || letter == "R":
return rowcols[half:]
}
return rowcols
}
func initializeRowcols(num int) []int {
var initialized []int
for i := 0; i < num; i++ {
initialized = append(initialized, i)
}
return initialized
}
// readLines reads a whole file into memory
// and returns a slice of its lines.
func readLines(path string) ([]string, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
var lines []string
scanner := bufio.NewScanner(file)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
return lines, scanner.Err()
}