Compare commits
2 Commits
d94663ae0e
...
d881a7e878
Author | SHA1 | Date | |
---|---|---|---|
|
d881a7e878 | ||
|
471273633c |
@@ -10,6 +10,7 @@
|
|||||||
<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="Rational.fs"/>
|
||||||
<Compile Include="Day1.fs"/>
|
<Compile Include="Day1.fs"/>
|
||||||
<Compile Include="Day2.fs"/>
|
<Compile Include="Day2.fs"/>
|
||||||
<Compile Include="Day3.fs"/>
|
<Compile Include="Day3.fs"/>
|
||||||
@@ -18,6 +19,7 @@
|
|||||||
<Compile Include="Day6.fs"/>
|
<Compile Include="Day6.fs"/>
|
||||||
<Compile Include="Day7.fs"/>
|
<Compile Include="Day7.fs"/>
|
||||||
<Compile Include="Day8.fs"/>
|
<Compile Include="Day8.fs"/>
|
||||||
|
<Compile Include="Day9.fs"/>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
55
AdventOfCode2023.FSharp/AdventOfCode2023.FSharp.Lib/Day9.fs
Normal file
55
AdventOfCode2023.FSharp/AdventOfCode2023.FSharp.Lib/Day9.fs
Normal 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
|
@@ -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>
|
||||||
|
@@ -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
|
@@ -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 (
|
||||||
|
@@ -17,6 +17,7 @@
|
|||||||
<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"/>
|
||||||
<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"/>
|
||||||
@@ -27,6 +28,7 @@
|
|||||||
<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>
|
||||||
|
@@ -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
|
||||||
|
@@ -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
|
||||||
|
@@ -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
|
||||||
|
47
AdventOfCode2023.FSharp/Test/TestDay9.fs
Normal file
47
AdventOfCode2023.FSharp/Test/TestDay9.fs
Normal 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
|
3
AdventOfCode2023.FSharp/Test/samples/day9.txt
Normal file
3
AdventOfCode2023.FSharp/Test/samples/day9.txt
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
0 3 6 9 12 15
|
||||||
|
1 3 6 10 15 21
|
||||||
|
10 13 16 21 30 45
|
Reference in New Issue
Block a user