Created
December 6, 2022 16:30
-
-
Save bishabosha/24ce043dddb2fed2d26658b94962144c to your computer and use it in GitHub Desktop.
AOC 2022 day 6 optimal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def part1(input: String): Int = | |
findIndexOptimal(input, n = 4) | |
def part2(input: String): Int = | |
findIndexOptimal(input, n = 14) | |
class MultiSet: | |
private val counts = new Array[Int](128) | |
private var total = 0 | |
def size = total | |
def add(c: Char) = | |
val count = counts(c.toInt) | |
if count == 0 then | |
total += 1 | |
counts(c.toInt) += 1 | |
def remove(c: Char) = | |
val count = counts(c.toInt) | |
if count > 0 then | |
if count == 1 then | |
total -= 1 | |
counts(c.toInt) -= 1 | |
end MultiSet | |
def findIndexOptimal(input: String, n: Int): Int = | |
val counts = MultiSet() | |
def loop(previous: Char, i: Int, j: Int): Int = | |
if i < input.length then | |
val next = input(i) | |
val last = input(j) | |
if previous != last then | |
counts.remove(previous) | |
counts.add(last) | |
if counts.size == n then | |
i + n // found the index | |
else | |
loop(next, i + 1, j + 1) | |
else | |
-1 | |
input.view.take(n).foreach(counts.add) | |
loop(input(0), i = 1, j = n) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment