Scaffolding
This commit is contained in:
@@ -12,6 +12,7 @@
|
||||
<Compile Include="Day2.fs" />
|
||||
<Compile Include="Day3.fs" />
|
||||
<Compile Include="Day4.fs" />
|
||||
<Compile Include="Day5.fs" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
@@ -1,15 +1,21 @@
|
||||
namespace AdventOfCode2023
|
||||
|
||||
open System
|
||||
open System.Collections.Generic
|
||||
|
||||
[<RequireQualifiedAccess>]
|
||||
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) =
|
||||
use lines = StringSplitEnumerator.make '\n' s
|
||||
let mutable total = 0
|
||||
let winningNumbers = HashSet ()
|
||||
let winningNumbers = ResizeArray<byte> ()
|
||||
|
||||
for line in lines do
|
||||
if not (line.IsWhiteSpace ()) then
|
||||
@@ -30,14 +36,14 @@ module Day4 =
|
||||
if split.Current.[0] = '|' then
|
||||
accumulatingWinning <- false
|
||||
else
|
||||
winningNumbers.Add (Int32.Parse split.Current) |> ignore
|
||||
winningNumbers.Add (parseByte split.Current)
|
||||
split.MoveNext () |> ignore
|
||||
|
||||
let mutable answer = 0
|
||||
|
||||
while split.MoveNext () do
|
||||
if not split.Current.IsEmpty then
|
||||
let n = Int32.Parse split.Current
|
||||
let n = parseByte split.Current
|
||||
|
||||
if winningNumbers.Contains n then
|
||||
answer <- answer + 1
|
||||
@@ -51,9 +57,8 @@ module Day4 =
|
||||
|
||||
let part2 (s : string) =
|
||||
use lines = StringSplitEnumerator.make '\n' s
|
||||
let mutable total = 0
|
||||
let winningNumbers = HashSet ()
|
||||
let winners = ResizeArray ()
|
||||
let winningNumbers = ResizeArray<byte> ()
|
||||
let winners = ResizeArray<int> ()
|
||||
|
||||
for line in lines do
|
||||
if not (line.IsWhiteSpace ()) then
|
||||
@@ -74,14 +79,14 @@ module Day4 =
|
||||
if split.Current.[0] = '|' then
|
||||
accumulatingWinning <- false
|
||||
else
|
||||
winningNumbers.Add (Int32.Parse split.Current) |> ignore
|
||||
winningNumbers.Add (parseByte split.Current)
|
||||
split.MoveNext () |> ignore
|
||||
|
||||
let mutable answer = 0
|
||||
|
||||
while split.MoveNext () do
|
||||
if not split.Current.IsEmpty then
|
||||
let n = Int32.Parse split.Current
|
||||
let n = parseByte split.Current
|
||||
|
||||
if winningNumbers.Contains n then
|
||||
answer <- answer + 1
|
||||
|
16
AdventOfCode2023.FSharp/AdventOfCode2023.FSharp.Lib/Day5.fs
Normal file
16
AdventOfCode2023.FSharp/AdventOfCode2023.FSharp.Lib/Day5.fs
Normal 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
|
@@ -98,6 +98,19 @@ module Program =
|
||||
Console.WriteLine (part2.ToString ())
|
||||
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 ()
|
||||
|
||||
Console.Error.WriteLine (
|
||||
|
@@ -13,11 +13,13 @@
|
||||
<Compile Include="TestDay2.fs" />
|
||||
<Compile Include="TestDay3.fs" />
|
||||
<Compile Include="TestDay4.fs" />
|
||||
<Compile Include="TestDay5.fs" />
|
||||
<EmbeddedResource Include="samples\day1.txt" />
|
||||
<EmbeddedResource Include="samples\day1part1.txt" />
|
||||
<EmbeddedResource Include="samples\day2.txt" />
|
||||
<EmbeddedResource Include="samples\day3.txt" />
|
||||
<EmbeddedResource Include="samples\day4.txt" />
|
||||
<EmbeddedResource Include="samples\day5.txt" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
43
AdventOfCode2023.FSharp/Test/TestDay5.fs
Normal file
43
AdventOfCode2023.FSharp/Test/TestDay5.fs
Normal 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
|
0
AdventOfCode2023.FSharp/Test/samples/day5.txt
Normal file
0
AdventOfCode2023.FSharp/Test/samples/day5.txt
Normal file
Reference in New Issue
Block a user