From c043bcc5d7925377df624215e5496c58ed0a96bd Mon Sep 17 00:00:00 2001 From: Luke Hubmayer-Werner Date: Tue, 6 Dec 2022 18:01:12 +1030 Subject: [PATCH] 2022 Day 6 Scala perf testing (loses to python!) --- 2022/day6.scala | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 2022/day6.scala diff --git a/2022/day6.scala b/2022/day6.scala new file mode 100644 index 0000000..4227eca --- /dev/null +++ b/2022/day6.scala @@ -0,0 +1,36 @@ +import java.nio.file.Files +import java.nio.file.Paths + +def first_unique_run_for(line: String, num: Int, skip: Int = 0): Int = + for i <- num.max(skip) until line.length do + if line.substring(i-num, i).toCharArray.toSet.size == num then + return i + return -1 + +def first_unique_run_while(line: String, num: Int, skip: Int = 0): Int = + var i = num.max(skip) + while i < line.length do + if line.substring(i-num, i).toCharArray.toSet.size == num then + return i + i += 1 + return -1 + +@main def main() = + val line = Files.readString(Paths.get("input/6")).strip + do_once(line) + // perf_test(line, 50000) + +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) + +def perf_test(line: String, n: int) = + var four = -1 + var fourteen = -1 + for i <- 1 to n do + four = first_unique_run_while(line, 4) + fourteen = first_unique_run_while(line, 14, four) + println(four) + println(fourteen)