2022 Day 6 Scala vs Nim benching (spoiler: Nim got blown off the board)
This commit is contained in:
parent
90c359f7cb
commit
e37e0b89f5
|
@ -2,34 +2,38 @@
|
||||||
import strutils
|
import strutils
|
||||||
let line = strip read_file "input/6"
|
let line = strip read_file "input/6"
|
||||||
|
|
||||||
# proc find_first(n: int): int =
|
proc string_no_duplicate_chars(s: string): bool =
|
||||||
# for i in n..line.len:
|
# 1M iterations: nim r -d:danger day6.nim 90.29s user 0.27s system 99% cpu 1:30.99 total
|
||||||
# var s = {line[i-n]}
|
for i in 0..<s.len:
|
||||||
# for c in line[i-n+1..<i]:
|
let c = s[i]
|
||||||
# s.incl(c)
|
for j in (i+1)..<s.len:
|
||||||
# if s.len == n:
|
if c == s[j]:
|
||||||
# return i
|
return false
|
||||||
# return -1
|
return true
|
||||||
|
|
||||||
let a = ord('a')
|
proc string_no_duplicate_chars_1iter(s: string): bool =
|
||||||
proc to_bit(c: char): uint32 =
|
# 1M iterations: nim r -d:danger day6.nim 206.13s user 0.69s system 99% cpu 3:27.83 total
|
||||||
return uint32(1 shl (ord(c) - a))
|
for i in 0..<s.len:
|
||||||
|
let c1 = s[i]
|
||||||
|
for c2 in s[i+1..^1]:
|
||||||
|
if c1==c2:
|
||||||
|
return false
|
||||||
|
return true
|
||||||
|
|
||||||
|
proc string_no_duplicate_chars_2iter(s: string): bool =
|
||||||
|
# 1M iterations: nim r -d:danger day6.nim 259.15s user 0.76s system 99% cpu 4:20.86 total
|
||||||
|
for i, c1 in s[0..^2]:
|
||||||
|
for c2 in s[i+1..^1]:
|
||||||
|
if c1==c2:
|
||||||
|
return false
|
||||||
|
return true
|
||||||
|
|
||||||
proc find_first(n: int, skip: int = 0): int =
|
proc find_first(n: int, skip: int = 0): int =
|
||||||
for i in max(n, skip)..line.len:
|
for i in max(n, skip)..<line.len:
|
||||||
block inner:
|
if string_no_duplicate_chars(line[i-n..<i]):
|
||||||
var s = to_bit line[i-n]
|
|
||||||
for c in line[i-n+1..<i]:
|
|
||||||
let b = to_bit c
|
|
||||||
if (s and b) != 0:
|
|
||||||
break inner
|
|
||||||
s = s or b
|
|
||||||
return i
|
return i
|
||||||
return -1
|
return -1
|
||||||
|
|
||||||
# echo find_first 4
|
|
||||||
# echo find_first 14
|
|
||||||
|
|
||||||
var four = 0
|
var four = 0
|
||||||
var fourteen = 0
|
var fourteen = 0
|
||||||
for i in 1..1000000:
|
for i in 1..1000000:
|
||||||
|
|
|
@ -2,9 +2,21 @@ 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
|
||||||
|
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
|
||||||
|
|
||||||
def first_unique_run_for(line: String, num: Int, skip: Int = 0): Int = returning {
|
def first_unique_run_for(line: String, num: Int, skip: Int = 0): Int = returning {
|
||||||
for i <- num.max(skip) until line.length do
|
for i <- num.max(skip) until line.length do
|
||||||
if line.substring(i-num, i).toCharArray.distinct.size == num then
|
if line.substring(i-num, i).noDuplicates then
|
||||||
throwReturn(i)
|
throwReturn(i)
|
||||||
throwReturn(-1)
|
throwReturn(-1)
|
||||||
}
|
}
|
||||||
|
@ -20,7 +32,7 @@ def first_unique_run_while(line: String, num: Int, skip: Int = 0): Int =
|
||||||
@main def main() =
|
@main def main() =
|
||||||
val line = Files.readString(Paths.get("input/6")).strip
|
val line = Files.readString(Paths.get("input/6")).strip
|
||||||
// do_once(line)
|
// do_once(line)
|
||||||
perf_test(line, 100000)
|
perf_test(line, 1000000)
|
||||||
|
|
||||||
def do_once(line: String) =
|
def do_once(line: String) =
|
||||||
val four = first_unique_run_for(line, 4)
|
val four = first_unique_run_for(line, 4)
|
||||||
|
|
Loading…
Reference in New Issue