advent-20/solutions/day05.go

64 lines
1.2 KiB
Go
Raw Normal View History

2024-03-01 14:05:46 +00:00
package solutions
import (
2024-03-01 14:24:23 +00:00
"slices"
2024-03-01 14:05:46 +00:00
"strconv"
"strings"
)
2024-03-01 14:24:23 +00:00
func seatStrToSeatId(seat string) int {
2024-03-01 14:32:39 +00:00
rowStr := seat[:7]
rowStr = strings.ReplaceAll(rowStr, "F", "0")
rowStr = strings.ReplaceAll(rowStr, "B", "1")
columnStr := seat[7:]
columnStr = strings.ReplaceAll(columnStr, "L", "0")
columnStr = strings.ReplaceAll(columnStr, "R", "1")
row, _ := strconv.ParseInt(rowStr, 2, 64)
column, _ := strconv.ParseInt(columnStr, 2, 64)
2024-03-01 14:05:46 +00:00
2024-03-01 14:24:23 +00:00
return int(row*8 + column)
2024-03-01 14:05:46 +00:00
}
func Day05Part01(isTest bool) int {
input := ReadInput("05", isTest)
seats := strings.Split(input, "\n")
highestSeatId := 0
for _, seat := range seats {
2024-03-01 14:24:23 +00:00
seatId := seatStrToSeatId(seat)
2024-03-01 14:05:46 +00:00
if seatId > highestSeatId {
highestSeatId = seatId
}
}
return highestSeatId
}
func Day05Part02(isTest bool) int {
2024-03-01 14:24:23 +00:00
input := ReadInput("05", isTest)
seats := strings.Split(input, "\n")
seatsAmount := len(seats)
seatIds := make([]int, seatsAmount)
for i, seat := range seats {
seatId := seatStrToSeatId(seat)
seatIds[i] = seatId
}
slices.Sort(seatIds)
for i := 0; i < seatsAmount-1; i++ {
el1 := seatIds[i]
el2 := seatIds[i+1]
if el2-el1 == 2 {
return el1 + 1
}
}
2024-03-01 14:05:46 +00:00
return -1
}