Skip to content

Instantly share code, notes, and snippets.

@kolemannix
Created August 9, 2023 12:12
Show Gist options
  • Save kolemannix/10ba68b519ff787800c466fcd3cb8528 to your computer and use it in GitHub Desktop.
Save kolemannix/10ba68b519ff787800c466fcd3cb8528 to your computer and use it in GitHub Desktop.
aoc_day6_fast
def day6fast(chars: Iterator[Char], targetNum: Int): Option[Int] = {
val buffer = scala.collection.mutable.ListBuffer.empty[Char]
try {
var i = 0
while (true) {
val c = chars.next()
if (buffer.length == targetNum) {
buffer.dropInPlace(1)
}
buffer.append(c)
if (buffer.distinct.size == targetNum) return Some(i + 1)
i += 1
}
None
} catch {
case _ => None
}
}
day6fast("mjqjpqmgbljsphdztnvjfqwrcgsmlb".iterator, 14)
day6fast("bvwbjplbgvbhsrlpgdmjqwftvncz".iterator, 14)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment