Compare commits
5 Commits
d78e3eed86
...
0b67ae4aad
Author | SHA1 | Date | |
---|---|---|---|
0b67ae4aad | |||
3624aa24bb | |||
6b5c65a811 | |||
fb5c2ab07b | |||
531b8a52ef |
6
main.go
6
main.go
@ -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
63
solutions/day05.go
Normal 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
59
solutions/day06.go
Normal 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
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user