mirror of
https://github.com/Smaug123/AdventOfCode2022
synced 2025-10-14 22:08:44 +00:00
Perhaps a bit more efficient (#12)
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<TargetFramework>net7.0</TargetFramework>
|
||||
<GenerateDocumentationFile>true</GenerateDocumentationFile>
|
||||
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
|
||||
</PropertyGroup>
|
||||
|
@@ -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<StringSplitEnumerator>) =
|
||||
let rec private parseInstruction (enumerator : byref<StringSplitEnumerator>) : 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)
|
||||
|
@@ -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
|
||||
|
@@ -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
|
||||
|
Reference in New Issue
Block a user