From 531b8a52ef308e29ad37d0c5422ceca0dd6cafc4 Mon Sep 17 00:00:00 2001 From: Araozu Date: Fri, 1 Mar 2024 09:05:46 -0500 Subject: [PATCH] Day 5 part 1 --- main.go | 3 +++ solutions/day05.go | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 solutions/day05.go diff --git a/main.go b/main.go index d61d57e..2d87a2f 100644 --- a/main.go +++ b/main.go @@ -18,6 +18,9 @@ func main() { runAndBenchmark("04", "1", false, solutions.Day04Part01) runAndBenchmark("04", "2", false, solutions.Day04Part02) + + runAndBenchmark("05", "1", false, solutions.Day05Part01) + runAndBenchmark("05", "2", true, solutions.Day05Part02) } type execute func(bool) int diff --git a/solutions/day05.go b/solutions/day05.go new file mode 100644 index 0000000..a15f1c9 --- /dev/null +++ b/solutions/day05.go @@ -0,0 +1,40 @@ +package solutions + +import ( + "regexp" + "strconv" + "strings" +) + +func seatToValue(seat string) (int, int, int) { + lowRegex := regexp.MustCompile("[FL]") + highRegex := regexp.MustCompile("[BR]") + + seatStr := lowRegex.ReplaceAllString(seat, "0") + seatStr = highRegex.ReplaceAllString(seatStr, "1") + + row, _ := strconv.ParseInt(seatStr[:7], 2, 64) + column, _ := strconv.ParseInt(seatStr[7:], 2, 64) + + return int(row), int(column), int(row*8 + column) +} + +func Day05Part01(isTest bool) int { + input := ReadInput("05", isTest) + seats := strings.Split(input, "\n") + + highestSeatId := 0 + + for _, seat := range seats { + _, _, seatId := seatToValue(seat) + if seatId > highestSeatId { + highestSeatId = seatId + } + } + + return highestSeatId +} + +func Day05Part02(isTest bool) int { + return -1 +}