From 9a4edca0037c37a6a9feba3c7efdd4630b3099b3 Mon Sep 17 00:00:00 2001 From: Patrick Stevens Date: Fri, 9 Dec 2022 20:45:35 +0000 Subject: [PATCH] Perhaps a bit more efficient (#12) --- AdventOfCode2022/AdventOfCode2022.fsproj | 2 +- AdventOfCode2022/Day5.fs | 26 ++++++++--------- AdventOfCode2022/Day7.fs | 2 +- AdventOfCode2022/Day8.fs | 36 ++++++++++++------------ 4 files changed, 32 insertions(+), 34 deletions(-) diff --git a/AdventOfCode2022/AdventOfCode2022.fsproj b/AdventOfCode2022/AdventOfCode2022.fsproj index 289e020..0bfe6d5 100644 --- a/AdventOfCode2022/AdventOfCode2022.fsproj +++ b/AdventOfCode2022/AdventOfCode2022.fsproj @@ -1,7 +1,7 @@  - net6.0 + net7.0 true true diff --git a/AdventOfCode2022/Day5.fs b/AdventOfCode2022/Day5.fs index 18ef5f1..2718c19 100644 --- a/AdventOfCode2022/Day5.fs +++ b/AdventOfCode2022/Day5.fs @@ -1,6 +1,7 @@ namespace AdventOfCode2022 open System +open System.Collections.Generic type Day5Instruction = { @@ -17,7 +18,7 @@ module Day5 = while enumerator.MoveNext () && not (enumerator.Current.IsWhiteSpace ()) do let s = enumerator.Current - if s.Contains ']' then + if s.IndexOf ']' >= 0 then for i in 1..4 .. s.Length - 1 do let pile = (i - 1) / 4 @@ -30,7 +31,7 @@ module Day5 = Array.init piles.Count (fun i -> List.init piles.[i].Count (fun j -> piles.[i].[j])) - let rec private parseInstruction (enumerator : byref) = + let rec private parseInstruction (enumerator : byref) : Day5Instruction IReadOnlyList = let outputs = ResizeArray () while (enumerator.MoveNext ()) && (not (enumerator.Current.IsWhiteSpace ())) do @@ -59,9 +60,9 @@ module Day5 = } |> outputs.Add - List.init outputs.Count (fun i -> outputs.[i]) + outputs - let parse (s : StringSplitEnumerator) : char list array * Day5Instruction list = + let parse (s : StringSplitEnumerator) : char list array * Day5Instruction IReadOnlyList = use mutable enumerator = s let piles = parseDrawing &enumerator @@ -71,26 +72,23 @@ module Day5 = let part1 (lines : StringSplitEnumerator) : string = let piles, instructions = parse lines - let rec go (instructions : _ list) (piles : _ list array) = - match instructions with - | [] -> piles - | instr :: rest -> + let go (instructions : _ IReadOnlyList) (piles : _ list array) = + for instr in instructions do piles.[instr.To - 1] <- List.rev piles.[instr.From - 1].[0 .. instr.Count - 1] @ piles.[instr.To - 1] piles.[instr.From - 1] <- piles.[instr.From - 1].[instr.Count ..] - go rest piles + piles String (go instructions piles |> Array.map List.head) let part2 (lines : StringSplitEnumerator) : string = let piles, instructions = parse lines - let rec go (instructions : _ list) (piles : _ list array) = - match instructions with - | [] -> piles - | instr :: rest -> + let rec go (instructions : _ IReadOnlyList) (piles : _ list array) = + for instr in instructions do piles.[instr.To - 1] <- piles.[instr.From - 1].[0 .. instr.Count - 1] @ piles.[instr.To - 1] piles.[instr.From - 1] <- piles.[instr.From - 1].[instr.Count ..] - go rest piles + + piles String (go instructions piles |> Array.map List.head) diff --git a/AdventOfCode2022/Day7.fs b/AdventOfCode2022/Day7.fs index 8df0943..a168e95 100644 --- a/AdventOfCode2022/Day7.fs +++ b/AdventOfCode2022/Day7.fs @@ -177,5 +177,5 @@ module Day7 = let required = 30000000 - unused results - |> Seq.choose (fun (KeyValue (path, size)) -> if size >= required then Some size else None) + |> Seq.choose (fun (KeyValue (_, size)) -> if size >= required then Some size else None) |> Seq.min diff --git a/AdventOfCode2022/Day8.fs b/AdventOfCode2022/Day8.fs index 7ce290b..26013dc 100644 --- a/AdventOfCode2022/Day8.fs +++ b/AdventOfCode2022/Day8.fs @@ -26,12 +26,12 @@ module Day8 = output :> _ let isVisible (board : byte[] IReadOnlyList) (x : int) (y : int) : bool = - // From the top? + // From the left? let mutable isVisible = true let mutable i = 0 - while i < y && isVisible do - if board.[i].[x] >= board.[y].[x] then + while i < x && isVisible do + if board.[y].[i] >= board.[y].[x] then isVisible <- false i <- i + 1 @@ -40,12 +40,12 @@ module Day8 = true else - // From the bottom? + // From the right? let mutable isVisible = true - let mutable i = board.Count - 1 + let mutable i = board.[0].Length - 1 - while i > y && isVisible do - if board.[i].[x] >= board.[y].[x] then + while i > x && isVisible do + if board.[y].[i] >= board.[y].[x] then isVisible <- false i <- i - 1 @@ -54,12 +54,12 @@ module Day8 = true else - // From the left? + // From the top? let mutable isVisible = true let mutable i = 0 - while i < x && isVisible do - if board.[y].[i] >= board.[y].[x] then + while i < y && isVisible do + if board.[i].[x] >= board.[y].[x] then isVisible <- false i <- i + 1 @@ -68,17 +68,17 @@ module Day8 = true else - // From the right? - let mutable isVisible = true - let mutable i = board.[0].Length - 1 + // From the bottom? + let mutable isVisible = true + let mutable i = board.Count - 1 - while i > x && isVisible do - if board.[y].[i] >= 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