adventofcode/2020/day05.go
2020-12-09 09:18:03 +00:00

58 lines
1.2 KiB
Go

package main
import (
"fmt"
"log"
"os"
"strconv"
"strings"
"git.annabunch.es/annabunches/adventofcode/2020/lib/fileutils"
)
func calculateSeatNumber(input string) int {
if input == "" {
return 0
}
row, err := strconv.ParseInt(strings.ReplaceAll(strings.ReplaceAll(input[0:7], "F", "0"), "B", "1"), 2, 32)
if err != nil {
log.Panicf(err.Error())
}
col, err := strconv.ParseInt(strings.ReplaceAll(strings.ReplaceAll(input[7:10], "L", "0"), "R", "1"), 2, 32)
if err != nil {
log.Panicf(err.Error())
}
return int((row * 8) + col)
}
func main() {
step := os.Args[1]
values := fileutils.InputParserStrings(os.Args[2])
switch step {
case "1":
max := 0
for _, line := range values {
seatNumber := calculateSeatNumber(line)
if seatNumber > max {
max = seatNumber
}
}
fmt.Println("Highest Seat Number:", max)
case "2":
// build a map of all seat numbers then just print the ones between 0 and 1024 that don't exist
seatMap := make(map[int]bool)
for _, line := range values {
seatNumber := calculateSeatNumber(line)
seatMap[seatNumber] = true
}
for i := 0; i < 1024; i++ {
if _, ok := seatMap[i]; !ok {
fmt.Println("Didn't find seat ", i)
}
}
}
}