Benchmark suite (#16)

This commit is contained in:
Patrick Stevens
2022-12-11 12:16:07 +00:00
committed by GitHub
parent 4205ec4661
commit d323984a12
11 changed files with 324 additions and 120 deletions

View File

@@ -17,13 +17,14 @@ module Day11 =
Number : int<monkey>
StartingItems : int64 ResizeArray
OperationIsPlus : bool
Argument : int64 option
/// Negative is None
Argument : int64
TestDivisibleBy : int64
TrueCase : int<monkey>
FalseCase : int<monkey>
}
let parse (lines : StringSplitEnumerator) : Monkey IReadOnlyList =
let parse (lines : StringSplitEnumerator) : Monkey array =
use mutable enum = lines
let output = ResizeArray ()
@@ -107,10 +108,10 @@ module Day11 =
let arg =
if EfficientString.equals "old" enum.Current then
None
-1L
else
let literal = Int64.Parse enum.Current
Some literal
literal
if enum.MoveNext () then
failwith "too many entries on row"
@@ -170,7 +171,7 @@ module Day11 =
}
|> output.Add
output :> IReadOnlyList<_>
output.ToArray ()
let oneRoundDivThree (monkeys : IReadOnlyList<Monkey>) (inspections : int64 array) =
for i in 0 .. monkeys.Count - 1 do
@@ -181,8 +182,8 @@ module Day11 =
let newWorry =
let arg =
match monkey.Argument with
| None -> worry
| Some l -> l
| -1L -> worry
| l -> l
if monkey.OperationIsPlus then worry + arg else worry * arg
@@ -201,7 +202,7 @@ module Day11 =
let part1 (lines : StringSplitEnumerator) : int64 =
let monkeys = parse lines
let mutable inspections = Array.zeroCreate<int64> monkeys.Count
let mutable inspections = Array.zeroCreate<int64> monkeys.Length
for _round in 1..20 do
oneRoundDivThree monkeys inspections
@@ -209,8 +210,8 @@ module Day11 =
inspections |> Array.sortInPlace
inspections.[inspections.Length - 1] * inspections.[inspections.Length - 2]
let oneRound (modulus : int64) (monkeys : IReadOnlyList<Monkey>) (inspections : int64 array) =
for i in 0 .. monkeys.Count - 1 do
let inline oneRound (modulus : int64) (monkeys : Monkey array) (inspections : int64 array) =
for i in 0 .. monkeys.Length - 1 do
let monkey = monkeys.[i]
inspections.[i] <- inspections.[i] + int64 monkey.StartingItems.Count
@@ -220,8 +221,8 @@ module Day11 =
let newWorry =
let arg =
match monkey.Argument with
| None -> worry
| Some l -> l
| -1L -> worry
| l -> l
if monkey.OperationIsPlus then worry + arg else worry * arg
@@ -241,7 +242,7 @@ module Day11 =
let part2 (lines : StringSplitEnumerator) : int64 =
let monkeys = parse lines
let mutable inspections = Array.zeroCreate<int64> monkeys.Count
let mutable inspections = Array.zeroCreate<int64> monkeys.Length
let modulus =
(1L, monkeys) ||> Seq.fold (fun i monkey -> i * monkey.TestDivisibleBy)

View File

@@ -6,7 +6,7 @@ open System
[<RequireQualifiedAccess>]
module Day8 =
let parse (lines : StringSplitEnumerator) : byte[] IReadOnlyList =
let parse (lines : StringSplitEnumerator) : byte[,] =
use mutable enum = lines
let output = ResizeArray ()
@@ -23,15 +23,15 @@ module Day8 =
output.Add arr
output :> _
Array2D.init output.Count output.[0].Length (fun x y -> output.[x].[y])
let isVisible (board : byte[] IReadOnlyList) (x : int) (y : int) : bool =
let isVisible (board : byte[,]) (x : int) (y : int) : bool =
// From the left?
let mutable isVisible = true
let mutable i = 0
while i < x && isVisible do
if board.[y].[i] >= board.[y].[x] then
if board.[y, i] >= board.[y, x] then
isVisible <- false
i <- i + 1
@@ -42,10 +42,10 @@ module Day8 =
// From the right?
let mutable isVisible = true
let mutable i = board.[0].Length - 1
let mutable i = board.GetLength 1 - 1
while i > x && isVisible do
if board.[y].[i] >= board.[y].[x] then
if board.[y, i] >= board.[y, x] then
isVisible <- false
i <- i - 1
@@ -54,45 +54,45 @@ module Day8 =
true
else
// From the top?
let mutable isVisible = true
let mutable i = 0
// From the top?
let mutable isVisible = true
let mutable i = 0
while i < y && isVisible do
if board.[i].[x] >= board.[y].[x] then
isVisible <- false
while i < y && isVisible do
if board.[i, x] >= board.[y, x] then
isVisible <- false
i <- i + 1
i <- i + 1
if isVisible then
true
else
if isVisible then
true
else
// From the bottom?
let mutable isVisible = true
let mutable i = board.Count - 1
// From the bottom?
let mutable isVisible = true
let mutable i = board.GetLength 0 - 1
while i > y && isVisible do
if board.[i].[x] >= board.[y].[x] then
isVisible <- false
while i > y && isVisible do
if board.[i, x] >= board.[y, x] then
isVisible <- false
i <- i - 1
i <- i - 1
isVisible
isVisible
let part1 (lines : StringSplitEnumerator) : int =
let board = parse lines
let mutable visibleCount = 0
for y in 0 .. board.Count - 1 do
for x in 0 .. board.[0].Length - 1 do
for y in 0 .. board.GetLength 0 - 1 do
for x in 0 .. board.GetLength 1 - 1 do
if isVisible board x y then
visibleCount <- visibleCount + 1
visibleCount
let scenicScore (board : byte[] IReadOnlyList) (x : int) (y : int) : int =
let scenicScore (board : byte[,]) (x : int) (y : int) : int =
let mutable scenicCount = 0
do
@@ -100,7 +100,7 @@ module Day8 =
let mutable i = y - 1
while i >= 0 && isVisible do
if board.[i].[x] >= board.[y].[x] then
if board.[i, x] >= board.[y, x] then
isVisible <- false
scenicCount <- scenicCount + 1
@@ -112,8 +112,8 @@ module Day8 =
let mutable i = y + 1
let mutable subCount = 0
while i < board.Count && isVisible do
if board.[i].[x] >= board.[y].[x] then
while i < board.GetLength 0 && isVisible do
if board.[i, x] >= board.[y, x] then
isVisible <- false
subCount <- subCount + 1
@@ -128,7 +128,7 @@ module Day8 =
let mutable subCount = 0
while i >= 0 && isVisible do
if board.[y].[i] >= board.[y].[x] then
if board.[y, i] >= board.[y, x] then
isVisible <- false
subCount <- subCount + 1
@@ -142,8 +142,8 @@ module Day8 =
let mutable i = x + 1
let mutable subCount = 0
while i < board.[0].Length && isVisible do
if board.[y].[i] >= board.[y].[x] then
while i < board.GetLength 1 && isVisible do
if board.[y, i] >= board.[y, x] then
isVisible <- false
subCount <- subCount + 1
@@ -159,8 +159,8 @@ module Day8 =
let board = parse lines
let mutable scenicMax = 0
for y in 0 .. board.Count - 1 do
for x in 0 .. board.[0].Length - 1 do
for y in 0 .. board.GetLength 0 - 1 do
for x in 0 .. board.GetLength 1 - 1 do
scenicMax <- max scenicMax (scenicScore board x y)
scenicMax

View File

@@ -81,9 +81,13 @@ module StringSplitEnumerator =
SplitOn = splitChar
}
let chomp (s : string) (e : byref<StringSplitEnumerator>) =
let chomp (s : string) (e : byref<StringSplitEnumerator>) : unit =
#if DEBUG
if not (e.MoveNext ()) || not (EfficientString.equals s e.Current) then
failwithf "expected '%s', got '%s'" s (e.Current.ToString ())
#else
e.MoveNext () |> ignore
#endif
let consumeInt (e : byref<StringSplitEnumerator>) : int =
if not (e.MoveNext ()) then