diff --git a/main.go b/main.go index 2d87a2f..af35bcd 100644 --- a/main.go +++ b/main.go @@ -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 diff --git a/solutions/day05.go b/solutions/day05.go index a15f1c9..48b0df9 100644 --- a/solutions/day05.go +++ b/solutions/day05.go @@ -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 }