adventofcode/day5/five.go

75 lines
1.4 KiB
Go
Raw Normal View History

2020-12-05 13:58:48 +00:00
package main
import (
2020-12-05 14:00:00 +00:00
"fmt"
2020-12-05 13:58:48 +00:00
"io/ioutil"
2020-12-05 14:13:42 +00:00
"sort"
2020-12-05 13:58:48 +00:00
"strings"
)
// diff finds the difference between two ints
2020-12-05 14:00:00 +00:00
func diff(a, b int) int {
2020-12-05 13:58:48 +00:00
if a > b {
2020-12-05 14:00:00 +00:00
return a - b
2020-12-05 13:58:48 +00:00
}
2020-12-05 14:00:00 +00:00
return b - a
2020-12-05 13:58:48 +00:00
}
func main() {
d, _ := ioutil.ReadFile("in5.txt")
2020-12-05 14:00:00 +00:00
s := strings.Split(string(d), "\n")
2020-12-05 13:58:48 +00:00
max := 0
2020-12-05 14:13:42 +00:00
var ids []int
2020-12-05 13:58:48 +00:00
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 {
2020-12-05 14:00:00 +00:00
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
}
2020-12-05 13:58:48 +00:00
}
}
2020-12-05 14:00:00 +00:00
id := row*8 + col
2020-12-05 14:13:42 +00:00
ids = append(ids, id)
2020-12-05 13:58:48 +00:00
if id > max {
max = id
}
}
2020-12-05 14:13:42 +00:00
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
}
2020-12-05 14:00:00 +00:00
}