Day 9
All checks were successful
ci/woodpecker/push/build Pipeline was successful
ci/woodpecker/push/all-checks-complete Pipeline was successful
ci/woodpecker/pr/build Pipeline was successful
ci/woodpecker/pr/all-checks-complete Pipeline was successful

This commit is contained in:
Smaug123
2023-12-09 09:37:18 +00:00
parent 471273633c
commit d881a7e878
12 changed files with 222 additions and 107 deletions

View File

@@ -6,19 +6,20 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Arr2D.fs" /> <Compile Include="Arr2D.fs"/>
<Compile Include="ResizeArray.fs" /> <Compile Include="ResizeArray.fs"/>
<Compile Include="EfficientString.fs" /> <Compile Include="EfficientString.fs"/>
<Compile Include="Arithmetic.fs" /> <Compile Include="Arithmetic.fs"/>
<Compile Include="Day1.fs" /> <Compile Include="Rational.fs"/>
<Compile Include="Day2.fs" /> <Compile Include="Day1.fs"/>
<Compile Include="Day3.fs" /> <Compile Include="Day2.fs"/>
<Compile Include="Day4.fs" /> <Compile Include="Day3.fs"/>
<Compile Include="Day5.fs" /> <Compile Include="Day4.fs"/>
<Compile Include="Day6.fs" /> <Compile Include="Day5.fs"/>
<Compile Include="Day7.fs" /> <Compile Include="Day6.fs"/>
<Compile Include="Day8.fs" /> <Compile Include="Day7.fs"/>
<Compile Include="Day9.fs" /> <Compile Include="Day8.fs"/>
<Compile Include="Day9.fs"/>
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@@ -1,37 +1,55 @@
namespace AdventOfCode2023 namespace AdventOfCode2023
open System open System
open System.Collections.Generic
[<RequireQualifiedAccess>] [<RequireQualifiedAccess>]
module Day9 = module Day9 =
let parse (s : string) = let extrapolate (isStart : bool) (arr : ResizeArray<int64>) =
use mutable lines = StringSplitEnumerator.make '\n' s let mutable answer = 0L
let pos = if isStart then -1L else int64 arr.Count
lines.MoveNext () |> ignore for i = 0 to arr.Count - 1 do
let mutable product = Rational.ofInt arr.[i]
let stepsLine = lines.Current.TrimEnd () for j = 0 to arr.Count - 1 do
let steps = Array.zeroCreate stepsLine.Length if j <> i then
product <- product * Rational.make (pos - int64 j) (int64 i - int64 j)
for i = 0 to stepsLine.Length - 1 do answer <- answer + Rational.assertIntegral product
steps.[i] <- (stepsLine.[i] = 'R')
let dict = Dictionary () answer
while lines.MoveNext () do
if not lines.Current.IsEmpty then
use mutable line = StringSplitEnumerator.make' ' ' lines.Current
line.MoveNext () |> ignore
{
Steps = steps
Nodes = dict
}
let part1 (s : string) = let part1 (s : string) =
let data = parse s use s = StringSplitEnumerator.make '\n' s
let mutable answer = 0L
let arr = ResizeArray ()
for line in s do
arr.Clear ()
use line = StringSplitEnumerator.make' ' ' line
for number in line do
let number = Int64.Parse number
arr.Add number
answer <- answer + extrapolate false arr
answer answer
let part2 (s : string) = let part2 (s : string) =
let data = parse s use s = StringSplitEnumerator.make '\n' s
0 let mutable answer = 0L
let arr = ResizeArray ()
for line in s do
arr.Clear ()
use line = StringSplitEnumerator.make' ' ' line
for number in line do
let number = Int64.Parse number
arr.Add number
answer <- answer + extrapolate true arr
answer

View File

@@ -1,7 +1,6 @@
namespace AdventOfCode2023 namespace AdventOfCode2023
open System open System
open System.Globalization
open System.Runtime.CompilerServices open System.Runtime.CompilerServices
type EfficientString = System.ReadOnlySpan<char> type EfficientString = System.ReadOnlySpan<char>

View File

@@ -0,0 +1,82 @@
namespace AdventOfCode2023
[<Struct>]
type Rational<'a
when 'a : (static member (+) : 'a * 'a -> 'a)
and 'a : (static member (*) : 'a * 'a -> 'a)
and 'a : (static member (/) : 'a * 'a -> 'a)
and 'a : (static member (-) : 'a * 'a -> 'a)
and 'a : (static member Zero : 'a)
and 'a : (static member One : 'a)
and 'a : comparison> =
{
Numerator : 'a
Denominator : 'a
}
static member inline (+) (a : Rational<'a>, b : Rational<'a>) =
let numerator = a.Numerator * b.Denominator + b.Numerator * a.Denominator
let denominator = a.Denominator * b.Denominator
let hcf = (Arithmetic.euclideanAlgorithm numerator denominator).Hcf
{
Numerator = numerator / hcf
Denominator = denominator / hcf
}
static member inline (*) (a : Rational<'a>, b : Rational<'a>) =
let numerator = a.Numerator * b.Numerator
let denominator = a.Denominator * b.Denominator
let hcf = (Arithmetic.euclideanAlgorithm numerator denominator).Hcf
{
Numerator = numerator / hcf
Denominator = denominator / hcf
}
[<RequireQualifiedAccess>]
module Rational =
let inline ofInt< ^a
when 'a : (static member (+) : 'a * 'a -> 'a)
and 'a : (static member (*) : 'a * 'a -> 'a)
and 'a : (static member (/) : 'a * 'a -> 'a)
and 'a : (static member (-) : 'a * 'a -> 'a)
and 'a : (static member Zero : 'a)
and 'a : (static member One : 'a)
and 'a : comparison>
(a : 'a)
=
{
Numerator = a
Denominator = LanguagePrimitives.GenericOne
}
let inline make< ^a
when 'a : (static member (+) : 'a * 'a -> 'a)
and 'a : (static member (*) : 'a * 'a -> 'a)
and 'a : (static member (/) : 'a * 'a -> 'a)
and 'a : (static member (-) : 'a * 'a -> 'a)
and 'a : (static member Zero : 'a)
and 'a : (static member One : 'a)
and 'a : comparison>
(numerator : 'a)
(denominator : 'a)
=
let hcf = (Arithmetic.euclideanAlgorithm numerator denominator).Hcf
{
Numerator = numerator / hcf
Denominator = denominator / hcf
}
let inline assertIntegral< ^a
when 'a : (static member (+) : 'a * 'a -> 'a)
and 'a : (static member (*) : 'a * 'a -> 'a)
and 'a : (static member (/) : 'a * 'a -> 'a)
and 'a : (static member (-) : 'a * 'a -> 'a)
and 'a : (static member Zero : 'a)
and 'a : (static member One : 'a)
and 'a : comparison>
(r : Rational<'a>)
=
r.Numerator

View File

@@ -22,7 +22,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\AdventOfCode2023.FSharp.Lib\AdventOfCode2023.FSharp.Lib.fsproj" /> <ProjectReference Include="..\AdventOfCode2023.FSharp.Lib\AdventOfCode2023.FSharp.Lib.fsproj"/>
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@@ -179,6 +179,22 @@ 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")
Console.WriteLine "=====Day 9====="
do
let input = Path.Combine (dir.FullName, "day9.txt") |> File.ReadAllText
sw.Restart ()
let part1 = Day9.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 = Day9.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

@@ -8,30 +8,31 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Util.fs" /> <Compile Include="Util.fs"/>
<Compile Include="TestDay1.fs" /> <Compile Include="TestDay1.fs"/>
<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" /> <Compile Include="TestDay5.fs"/>
<Compile Include="TestDay6.fs" /> <Compile Include="TestDay6.fs"/>
<Compile Include="TestDay7.fs" /> <Compile Include="TestDay7.fs"/>
<Compile Include="TestDay8.fs" /> <Compile Include="TestDay8.fs"/>
<Compile Include="TestDay9.fs" /> <Compile Include="TestDay9.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" /> <EmbeddedResource Include="samples\day5.txt"/>
<EmbeddedResource Include="samples\day6.txt" /> <EmbeddedResource Include="samples\day6.txt"/>
<EmbeddedResource Include="samples\day7.txt" /> <EmbeddedResource Include="samples\day7.txt"/>
<EmbeddedResource Include="samples\day8part1.txt" /> <EmbeddedResource Include="samples\day8part1.txt"/>
<EmbeddedResource Include="samples\day8.txt" /> <EmbeddedResource Include="samples\day8.txt"/>
<EmbeddedResource Include="samples\day9.txt"/>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="FsUnit" Version="5.6.1" /> <PackageReference Include="FsUnit" Version="5.6.1"/>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0"/> <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0"/>
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1"/> <PackageReference Include="NUnit3TestAdapter" Version="4.2.1"/>
<PackageReference Include="NUnit.Analyzers" Version="3.6.1"/> <PackageReference Include="NUnit.Analyzers" Version="3.6.1"/>
@@ -39,7 +40,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\AdventOfCode2023.FSharp.Lib\AdventOfCode2023.FSharp.Lib.fsproj" /> <ProjectReference Include="..\AdventOfCode2023.FSharp.Lib\AdventOfCode2023.FSharp.Lib.fsproj"/>
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@@ -1,6 +1,5 @@
namespace AdventOfCode2023.Test namespace AdventOfCode2023.Test
open System
open AdventOfCode2023 open AdventOfCode2023
open NUnit.Framework open NUnit.Framework
open FsUnitTyped open FsUnitTyped

View File

@@ -1,6 +1,5 @@
namespace AdventOfCode2023.Test namespace AdventOfCode2023.Test
open System
open AdventOfCode2023 open AdventOfCode2023
open NUnit.Framework open NUnit.Framework
open FsUnitTyped open FsUnitTyped

View File

@@ -1,6 +1,5 @@
namespace AdventOfCode2023.Test namespace AdventOfCode2023.Test
open System
open AdventOfCode2023 open AdventOfCode2023
open NUnit.Framework open NUnit.Framework
open FsUnitTyped open FsUnitTyped

View File

@@ -12,15 +12,13 @@ module TestDay9 =
[<Test>] [<Test>]
let part1Sample () = let part1Sample () =
sample sample |> Day9.part1 |> shouldEqual 114L
|> Day9.part1
|> shouldEqual 2
[<Test>] [<Test>]
let part2Sample () = let part2Sample () =
Assembly.getEmbeddedResource typeof<Dummy>.Assembly "day9.txt" Assembly.getEmbeddedResource typeof<Dummy>.Assembly "day9.txt"
|> Day9.part2 |> Day9.part2
|> shouldEqual 0 |> shouldEqual 2L
[<Test>] [<Test>]
let part1Actual () = let part1Actual () =
@@ -33,7 +31,7 @@ module TestDay9 =
Assert.Inconclusive () Assert.Inconclusive ()
failwith "unreachable" failwith "unreachable"
Day8.part1 s |> shouldEqual 0 Day9.part1 s |> shouldEqual 1898776583L
[<Test>] [<Test>]
let part2Actual () = let part2Actual () =
@@ -46,4 +44,4 @@ module TestDay9 =
Assert.Inconclusive () Assert.Inconclusive ()
failwith "unreachable" failwith "unreachable"
Day8.part2 s |> shouldEqual 0 Day9.part2 s |> shouldEqual 1100L

View File

@@ -0,0 +1,3 @@
0 3 6 9 12 15
1 3 6 10 15 21
10 13 16 21 30 45