Compare commits
2 Commits
786a7eba8e
...
d2760ba051
Author | SHA1 | Date | |
---|---|---|---|
|
d2760ba051 | ||
|
148ea496c8 |
@@ -11,8 +11,9 @@ steps:
|
|||||||
- nix develop --command alejandra --check .
|
- nix develop --command alejandra --check .
|
||||||
- nix develop --command dotnet tool restore
|
- nix develop --command dotnet tool restore
|
||||||
- nix develop --command dotnet fantomas --check .
|
- nix develop --command dotnet fantomas --check .
|
||||||
# - nix develop --command dotnet publish AdventOfCode2023.FSharp/AdventOfCode2023.FSharp/AdventOfCode2023.FSharp.fsproj --configuration Release
|
# TODO: if https://github.com/dotnet/sdk/issues/37295 ever gets fixed, remove the PublishAot=false
|
||||||
# - nix develop --command sh -c "$(find . -type f -name AdventOfCode2023.FSharp | grep Release | grep publish) AdventOfCode2023.FSharp/Test/samples"
|
- "nix develop --command dotnet publish AdventOfCode2023.FSharp/AdventOfCode2023.FSharp/AdventOfCode2023.FSharp.fsproj --configuration Release -p:PublishAot=false"
|
||||||
|
- nix develop --command sh -c "$(find . -type f -name AdventOfCode2023.FSharp | grep Release | grep publish) AdventOfCode2023.FSharp/Test/samples"
|
||||||
|
|
||||||
when:
|
when:
|
||||||
- event: "push"
|
- event: "push"
|
||||||
|
@@ -14,6 +14,7 @@
|
|||||||
<Compile Include="Day4.fs" />
|
<Compile Include="Day4.fs" />
|
||||||
<Compile Include="Day5.fs" />
|
<Compile Include="Day5.fs" />
|
||||||
<Compile Include="Day6.fs" />
|
<Compile Include="Day6.fs" />
|
||||||
|
<Compile Include="Day7.fs" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
@@ -5,21 +5,13 @@ open System
|
|||||||
[<RequireQualifiedAccess>]
|
[<RequireQualifiedAccess>]
|
||||||
module Day1 =
|
module Day1 =
|
||||||
|
|
||||||
let firstDigit (s : ReadOnlySpan<char>) =
|
let inline firstDigit (s : ReadOnlySpan<char>) =
|
||||||
let mutable pos = 0
|
let pos = s.IndexOfAnyInRange ('0', '9')
|
||||||
|
|
||||||
while '0' > s.[pos] || s.[pos] > '9' do
|
|
||||||
pos <- pos + 1
|
|
||||||
|
|
||||||
byte s.[pos] - byte '0'
|
byte s.[pos] - byte '0'
|
||||||
|
|
||||||
// No surrogate pairs please!
|
// No surrogate pairs please!
|
||||||
let lastDigit (s : ReadOnlySpan<char>) =
|
let inline lastDigit (s : ReadOnlySpan<char>) =
|
||||||
let mutable pos = s.Length - 1
|
let pos = s.LastIndexOfAnyInRange ('0', '9')
|
||||||
|
|
||||||
while '0' > s.[pos] || s.[pos] > '9' do
|
|
||||||
pos <- pos - 1
|
|
||||||
|
|
||||||
byte s.[pos] - byte '0'
|
byte s.[pos] - byte '0'
|
||||||
|
|
||||||
let part1 (s : string) =
|
let part1 (s : string) =
|
||||||
@@ -35,35 +27,66 @@ module Day1 =
|
|||||||
|
|
||||||
total
|
total
|
||||||
|
|
||||||
let table =
|
let isDigitSpelled (s : ReadOnlySpan<char>) (pos : int) (answer : byref<byte>) =
|
||||||
[|
|
// Can't be bothered to write a jump-table compiler
|
||||||
"one", 1uy
|
if s.[pos] >= '0' && s.[pos] <= '9' then
|
||||||
"two", 2uy
|
answer <- byte s.[pos] - byte '0'
|
||||||
"three", 3uy
|
else if s.[pos] = 'o' then
|
||||||
"four", 4uy
|
if pos + 2 < s.Length && s.[pos + 1] = 'n' && s.[pos + 2] = 'e' then
|
||||||
"five", 5uy
|
answer <- 1uy
|
||||||
"six", 6uy
|
elif s.[pos] = 't' then
|
||||||
"seven", 7uy
|
if pos + 2 < s.Length && s.[pos + 1] = 'w' && s.[pos + 2] = 'o' then
|
||||||
"eight", 8uy
|
answer <- 2uy
|
||||||
"nine", 9uy
|
elif
|
||||||
|]
|
pos + 4 < s.Length
|
||||||
|
&& s.[pos + 1] = 'h'
|
||||||
|
&& s.[pos + 2] = 'r'
|
||||||
|
&& s.[pos + 3] = 'e'
|
||||||
|
&& s.[pos + 4] = 'e'
|
||||||
|
then
|
||||||
|
answer <- 3uy
|
||||||
|
elif s.[pos] = 'f' then
|
||||||
|
if pos + 3 < s.Length then
|
||||||
|
if s.[pos + 1] = 'o' && s.[pos + 2] = 'u' && s.[pos + 3] = 'r' then
|
||||||
|
answer <- 4uy
|
||||||
|
elif s.[pos + 1] = 'i' && s.[pos + 2] = 'v' && s.[pos + 3] = 'e' then
|
||||||
|
answer <- 5uy
|
||||||
|
elif s.[pos] = 's' then
|
||||||
|
if pos + 2 < s.Length && s.[pos + 1] = 'i' && s.[pos + 2] = 'x' then
|
||||||
|
answer <- 6uy
|
||||||
|
elif
|
||||||
|
pos + 4 < s.Length
|
||||||
|
&& s.[pos + 1] = 'e'
|
||||||
|
&& s.[pos + 2] = 'v'
|
||||||
|
&& s.[pos + 3] = 'e'
|
||||||
|
&& s.[pos + 4] = 'n'
|
||||||
|
then
|
||||||
|
answer <- 7uy
|
||||||
|
elif s.[pos] = 'e' then
|
||||||
|
if
|
||||||
|
pos + 4 < s.Length
|
||||||
|
&& s.[pos + 1] = 'i'
|
||||||
|
&& s.[pos + 2] = 'g'
|
||||||
|
&& s.[pos + 3] = 'h'
|
||||||
|
&& s.[pos + 4] = 't'
|
||||||
|
then
|
||||||
|
answer <- 8uy
|
||||||
|
elif s.[pos] = 'n' then
|
||||||
|
if
|
||||||
|
pos + 3 < s.Length
|
||||||
|
&& s.[pos + 1] = 'i'
|
||||||
|
&& s.[pos + 2] = 'n'
|
||||||
|
&& s.[pos + 3] = 'e'
|
||||||
|
then
|
||||||
|
answer <- 9uy
|
||||||
|
|
||||||
let firstDigitIncSpelled (s : ReadOnlySpan<char>) =
|
let firstDigitIncSpelled (s : ReadOnlySpan<char>) =
|
||||||
let mutable pos = 0
|
let mutable pos = 0
|
||||||
let mutable answer = 255uy
|
let mutable answer = 255uy
|
||||||
|
|
||||||
while answer = 255uy do
|
while answer = 255uy do
|
||||||
if s.[pos] >= '0' && s.[pos] <= '9' then
|
isDigitSpelled s pos &answer
|
||||||
answer <- byte s.[pos] - byte '0'
|
pos <- pos + 1
|
||||||
else
|
|
||||||
for i, value in table do
|
|
||||||
if
|
|
||||||
pos + i.Length < s.Length
|
|
||||||
&& MemoryExtensions.SequenceEqual (s.Slice (pos, i.Length), i)
|
|
||||||
then
|
|
||||||
answer <- value
|
|
||||||
|
|
||||||
pos <- pos + 1
|
|
||||||
|
|
||||||
answer
|
answer
|
||||||
|
|
||||||
@@ -72,17 +95,8 @@ module Day1 =
|
|||||||
let mutable answer = 255uy
|
let mutable answer = 255uy
|
||||||
|
|
||||||
while answer = 255uy do
|
while answer = 255uy do
|
||||||
if s.[pos] >= '0' && s.[pos] <= '9' then
|
isDigitSpelled s pos &answer
|
||||||
answer <- byte s.[pos] - byte '0'
|
pos <- pos - 1
|
||||||
else
|
|
||||||
for i, value in table do
|
|
||||||
if
|
|
||||||
pos - i.Length + 1 >= 0
|
|
||||||
&& MemoryExtensions.SequenceEqual (s.Slice (pos - i.Length + 1, i.Length), i)
|
|
||||||
then
|
|
||||||
answer <- value
|
|
||||||
|
|
||||||
pos <- pos - 1
|
|
||||||
|
|
||||||
answer
|
answer
|
||||||
|
|
||||||
|
@@ -146,7 +146,7 @@ module Day3 =
|
|||||||
|
|
||||||
let mutable answer = 0
|
let mutable answer = 0
|
||||||
|
|
||||||
for KeyValue (_gearPos, gears) in gears do
|
for gears in gears.Values do
|
||||||
if gears.Count = 2 then
|
if gears.Count = 2 then
|
||||||
answer <- answer + gears.[0] * gears.[1]
|
answer <- answer + gears.[0] * gears.[1]
|
||||||
|
|
||||||
|
@@ -54,8 +54,6 @@ module Day4 =
|
|||||||
|
|
||||||
total
|
total
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
let part2 (s : string) =
|
let part2 (s : string) =
|
||||||
use lines = StringSplitEnumerator.make '\n' s
|
use lines = StringSplitEnumerator.make '\n' s
|
||||||
let winningNumbers = ResizeArray<byte> ()
|
let winningNumbers = ResizeArray<byte> ()
|
||||||
|
86
AdventOfCode2023.FSharp/AdventOfCode2023.FSharp.Lib/Day7.fs
Normal file
86
AdventOfCode2023.FSharp/AdventOfCode2023.FSharp.Lib/Day7.fs
Normal file
@@ -0,0 +1,86 @@
|
|||||||
|
namespace AdventOfCode2023
|
||||||
|
|
||||||
|
open System
|
||||||
|
|
||||||
|
[<RequireQualifiedAccess>]
|
||||||
|
module Day7 =
|
||||||
|
|
||||||
|
let parse (s : string) =
|
||||||
|
use mutable lines = StringSplitEnumerator.make '\n' s
|
||||||
|
|
||||||
|
let times =
|
||||||
|
lines.MoveNext () |> ignore
|
||||||
|
use mutable line = StringSplitEnumerator.make' ' ' lines.Current
|
||||||
|
StringSplitEnumerator.chomp "Time:" &line
|
||||||
|
line.MoveNext () |> ignore
|
||||||
|
let times = ResizeArray ()
|
||||||
|
|
||||||
|
while line.MoveNext () do
|
||||||
|
if not line.Current.IsEmpty then
|
||||||
|
times.Add (UInt64.Parse line.Current)
|
||||||
|
|
||||||
|
times
|
||||||
|
|
||||||
|
let distance =
|
||||||
|
lines.MoveNext () |> ignore
|
||||||
|
use mutable line = StringSplitEnumerator.make' ' ' lines.Current
|
||||||
|
StringSplitEnumerator.chomp "Distance:" &line
|
||||||
|
line.MoveNext () |> ignore
|
||||||
|
let distance = ResizeArray ()
|
||||||
|
|
||||||
|
while line.MoveNext () do
|
||||||
|
if not line.Current.IsEmpty then
|
||||||
|
distance.Add (UInt64.Parse line.Current)
|
||||||
|
|
||||||
|
distance
|
||||||
|
|
||||||
|
times, distance
|
||||||
|
|
||||||
|
let furthest (distance : uint64) (toBeat : uint64) =
|
||||||
|
// Count i in [1 .. distance - 1] such that (distance - i) * i > toBeat
|
||||||
|
// i.e. such that distance * i - i * i > toBeat
|
||||||
|
// -i^2 + distance * i - toBeat = 0 when:
|
||||||
|
// i = (distance +- sqrt(distance^2 - 4 * toBeat)) / 2
|
||||||
|
let distFloat = float distance
|
||||||
|
let inside = sqrt (distFloat * distFloat - 4.0 * float toBeat)
|
||||||
|
let limit1 = (distFloat + inside) / 2.0
|
||||||
|
let limit2 = (distFloat - sqrt (distFloat * distFloat - 4.0 * float toBeat)) / 2.0
|
||||||
|
// round limit2 up and limit1 down
|
||||||
|
let limit1 = uint64 (floor limit1)
|
||||||
|
let limit2 = uint64 (ceil limit2)
|
||||||
|
// cope with edge case of an exact square
|
||||||
|
if (uint64 inside) * (uint64 inside) = uint64 distance * uint64 distance - 4uL * uint64 toBeat then
|
||||||
|
limit1 - limit2 - 1uL
|
||||||
|
else
|
||||||
|
limit1 - limit2 + 1uL
|
||||||
|
|
||||||
|
let part1 (s : string) =
|
||||||
|
let times, distance = parse s
|
||||||
|
let mutable answer = 1uL
|
||||||
|
|
||||||
|
for i = 0 to times.Count - 1 do
|
||||||
|
let time = times.[i]
|
||||||
|
let distance = distance.[i]
|
||||||
|
let winners = furthest time distance
|
||||||
|
answer <- answer * winners
|
||||||
|
|
||||||
|
answer
|
||||||
|
|
||||||
|
let concat (digits : ResizeArray<uint64>) : uint64 =
|
||||||
|
let mutable answer = 0uL
|
||||||
|
|
||||||
|
for digit in digits do
|
||||||
|
let mutable power = 10uL
|
||||||
|
|
||||||
|
while digit >= power do
|
||||||
|
power <- power * 10uL
|
||||||
|
|
||||||
|
answer <- answer * power + digit
|
||||||
|
|
||||||
|
answer
|
||||||
|
|
||||||
|
let part2 (s : string) =
|
||||||
|
let times, distance = parse s
|
||||||
|
let concatTime = concat times
|
||||||
|
let concatDist = concat distance
|
||||||
|
furthest concatTime concatDist
|
@@ -136,6 +136,21 @@ 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")
|
||||||
|
|
||||||
|
printfn "=====Day 7====="
|
||||||
|
|
||||||
|
do
|
||||||
|
let input = Path.Combine (dir.FullName, "day7.txt") |> File.ReadAllText
|
||||||
|
sw.Restart ()
|
||||||
|
let part1 = Day7.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 = Day7.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 (
|
||||||
|
@@ -15,6 +15,7 @@
|
|||||||
<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" />
|
||||||
<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" />
|
||||||
@@ -22,6 +23,7 @@
|
|||||||
<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" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
46
AdventOfCode2023.FSharp/Test/TestDay7.fs
Normal file
46
AdventOfCode2023.FSharp/Test/TestDay7.fs
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
namespace AdventOfCode2023.Test
|
||||||
|
|
||||||
|
open System
|
||||||
|
open AdventOfCode2023
|
||||||
|
open NUnit.Framework
|
||||||
|
open FsUnitTyped
|
||||||
|
open System.IO
|
||||||
|
|
||||||
|
[<TestFixture>]
|
||||||
|
module TestDay7 =
|
||||||
|
|
||||||
|
let sample = Assembly.getEmbeddedResource typeof<Dummy>.Assembly "day7.txt"
|
||||||
|
|
||||||
|
[<Test>]
|
||||||
|
let part1Sample () =
|
||||||
|
sample |> Day7.part1 |> shouldEqual 288uL
|
||||||
|
|
||||||
|
[<Test>]
|
||||||
|
let part2Sample () =
|
||||||
|
sample |> Day7.part2 |> shouldEqual 71503uL
|
||||||
|
|
||||||
|
[<Test>]
|
||||||
|
let part1Actual () =
|
||||||
|
let s =
|
||||||
|
try
|
||||||
|
File.ReadAllText (Path.Combine (__SOURCE_DIRECTORY__, "../../inputs/day6.txt"))
|
||||||
|
with
|
||||||
|
| :? DirectoryNotFoundException
|
||||||
|
| :? FileNotFoundException ->
|
||||||
|
Assert.Inconclusive ()
|
||||||
|
failwith "unreachable"
|
||||||
|
|
||||||
|
Day7.part1 s |> shouldEqual 32076uL
|
||||||
|
|
||||||
|
[<Test>]
|
||||||
|
let part2Actual () =
|
||||||
|
let s =
|
||||||
|
try
|
||||||
|
File.ReadAllText (Path.Combine (__SOURCE_DIRECTORY__, "../../inputs/day6.txt"))
|
||||||
|
with
|
||||||
|
| :? DirectoryNotFoundException
|
||||||
|
| :? FileNotFoundException ->
|
||||||
|
Assert.Inconclusive ()
|
||||||
|
failwith "unreachable"
|
||||||
|
|
||||||
|
Day7.part2 s |> shouldEqual 34278221uL
|
0
AdventOfCode2023.FSharp/Test/samples/day7.txt
Normal file
0
AdventOfCode2023.FSharp/Test/samples/day7.txt
Normal file
Reference in New Issue
Block a user