Nim losing to Scala (again)
This commit is contained in:
parent
8d7aedcc39
commit
d4a3d8f89b
|
@ -2,7 +2,7 @@ import java.nio.file.Files
|
||||||
import java.nio.file.Paths
|
import java.nio.file.Paths
|
||||||
import scala.util.control.NonLocalReturns.*
|
import scala.util.control.NonLocalReturns.*
|
||||||
|
|
||||||
// scala day6.jar 52.22s user 0.54s system 100% cpu 52.389 total
|
// 1M iterations: scala day6.jar 52.22s user 0.54s system 100% cpu 52.389 total
|
||||||
extension (s: String) def noDuplicates: Boolean =
|
extension (s: String) def noDuplicates: Boolean =
|
||||||
var i = 0
|
var i = 0
|
||||||
while i < (s.length-1) do
|
while i < (s.length-1) do
|
||||||
|
@ -21,12 +21,24 @@ def first_unique_run_for(line: String, num: Int, skip: Int = 0): Int = returning
|
||||||
throwReturn(-1)
|
throwReturn(-1)
|
||||||
}
|
}
|
||||||
|
|
||||||
def first_unique_run_while(line: String, num: Int, skip: Int = 0): Int =
|
// 1M iterations: scala day6.jar 20.44s user 0.07s system 100% cpu 20.371 total
|
||||||
var i = num.max(skip)
|
def no_duplicates_copyless(s: String, right: Int, length: Int): Boolean =
|
||||||
while i < line.length do
|
var i = right - length + 1
|
||||||
if line.substring(i-num, i).toCharArray.distinct.size == num then
|
while i < right do
|
||||||
return i
|
val c = s(i)
|
||||||
|
var j = i + 1
|
||||||
|
while j <= right do
|
||||||
|
if c == s(j) then return false
|
||||||
|
j += 1
|
||||||
i += 1
|
i += 1
|
||||||
|
return true
|
||||||
|
|
||||||
|
def first_unique_run_while(line: String, num: Int, skip: Int = 0): Int =
|
||||||
|
var right = num.max(skip)
|
||||||
|
while right < line.length do
|
||||||
|
if no_duplicates_copyless(line, right, num) then
|
||||||
|
return right
|
||||||
|
right += 1
|
||||||
return -1
|
return -1
|
||||||
|
|
||||||
@main def main() =
|
@main def main() =
|
||||||
|
@ -44,7 +56,7 @@ def perf_test(line: String, n: Int) =
|
||||||
var four = -1
|
var four = -1
|
||||||
var fourteen = -1
|
var fourteen = -1
|
||||||
for i <- 1 to n do
|
for i <- 1 to n do
|
||||||
four = first_unique_run_for(line, 4)
|
four = first_unique_run_while(line, 4)
|
||||||
fourteen = first_unique_run_for(line, 14, four)
|
fourteen = first_unique_run_while(line, 14, four)
|
||||||
println(four)
|
println(four)
|
||||||
println(fourteen)
|
println(fourteen)
|
||||||
|
|
Loading…
Reference in New Issue