AoC2020/day5/day5.go
Karl Spickermann 21e6b80a5e Day5
2020-12-05 15:06:57 +01:00

78 lines
1.3 KiB
Go

package main
import (
"AoC2020/helper"
"fmt"
"os"
"sort"
)
func main() {
args := os.Args[1:]
input, err := helper.GetInput(args[0])
if err != nil {
fmt.Println(err)
}
ids := getIds(input)
fmt.Println(day5(ids))
fmt.Println(day5Part2(ids))
}
func day5(ids []int) int {
var max int
for _, id := range ids {
if id > max {
max = id
}
}
return max
}
func day5Part2(ids []int) int {
sort.Ints(ids)
for i := 1; i < len(ids) -1; i++ {
id := ids[i]
if id - ids[i-1] > 1 {
return id - 1
}
}
return 0
}
func getIds(boardingPasses []string) []int {
var ids []int
for _, boardingPass := range boardingPasses {
row, column := getSeat(boardingPass)
ids = append(ids, getId(row, column))
}
return ids
}
func getSeat(boardingPass string) (int, int) {
rowP1 := 0
rowP2 := 127
for _, char := range boardingPass[:7] {
if char == 'F' {
rowP2 = rowP2 - (rowP2-rowP1)/2 - 1
}
if char == 'B' {
rowP1 = rowP1 + (rowP2-rowP1)/2 + 1
}
}
columnP1 := 0
columnP2 := 7
for _, char := range boardingPass[7:] {
if char == 'L' {
columnP2 = columnP2 - (columnP2-columnP1)/2 - 1
}
if char == 'R' {
columnP1 = columnP1 + (columnP2-columnP1)/2 + 1
}
}
return rowP1, columnP1
}
func getId(row int, column int) int {
return row*8 + column
}