Scaffolding

This commit is contained in:
Smaug123
2023-12-04 23:40:02 +00:00
parent c8c1cdc950
commit 65b82a3f74
7 changed files with 89 additions and 9 deletions

View File

@@ -12,6 +12,7 @@
<Compile Include="Day2.fs" /> <Compile Include="Day2.fs" />
<Compile Include="Day3.fs" /> <Compile Include="Day3.fs" />
<Compile Include="Day4.fs" /> <Compile Include="Day4.fs" />
<Compile Include="Day5.fs" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@@ -1,15 +1,21 @@
namespace AdventOfCode2023 namespace AdventOfCode2023
open System open System
open System.Collections.Generic
[<RequireQualifiedAccess>] [<RequireQualifiedAccess>]
module Day4 = module Day4 =
let inline parseByte (chars : ReadOnlySpan<char>) : byte =
// Byte.Parse (chars, NumberStyles.None, NumberFormatInfo.InvariantInfo)
let mutable answer = 0uy
for c in chars do
answer <- answer * 10uy + (byte c - 48uy)
answer
let part1 (s : string) = let part1 (s : string) =
use lines = StringSplitEnumerator.make '\n' s use lines = StringSplitEnumerator.make '\n' s
let mutable total = 0 let mutable total = 0
let winningNumbers = HashSet () let winningNumbers = ResizeArray<byte> ()
for line in lines do for line in lines do
if not (line.IsWhiteSpace ()) then if not (line.IsWhiteSpace ()) then
@@ -30,14 +36,14 @@ module Day4 =
if split.Current.[0] = '|' then if split.Current.[0] = '|' then
accumulatingWinning <- false accumulatingWinning <- false
else else
winningNumbers.Add (Int32.Parse split.Current) |> ignore winningNumbers.Add (parseByte split.Current)
split.MoveNext () |> ignore split.MoveNext () |> ignore
let mutable answer = 0 let mutable answer = 0
while split.MoveNext () do while split.MoveNext () do
if not split.Current.IsEmpty then if not split.Current.IsEmpty then
let n = Int32.Parse split.Current let n = parseByte split.Current
if winningNumbers.Contains n then if winningNumbers.Contains n then
answer <- answer + 1 answer <- answer + 1
@@ -51,9 +57,8 @@ module Day4 =
let part2 (s : string) = let part2 (s : string) =
use lines = StringSplitEnumerator.make '\n' s use lines = StringSplitEnumerator.make '\n' s
let mutable total = 0 let winningNumbers = ResizeArray<byte> ()
let winningNumbers = HashSet () let winners = ResizeArray<int> ()
let winners = ResizeArray ()
for line in lines do for line in lines do
if not (line.IsWhiteSpace ()) then if not (line.IsWhiteSpace ()) then
@@ -74,14 +79,14 @@ module Day4 =
if split.Current.[0] = '|' then if split.Current.[0] = '|' then
accumulatingWinning <- false accumulatingWinning <- false
else else
winningNumbers.Add (Int32.Parse split.Current) |> ignore winningNumbers.Add (parseByte split.Current)
split.MoveNext () |> ignore split.MoveNext () |> ignore
let mutable answer = 0 let mutable answer = 0
while split.MoveNext () do while split.MoveNext () do
if not split.Current.IsEmpty then if not split.Current.IsEmpty then
let n = Int32.Parse split.Current let n = parseByte split.Current
if winningNumbers.Contains n then if winningNumbers.Contains n then
answer <- answer + 1 answer <- answer + 1

View File

@@ -0,0 +1,16 @@
namespace AdventOfCode2023
open System
open System.Collections.Generic
[<RequireQualifiedAccess>]
module Day5 =
let part1 (s : string) =
use lines = StringSplitEnumerator.make '\n' s
0
let part2 (s : string) =
use lines = StringSplitEnumerator.make '\n' s
0

View File

@@ -98,6 +98,19 @@ module Program =
Console.WriteLine (part2.ToString ()) Console.WriteLine (part2.ToString ())
Console.Error.WriteLine ((1_000.0 * float sw.ElapsedTicks / float Stopwatch.Frequency).ToString () + "ms") Console.Error.WriteLine ((1_000.0 * float sw.ElapsedTicks / float Stopwatch.Frequency).ToString () + "ms")
do
let input = Path.Combine (dir.FullName, "day5.txt") |> File.ReadAllText
sw.Restart ()
let part1 = Day5.part1 input
sw.Stop ()
Console.WriteLine (part1.ToString ())
Console.Error.WriteLine ((1_000.0 * float sw.ElapsedTicks / float Stopwatch.Frequency).ToString () + "ms")
sw.Restart ()
let part2 = Day5.part2 input
sw.Stop ()
Console.WriteLine (part2.ToString ())
Console.Error.WriteLine ((1_000.0 * float sw.ElapsedTicks / float Stopwatch.Frequency).ToString () + "ms")
endToEnd.Stop () endToEnd.Stop ()
Console.Error.WriteLine ( Console.Error.WriteLine (

View File

@@ -13,11 +13,13 @@
<Compile Include="TestDay2.fs" /> <Compile Include="TestDay2.fs" />
<Compile Include="TestDay3.fs" /> <Compile Include="TestDay3.fs" />
<Compile Include="TestDay4.fs" /> <Compile Include="TestDay4.fs" />
<Compile Include="TestDay5.fs" />
<EmbeddedResource Include="samples\day1.txt" /> <EmbeddedResource Include="samples\day1.txt" />
<EmbeddedResource Include="samples\day1part1.txt" /> <EmbeddedResource Include="samples\day1part1.txt" />
<EmbeddedResource Include="samples\day2.txt" /> <EmbeddedResource Include="samples\day2.txt" />
<EmbeddedResource Include="samples\day3.txt" /> <EmbeddedResource Include="samples\day3.txt" />
<EmbeddedResource Include="samples\day4.txt" /> <EmbeddedResource Include="samples\day4.txt" />
<EmbeddedResource Include="samples\day5.txt" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@@ -0,0 +1,43 @@
namespace AdventOfCode2023.Test
open AdventOfCode2023
open NUnit.Framework
open FsUnitTyped
open System.IO
[<TestFixture>]
module TestDay5 =
let sample = Assembly.getEmbeddedResource typeof<Dummy>.Assembly "day5.txt"
[<Test>]
let part1Sample () = sample |> Day5.part1 |> shouldEqual 0
[<Test>]
let part2Sample () = sample |> Day5.part2 |> shouldEqual 0
[<Test>]
let part1Actual () =
let s =
try
File.ReadAllText (Path.Combine (__SOURCE_DIRECTORY__, "../../inputs/day5.txt"))
with
| :? DirectoryNotFoundException
| :? FileNotFoundException ->
Assert.Inconclusive ()
failwith "unreachable"
Day5.part1 s |> shouldEqual 0
[<Test>]
let part2Actual () =
let s =
try
File.ReadAllText (Path.Combine (__SOURCE_DIRECTORY__, "../../inputs/day5.txt"))
with
| :? DirectoryNotFoundException
| :? FileNotFoundException ->
Assert.Inconclusive ()
failwith "unreachable"
Day5.part2 s |> shouldEqual 0