From f9ef1da6d4d584d2fd92ad4202b80106105b4cd9 Mon Sep 17 00:00:00 2001 From: Alexander Avery Date: Tue, 3 Dec 2024 21:26:35 -0500 Subject: [PATCH] custom function to reverse slices --- 2024/day02.go | 5 ++--- 2024/iterator.go | 10 ++++++++++ 2 files changed, 12 insertions(+), 3 deletions(-) create mode 100644 2024/iterator.go diff --git a/2024/day02.go b/2024/day02.go index be2c977..9443852 100644 --- a/2024/day02.go +++ b/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++ } diff --git a/2024/iterator.go b/2024/iterator.go new file mode 100644 index 0000000..a1b871e --- /dev/null +++ b/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-- + } +}