AoC2020/day5/day5.go

78 lines
1.3 KiB
Go
Raw Normal View History

2020-12-05 14:43:34 +01:00
package main
import (
2020-12-05 15:06:57 +01:00
"AoC2020/helper"
2020-12-05 14:43:34 +01:00
"fmt"
"os"
2020-12-05 15:06:57 +01:00
"sort"
2020-12-05 14:43:34 +01:00
)
func main() {
args := os.Args[1:]
2020-12-05 15:06:57 +01:00
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
2020-12-05 14:43:34 +01:00
}
2020-12-05 15:06:57 +01:00
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) {
2020-12-05 14:43:34 +01:00
rowP1 := 0
rowP2 := 127
2020-12-05 15:06:57 +01:00
for _, char := range boardingPass[:7] {
2020-12-05 14:43:34 +01:00
if char == 'F' {
rowP2 = rowP2 - (rowP2-rowP1)/2 - 1
}
if char == 'B' {
rowP1 = rowP1 + (rowP2-rowP1)/2 + 1
}
}
columnP1 := 0
columnP2 := 7
2020-12-05 15:06:57 +01:00
for _, char := range boardingPass[7:] {
2020-12-05 14:43:34 +01:00
if char == 'L' {
columnP2 = columnP2 - (columnP2-columnP1)/2 - 1
}
if char == 'R' {
columnP1 = columnP1 + (columnP2-columnP1)/2 + 1
}
}
return rowP1, columnP1
2020-12-05 15:06:57 +01:00
}
func getId(row int, column int) int {
return row*8 + column
}