Day 5 part 2

This commit is contained in:
Araozu 2024-03-01 09:24:23 -05:00
parent 531b8a52ef
commit fb5c2ab07b
2 changed files with 27 additions and 4 deletions

View File

@ -20,7 +20,7 @@ func main() {
runAndBenchmark("04", "2", false, solutions.Day04Part02)
runAndBenchmark("05", "1", false, solutions.Day05Part01)
runAndBenchmark("05", "2", true, solutions.Day05Part02)
runAndBenchmark("05", "2", false, solutions.Day05Part02)
}
type execute func(bool) int

View File

@ -2,11 +2,12 @@ package solutions
import (
"regexp"
"slices"
"strconv"
"strings"
)
func seatToValue(seat string) (int, int, int) {
func seatStrToSeatId(seat string) int {
lowRegex := regexp.MustCompile("[FL]")
highRegex := regexp.MustCompile("[BR]")
@ -16,7 +17,7 @@ func seatToValue(seat string) (int, int, int) {
row, _ := strconv.ParseInt(seatStr[:7], 2, 64)
column, _ := strconv.ParseInt(seatStr[7:], 2, 64)
return int(row), int(column), int(row*8 + column)
return int(row*8 + column)
}
func Day05Part01(isTest bool) int {
@ -26,7 +27,7 @@ func Day05Part01(isTest bool) int {
highestSeatId := 0
for _, seat := range seats {
_, _, seatId := seatToValue(seat)
seatId := seatStrToSeatId(seat)
if seatId > highestSeatId {
highestSeatId = seatId
}
@ -36,5 +37,27 @@ func Day05Part01(isTest bool) int {
}
func Day05Part02(isTest bool) int {
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
}
}
return -1
}