75 lines
1.4 KiB
Go
75 lines
1.4 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"io/ioutil"
|
|
"sort"
|
|
"strings"
|
|
)
|
|
|
|
// diff finds the difference between two ints
|
|
func diff(a, b int) int {
|
|
if a > b {
|
|
return a - b
|
|
}
|
|
return b - a
|
|
}
|
|
|
|
func main() {
|
|
d, _ := ioutil.ReadFile("in5.txt")
|
|
s := strings.Split(string(d), "\n")
|
|
max := 0
|
|
var ids []int
|
|
for _, code := range s {
|
|
rowUpper := 127
|
|
rowLower := 0
|
|
colUpper := 7
|
|
colLower := 0
|
|
row := 0 // will store the row when it's completed
|
|
col := 0 // will store the col when it's completed
|
|
for i, sym := range code {
|
|
switch sym {
|
|
case 'F': // The lower half
|
|
if i == 6 { // the last command
|
|
row = rowLower
|
|
} else {
|
|
rowUpper = rowUpper - (diff(rowLower, rowUpper) / 2) - 1
|
|
}
|
|
case 'B': // The upper half
|
|
if i == 6 {
|
|
row = rowUpper
|
|
} else {
|
|
rowLower = rowLower + (diff(rowLower, rowUpper) / 2) + 1
|
|
}
|
|
case 'L': // Lower Half
|
|
if i == 9 {
|
|
col = colLower
|
|
} else {
|
|
colUpper = colUpper - (diff(colLower, colUpper) / 2) - 1
|
|
}
|
|
case 'R': // Upper Half
|
|
if i == 9 {
|
|
col = colUpper
|
|
} else {
|
|
colLower = colLower + (diff(colLower, colUpper) / 2) + 1
|
|
}
|
|
|
|
}
|
|
}
|
|
id := row*8 + col
|
|
ids = append(ids, id)
|
|
if id > max {
|
|
max = id
|
|
}
|
|
}
|
|
sort.Ints(ids)
|
|
fmt.Println("Highest ID: ", max)
|
|
prev := 0
|
|
for _, d := range ids {
|
|
if prev != 0 && prev+1 != d {
|
|
fmt.Println("Missing Seat: ", d-1)
|
|
}
|
|
prev = d
|
|
}
|
|
}
|