advent2022

Advent of Code 2022 Solutions
git clone git://bsandro.tech/advent2022
Log | Files | Refs | README | LICENSE

commit 772f53dd72502bb5a0aea7a3c9145dc7d27b6c0d
parent 4145ed6a4233b0293557e36d69eb863ba52bd100
Author: bsandro <email@bsandro.tech>
Date:   Tue,  6 Dec 2022 07:57:26 +0000

Day 06 proper solution and not the O(n^3) naive garbage I've got before

Diffstat:
Mday06/main.go | 27++++++++++++---------------
1 file changed, 12 insertions(+), 15 deletions(-)

diff --git a/day06/main.go b/day06/main.go @@ -33,27 +33,24 @@ func day06(input_file string) { func checkStream(input []byte, size int) int { var buf []byte for i, b := range input { - if len(buf) == size { - buf = buf[1:] + if len(buf) > 0 { + has, pos := sliceHas(buf, b) + if has { + buf = buf[pos+1:] + } else if len(buf) == size-1 { + return i + 1 + } } buf = append(buf, b) - if checkMarker(buf, size) { - return i+1 - } } return -1 } -func checkMarker(buf []byte, size int) bool { - if len(buf) != size { - return false - } - for i:=0; i<size; i++ { - for j:=0; j<size; j++ { - if i!=j && buf[i] == buf[j] { - return false - } +func sliceHas(buf []byte, n byte) (bool, int) { + for i, e := range buf { + if e == n { + return true, i } } - return true + return false, -1 }