Compare commits
3 Commits
9c48e5fa96
...
c2d4820714
Author | SHA1 | Date | |
---|---|---|---|
|
c2d4820714 | ||
|
4e0ffe6e64 | ||
|
fc9e99aef0 |
@@ -13,6 +13,7 @@
|
||||
<Compile Include="Day3.fs" />
|
||||
<Compile Include="Day4.fs" />
|
||||
<Compile Include="Day5.fs" />
|
||||
<Compile Include="Day6.fs" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
86
AdventOfCode2023.FSharp/AdventOfCode2023.FSharp.Lib/Day6.fs
Normal file
86
AdventOfCode2023.FSharp/AdventOfCode2023.FSharp.Lib/Day6.fs
Normal file
@@ -0,0 +1,86 @@
|
||||
namespace AdventOfCode2023
|
||||
|
||||
open System
|
||||
|
||||
[<RequireQualifiedAccess>]
|
||||
module Day6 =
|
||||
|
||||
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
|
@@ -121,6 +121,21 @@ module Program =
|
||||
Console.WriteLine (part2.ToString ())
|
||||
Console.Error.WriteLine ((1_000.0 * float sw.ElapsedTicks / float Stopwatch.Frequency).ToString () + "ms")
|
||||
|
||||
printfn "=====Day 6====="
|
||||
|
||||
do
|
||||
let input = Path.Combine (dir.FullName, "day6.txt") |> File.ReadAllText
|
||||
sw.Restart ()
|
||||
let part1 = Day6.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 = Day6.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 (
|
||||
|
@@ -14,12 +14,14 @@
|
||||
<Compile Include="TestDay3.fs" />
|
||||
<Compile Include="TestDay4.fs" />
|
||||
<Compile Include="TestDay5.fs" />
|
||||
<Compile Include="TestDay6.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" />
|
||||
<EmbeddedResource Include="samples\day6.txt" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
46
AdventOfCode2023.FSharp/Test/TestDay6.fs
Normal file
46
AdventOfCode2023.FSharp/Test/TestDay6.fs
Normal file
@@ -0,0 +1,46 @@
|
||||
namespace AdventOfCode2023.Test
|
||||
|
||||
open System
|
||||
open AdventOfCode2023
|
||||
open NUnit.Framework
|
||||
open FsUnitTyped
|
||||
open System.IO
|
||||
|
||||
[<TestFixture>]
|
||||
module TestDay6 =
|
||||
|
||||
let sample = Assembly.getEmbeddedResource typeof<Dummy>.Assembly "day6.txt"
|
||||
|
||||
[<Test>]
|
||||
let part1Sample () =
|
||||
sample |> Day6.part1 |> shouldEqual 288uL
|
||||
|
||||
[<Test>]
|
||||
let part2Sample () =
|
||||
sample |> Day6.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"
|
||||
|
||||
Day6.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"
|
||||
|
||||
Day6.part2 s |> shouldEqual 34278221uL
|
2
AdventOfCode2023.FSharp/Test/samples/day6.txt
Normal file
2
AdventOfCode2023.FSharp/Test/samples/day6.txt
Normal file
@@ -0,0 +1,2 @@
|
||||
Time: 7 15 30
|
||||
Distance: 9 40 200
|
Reference in New Issue
Block a user