Browse Source

custom function to reverse slices

master
Alexander Avery 3 weeks ago
parent
commit
f9ef1da6d4
  1. 5
      2024/day02.go
  2. 10
      2024/iterator.go

5
2024/day02.go

@ -4,7 +4,6 @@ import (
"bufio"
"fmt"
"io"
"slices"
"strings"
)
@ -29,7 +28,7 @@ func (d Day2) Part1(r io.Reader) string {
if layerOk(level) {
sum++
} else {
slices.Reverse(level)
reverse(level)
if layerOk(level) {
sum++
}
@ -58,7 +57,7 @@ func (d Day2) Part2(r io.Reader) string {
if layerOkWithDampen(level) {
sum++
} else {
slices.Reverse(level)
reverse(level)
if layerOkWithDampen(level) {
sum++
}

10
2024/iterator.go

@ -0,0 +1,10 @@
package adventoc2024
func reverse(s []int) {
start, end := 0, len(s)-1
for start < end {
s[start], s[end] = s[end], s[start]
start++
end--
}
}
Loading…
Cancel
Save