Compare commits

...

5 Commits

Author SHA1 Message Date
0b67ae4aad Day 6 part 2 2024-03-01 15:49:15 -05:00
3624aa24bb Day 6 part 1 2024-03-01 15:49:12 -05:00
6b5c65a811 Improve performance of day 5 2024-03-01 09:32:39 -05:00
fb5c2ab07b Day 5 part 2 2024-03-01 09:24:23 -05:00
531b8a52ef Day 5 part 1 2024-03-01 09:05:46 -05:00
3 changed files with 128 additions and 0 deletions

View File

@ -18,6 +18,12 @@ func main() {
runAndBenchmark("04", "1", false, solutions.Day04Part01) runAndBenchmark("04", "1", false, solutions.Day04Part01)
runAndBenchmark("04", "2", false, solutions.Day04Part02) runAndBenchmark("04", "2", false, solutions.Day04Part02)
runAndBenchmark("05", "1", false, solutions.Day05Part01)
runAndBenchmark("05", "2", false, solutions.Day05Part02)
runAndBenchmark("06", "1", false, solutions.Day06Part01)
runAndBenchmark("06", "2", false, solutions.Day06Part02)
} }
type execute func(bool) int type execute func(bool) int

63
solutions/day05.go Normal file
View File

@ -0,0 +1,63 @@
package solutions
import (
"slices"
"strconv"
"strings"
)
func seatStrToSeatId(seat string) int {
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)
return 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 := seatStrToSeatId(seat)
if seatId > highestSeatId {
highestSeatId = seatId
}
}
return highestSeatId
}
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
}

59
solutions/day06.go Normal file
View File

@ -0,0 +1,59 @@
package solutions
import (
"strings"
)
func Day06Part01(isTest bool) int {
input := ReadInput("06", isTest)
groups := strings.Split(input, "\n\n")
sum := 0
for _, group := range groups {
frequency := make(map[rune]int)
for _, letter := range group {
// remove non a-z character
if letter < 97 || letter > 122 {
continue
}
previousValue, _ := frequency[letter]
frequency[letter] = previousValue + 1
}
sum += len(frequency)
}
return sum
}
func Day06Part02(isTest bool) int {
input := ReadInput("06", isTest)
groups := strings.Split(input, "\n\n")
sum := 0
for _, group := range groups {
persons := strings.Split(group, "\n")
personAmount := len(persons)
frequency := make(map[rune]int)
for _, person := range persons {
for _, letter := range person {
previousValue, _ := frequency[letter]
frequency[letter] = previousValue + 1
}
}
for _, value := range frequency {
if value == personAmount {
sum += 1
}
}
}
return sum
}