2022-12-06 18:01:12 +10:30
|
|
|
import java.nio.file.Files
|
|
|
|
import java.nio.file.Paths
|
2022-12-06 20:09:07 +10:30
|
|
|
import scala.util.control.NonLocalReturns.*
|
2022-12-06 18:01:12 +10:30
|
|
|
|
2022-12-06 23:02:25 +10:30
|
|
|
// scala day6.jar 52.22s user 0.54s system 100% cpu 52.389 total
|
|
|
|
extension (s: String) def noDuplicates: Boolean =
|
|
|
|
var i = 0
|
|
|
|
while i < (s.length-1) do
|
|
|
|
val c = s(i)
|
|
|
|
var j = i + 1
|
|
|
|
while j < s.length do
|
|
|
|
if s(j) == c then return false
|
|
|
|
j += 1
|
|
|
|
i += 1
|
|
|
|
return true
|
|
|
|
|
2022-12-06 20:09:07 +10:30
|
|
|
def first_unique_run_for(line: String, num: Int, skip: Int = 0): Int = returning {
|
2022-12-06 18:01:12 +10:30
|
|
|
for i <- num.max(skip) until line.length do
|
2022-12-06 23:02:25 +10:30
|
|
|
if line.substring(i-num, i).noDuplicates then
|
2022-12-06 20:09:07 +10:30
|
|
|
throwReturn(i)
|
|
|
|
throwReturn(-1)
|
|
|
|
}
|
2022-12-06 18:01:12 +10:30
|
|
|
|
|
|
|
def first_unique_run_while(line: String, num: Int, skip: Int = 0): Int =
|
|
|
|
var i = num.max(skip)
|
|
|
|
while i < line.length do
|
2022-12-06 20:09:07 +10:30
|
|
|
if line.substring(i-num, i).toCharArray.distinct.size == num then
|
2022-12-06 18:01:12 +10:30
|
|
|
return i
|
|
|
|
i += 1
|
|
|
|
return -1
|
|
|
|
|
|
|
|
@main def main() =
|
|
|
|
val line = Files.readString(Paths.get("input/6")).strip
|
2022-12-06 20:09:07 +10:30
|
|
|
// do_once(line)
|
2022-12-06 23:02:25 +10:30
|
|
|
perf_test(line, 1000000)
|
2022-12-06 18:01:12 +10:30
|
|
|
|
|
|
|
def do_once(line: String) =
|
|
|
|
val four = first_unique_run_for(line, 4)
|
|
|
|
val fourteen = first_unique_run_for(line, 14, four)
|
|
|
|
println(four)
|
|
|
|
println(fourteen)
|
|
|
|
|
2022-12-06 20:09:07 +10:30
|
|
|
def perf_test(line: String, n: Int) =
|
2022-12-06 18:01:12 +10:30
|
|
|
var four = -1
|
|
|
|
var fourteen = -1
|
|
|
|
for i <- 1 to n do
|
2022-12-06 20:09:07 +10:30
|
|
|
four = first_unique_run_for(line, 4)
|
|
|
|
fourteen = first_unique_run_for(line, 14, four)
|
2022-12-06 18:01:12 +10:30
|
|
|
println(four)
|
|
|
|
println(fourteen)
|