Day 9 (#10)
All checks were successful
ci/woodpecker/push/build Pipeline was successful
ci/woodpecker/push/all-checks-complete Pipeline was successful

Went with Lagrange interpolation instead because that seemed easier.

Co-authored-by: Smaug123 <patrick+github@patrickstevens.co.uk>
Reviewed-on: #10
This commit is contained in:
2023-12-09 09:46:17 +00:00
parent d94663ae0e
commit 3b98d704d1
12 changed files with 281 additions and 78 deletions

View File

@@ -1,23 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFramework>net8.0</TargetFramework> <TargetFramework>net8.0</TargetFramework>
<GenerateDocumentationFile>true</GenerateDocumentationFile> <GenerateDocumentationFile>true</GenerateDocumentationFile>
</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"/>
</ItemGroup> <Compile Include="Day8.fs"/>
<Compile Include="Day9.fs"/>
</ItemGroup>
</Project> </Project>

View File

@@ -0,0 +1,55 @@
namespace AdventOfCode2023
open System
[<RequireQualifiedAccess>]
module Day9 =
let extrapolate (isStart : bool) (arr : ResizeArray<int64>) =
let mutable answer = 0L
let pos = if isStart then -1L else int64 arr.Count
for i = 0 to arr.Count - 1 do
let mutable product = Rational.ofInt arr.[i]
for j = 0 to arr.Count - 1 do
if j <> i then
product <- product * Rational.make (pos - int64 j) (int64 i - int64 j)
answer <- answer + Rational.assertIntegral product
answer
let part1 (s : string) =
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
let part2 (s : string) =
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 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

@@ -1,28 +1,28 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<OutputType>Exe</OutputType> <OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework> <TargetFramework>net8.0</TargetFramework>
<PublishAot>true</PublishAot> <PublishAot>true</PublishAot>
<InvariantGlobalization>true</InvariantGlobalization> <InvariantGlobalization>true</InvariantGlobalization>
<UseSystemResourceKeys>true</UseSystemResourceKeys> <UseSystemResourceKeys>true</UseSystemResourceKeys>
<IlcOptimizationPreference>Speed</IlcOptimizationPreference> <IlcOptimizationPreference>Speed</IlcOptimizationPreference>
<IlcGenerateStackTraceData>false</IlcGenerateStackTraceData> <IlcGenerateStackTraceData>false</IlcGenerateStackTraceData>
<DebuggerSupport>false</DebuggerSupport> <DebuggerSupport>false</DebuggerSupport>
<EnableUnsafeBinaryFormatterSerialization>false</EnableUnsafeBinaryFormatterSerialization> <EnableUnsafeBinaryFormatterSerialization>false</EnableUnsafeBinaryFormatterSerialization>
<EventSourceSupport>false</EventSourceSupport> <EventSourceSupport>false</EventSourceSupport>
<HttpActivityPropagationSupport>false</HttpActivityPropagationSupport> <HttpActivityPropagationSupport>false</HttpActivityPropagationSupport>
<MetadataUpdaterSupport>false</MetadataUpdaterSupport> <MetadataUpdaterSupport>false</MetadataUpdaterSupport>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Program.fs"/> <Compile Include="Program.fs"/>
</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

@@ -1,44 +1,46 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFramework>net8.0</TargetFramework> <TargetFramework>net8.0</TargetFramework>
<IsPackable>false</IsPackable> <IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject> <IsTestProject>true</IsTestProject>
</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"/>
<EmbeddedResource Include="samples\day1.txt" /> <Compile Include="TestDay9.fs"/>
<EmbeddedResource Include="samples\day1part1.txt" /> <EmbeddedResource Include="samples\day1.txt"/>
<EmbeddedResource Include="samples\day2.txt" /> <EmbeddedResource Include="samples\day1part1.txt"/>
<EmbeddedResource Include="samples\day3.txt" /> <EmbeddedResource Include="samples\day2.txt"/>
<EmbeddedResource Include="samples\day4.txt" /> <EmbeddedResource Include="samples\day3.txt"/>
<EmbeddedResource Include="samples\day5.txt" /> <EmbeddedResource Include="samples\day4.txt"/>
<EmbeddedResource Include="samples\day6.txt" /> <EmbeddedResource Include="samples\day5.txt"/>
<EmbeddedResource Include="samples\day7.txt" /> <EmbeddedResource Include="samples\day6.txt"/>
<EmbeddedResource Include="samples\day8part1.txt" /> <EmbeddedResource Include="samples\day7.txt"/>
<EmbeddedResource Include="samples\day8.txt" /> <EmbeddedResource Include="samples\day8part1.txt"/>
</ItemGroup> <EmbeddedResource Include="samples\day8.txt"/>
<EmbeddedResource Include="samples\day9.txt"/>
</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"/>
<PackageReference Include="coverlet.collector" Version="6.0.0"/> <PackageReference Include="coverlet.collector" Version="6.0.0"/>
</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

@@ -0,0 +1,47 @@
namespace AdventOfCode2023.Test
open AdventOfCode2023
open NUnit.Framework
open FsUnitTyped
open System.IO
[<TestFixture>]
module TestDay9 =
let sample = Assembly.getEmbeddedResource typeof<Dummy>.Assembly "day9.txt"
[<Test>]
let part1Sample () =
sample |> Day9.part1 |> shouldEqual 114L
[<Test>]
let part2Sample () =
Assembly.getEmbeddedResource typeof<Dummy>.Assembly "day9.txt"
|> Day9.part2
|> shouldEqual 2L
[<Test>]
let part1Actual () =
let s =
try
File.ReadAllText (Path.Combine (__SOURCE_DIRECTORY__, "../../inputs/day9.txt"))
with
| :? DirectoryNotFoundException
| :? FileNotFoundException ->
Assert.Inconclusive ()
failwith "unreachable"
Day9.part1 s |> shouldEqual 1898776583L
[<Test>]
let part2Actual () =
let s =
try
File.ReadAllText (Path.Combine (__SOURCE_DIRECTORY__, "../../inputs/day9.txt"))
with
| :? DirectoryNotFoundException
| :? FileNotFoundException ->
Assert.Inconclusive ()
failwith "unreachable"
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