Compare commits
4 Commits
woodpekcer
...
16053d1a51
Author | SHA1 | Date | |
---|---|---|---|
|
16053d1a51 | ||
|
4757ca4753 | ||
8af8916d46 | |||
3b98d704d1 |
@@ -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,9 @@
|
|||||||
<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"/>
|
||||||
|
<Compile Include="Day10.fs" />
|
||||||
|
<Compile Include="Day11.fs" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
327
AdventOfCode2023.FSharp/AdventOfCode2023.FSharp.Lib/Day10.fs
Normal file
327
AdventOfCode2023.FSharp/AdventOfCode2023.FSharp.Lib/Day10.fs
Normal file
@@ -0,0 +1,327 @@
|
|||||||
|
namespace AdventOfCode2023
|
||||||
|
|
||||||
|
#if DEBUG
|
||||||
|
#else
|
||||||
|
#nowarn "9"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
open System
|
||||||
|
|
||||||
|
[<RequireQualifiedAccess>]
|
||||||
|
module Day10 =
|
||||||
|
|
||||||
|
/// Returns first the line number, then the position within that line.
|
||||||
|
/// lineLength includes the newline, as does pos.
|
||||||
|
let inline private toRowAndCol (lineLength : int) (pos : int) : struct (int * int) =
|
||||||
|
let lineNum = pos / lineLength
|
||||||
|
let withinLine = pos % lineLength
|
||||||
|
struct (lineNum, withinLine)
|
||||||
|
|
||||||
|
let inline private ofRowAndCol (lineLength : int) (lineNum : int) (col : int) : int = lineNum * lineLength + col
|
||||||
|
|
||||||
|
let inline nextPoint (s : ReadOnlySpan<char>) (lineLength : int) (currPos : int) (prevPos : int) =
|
||||||
|
let struct (currLineNum, currCol) = toRowAndCol lineLength currPos
|
||||||
|
let struct (prevLineNum, prevCol) = toRowAndCol lineLength prevPos
|
||||||
|
|
||||||
|
match s.[currPos] with
|
||||||
|
| '|' ->
|
||||||
|
if prevLineNum < currLineNum then
|
||||||
|
ofRowAndCol lineLength (currLineNum + 1) currCol
|
||||||
|
else
|
||||||
|
ofRowAndCol lineLength (currLineNum - 1) currCol
|
||||||
|
| '-' ->
|
||||||
|
if prevCol < currCol then
|
||||||
|
ofRowAndCol lineLength currLineNum (currCol + 1)
|
||||||
|
else
|
||||||
|
ofRowAndCol lineLength currLineNum (currCol - 1)
|
||||||
|
| 'L' ->
|
||||||
|
if prevLineNum = currLineNum then
|
||||||
|
ofRowAndCol lineLength (currLineNum - 1) currCol
|
||||||
|
else
|
||||||
|
ofRowAndCol lineLength currLineNum (currCol + 1)
|
||||||
|
| '7' ->
|
||||||
|
if prevLineNum = currLineNum then
|
||||||
|
ofRowAndCol lineLength (currLineNum + 1) currCol
|
||||||
|
else
|
||||||
|
ofRowAndCol lineLength currLineNum (currCol - 1)
|
||||||
|
| 'F' ->
|
||||||
|
if prevLineNum = currLineNum then
|
||||||
|
ofRowAndCol lineLength (currLineNum + 1) currCol
|
||||||
|
else
|
||||||
|
ofRowAndCol lineLength currLineNum (currCol + 1)
|
||||||
|
| 'J' ->
|
||||||
|
if prevLineNum = currLineNum then
|
||||||
|
ofRowAndCol lineLength (currLineNum - 1) currCol
|
||||||
|
else
|
||||||
|
ofRowAndCol lineLength currLineNum (currCol - 1)
|
||||||
|
| c -> failwithf "unrecognised: %c" c
|
||||||
|
|
||||||
|
let part1 (s : string) =
|
||||||
|
let s = s.AsSpan ()
|
||||||
|
let lineLength = (s.IndexOf '\n' + 1)
|
||||||
|
let startPos = s.IndexOf 'S'
|
||||||
|
|
||||||
|
let struct (startLine, startCol) = toRowAndCol lineLength startPos
|
||||||
|
let mutable distance = 1
|
||||||
|
let mutable prevPointA = startPos
|
||||||
|
let mutable prevPointB = startPos
|
||||||
|
|
||||||
|
let mutable pointA =
|
||||||
|
let pos = ofRowAndCol lineLength startLine (startCol - 1)
|
||||||
|
|
||||||
|
match s.[pos] with
|
||||||
|
| '-'
|
||||||
|
| 'L'
|
||||||
|
| 'F' -> pos
|
||||||
|
| _ ->
|
||||||
|
|
||||||
|
let pos = ofRowAndCol lineLength startLine (startCol + 1)
|
||||||
|
|
||||||
|
match s.[pos] with
|
||||||
|
| '-'
|
||||||
|
| 'J'
|
||||||
|
| '7' -> pos
|
||||||
|
| _ ->
|
||||||
|
|
||||||
|
ofRowAndCol lineLength (startLine + 1) startCol
|
||||||
|
|
||||||
|
let mutable pointB =
|
||||||
|
let pos = ofRowAndCol lineLength (startLine - 1) startCol
|
||||||
|
|
||||||
|
match if pos >= 0 then s.[pos] else 'n' with
|
||||||
|
| '|'
|
||||||
|
| '7'
|
||||||
|
| 'F' -> pos
|
||||||
|
| _ ->
|
||||||
|
|
||||||
|
let pos = ofRowAndCol lineLength (startLine + 1) startCol
|
||||||
|
|
||||||
|
match if pos < s.Length then s.[pos] else 'n' with
|
||||||
|
| '|'
|
||||||
|
| 'L'
|
||||||
|
| 'J' -> pos
|
||||||
|
| _ ->
|
||||||
|
|
||||||
|
let pos = ofRowAndCol lineLength startLine (startCol + 1)
|
||||||
|
|
||||||
|
match if pos < s.Length then s.[pos] else 'n' with
|
||||||
|
| '-'
|
||||||
|
| 'J'
|
||||||
|
| '7' -> pos
|
||||||
|
| _ -> ofRowAndCol lineLength startLine (startCol - 1)
|
||||||
|
|
||||||
|
while pointA <> pointB do
|
||||||
|
let currentA = pointA
|
||||||
|
pointA <- nextPoint s lineLength pointA prevPointA
|
||||||
|
prevPointA <- currentA
|
||||||
|
|
||||||
|
let currentB = pointB
|
||||||
|
pointB <- nextPoint s lineLength pointB prevPointB
|
||||||
|
prevPointB <- currentB
|
||||||
|
|
||||||
|
distance <- distance + 1
|
||||||
|
|
||||||
|
distance
|
||||||
|
|
||||||
|
let floodFill (stackBuf : ResizeArray<_>) (s : Arr2D<byte>) (currX : int) (currY : int) =
|
||||||
|
stackBuf.Clear ()
|
||||||
|
stackBuf.Add currX
|
||||||
|
stackBuf.Add currY
|
||||||
|
|
||||||
|
while stackBuf.Count > 0 do
|
||||||
|
let currY = stackBuf.[stackBuf.Count - 1]
|
||||||
|
stackBuf.RemoveAt (stackBuf.Count - 1)
|
||||||
|
let currX = stackBuf.[stackBuf.Count - 1]
|
||||||
|
stackBuf.RemoveAt (stackBuf.Count - 1)
|
||||||
|
|
||||||
|
if currX > 0 then
|
||||||
|
if Arr2D.get s (currX - 1) currY = 0uy then
|
||||||
|
Arr2D.set s (currX - 1) currY 2uy
|
||||||
|
stackBuf.Add (currX - 1)
|
||||||
|
stackBuf.Add currY
|
||||||
|
|
||||||
|
if currX < s.Width - 1 then
|
||||||
|
if Arr2D.get s (currX + 1) currY = 0uy then
|
||||||
|
Arr2D.set s (currX + 1) currY 2uy
|
||||||
|
stackBuf.Add (currX + 1)
|
||||||
|
stackBuf.Add currY
|
||||||
|
|
||||||
|
if currY > 0 then
|
||||||
|
if Arr2D.get s currX (currY - 1) = 0uy then
|
||||||
|
Arr2D.set s currX (currY - 1) 2uy
|
||||||
|
stackBuf.Add currX
|
||||||
|
stackBuf.Add (currY - 1)
|
||||||
|
|
||||||
|
if currY < s.Height - 1 then
|
||||||
|
if Arr2D.get s currX (currY + 1) = 0uy then
|
||||||
|
Arr2D.set s currX (currY + 1) 2uy
|
||||||
|
stackBuf.Add currX
|
||||||
|
stackBuf.Add (currY + 1)
|
||||||
|
|
||||||
|
let print (s : Arr2D<byte>) =
|
||||||
|
for y = 0 to s.Height - 1 do
|
||||||
|
for x = 0 to s.Width - 1 do
|
||||||
|
match Arr2D.get s x y with
|
||||||
|
| 0uy -> printf " "
|
||||||
|
| 1uy -> printf "#"
|
||||||
|
| 2uy -> printf "."
|
||||||
|
| s -> failwithf "unrecognised: %i" s
|
||||||
|
|
||||||
|
printfn ""
|
||||||
|
|
||||||
|
printfn ""
|
||||||
|
printfn ""
|
||||||
|
|
||||||
|
let inline setAt (arr : Arr2D<byte>) (x : int) (y : int) (matching : char) (target : byte) =
|
||||||
|
Arr2D.set arr x y target
|
||||||
|
|
||||||
|
match matching with
|
||||||
|
| '-' ->
|
||||||
|
Arr2D.set arr (x - 1) y target
|
||||||
|
Arr2D.set arr (x + 1) y target
|
||||||
|
| '|' ->
|
||||||
|
Arr2D.set arr x (y - 1) target
|
||||||
|
Arr2D.set arr x (y + 1) target
|
||||||
|
| 'L' ->
|
||||||
|
Arr2D.set arr x (y - 1) target
|
||||||
|
Arr2D.set arr (x + 1) y target
|
||||||
|
| 'J' ->
|
||||||
|
Arr2D.set arr x (y - 1) target
|
||||||
|
Arr2D.set arr (x - 1) y target
|
||||||
|
| '7' ->
|
||||||
|
Arr2D.set arr x (y + 1) target
|
||||||
|
Arr2D.set arr (x - 1) y target
|
||||||
|
| 'F' ->
|
||||||
|
Arr2D.set arr x (y + 1) target
|
||||||
|
Arr2D.set arr (x + 1) y target
|
||||||
|
| c -> failwithf "bad char: %c" c
|
||||||
|
|
||||||
|
let part2 (s : string) =
|
||||||
|
let s = s.AsSpan ()
|
||||||
|
let lineCount = s.Count '\n'
|
||||||
|
let lineLength = (s.IndexOf '\n' + 1)
|
||||||
|
let startPos = s.IndexOf 'S'
|
||||||
|
|
||||||
|
let buffer = Array.zeroCreate (lineCount * lineLength * 9)
|
||||||
|
#if DEBUG
|
||||||
|
let system : Arr2D<byte> =
|
||||||
|
{
|
||||||
|
Elements = buffer
|
||||||
|
Width = 3 * lineLength
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
use ptr = fixed buffer
|
||||||
|
|
||||||
|
let system : Arr2D<byte> =
|
||||||
|
{
|
||||||
|
Elements = ptr
|
||||||
|
Length = buffer.Length
|
||||||
|
Width = 3 * lineLength
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
let struct (startLine, startCol) = toRowAndCol lineLength startPos
|
||||||
|
let mutable prevPointA = startPos
|
||||||
|
let mutable prevPointB = startPos
|
||||||
|
|
||||||
|
Arr2D.set system (3 * startCol + 1) (3 * startLine + 1) 1uy
|
||||||
|
|
||||||
|
let mutable pointA =
|
||||||
|
let pos = ofRowAndCol lineLength startLine (startCol - 1)
|
||||||
|
|
||||||
|
match if pos >= 0 then s.[pos] else 'n' with
|
||||||
|
| '-'
|
||||||
|
| 'L'
|
||||||
|
| 'F' ->
|
||||||
|
Arr2D.set system (3 * startCol) (3 * startLine + 1) 1uy
|
||||||
|
pos
|
||||||
|
| _ ->
|
||||||
|
|
||||||
|
let pos = ofRowAndCol lineLength startLine (startCol + 1)
|
||||||
|
|
||||||
|
match if pos < s.Length then s.[pos] else 'n' with
|
||||||
|
| '-'
|
||||||
|
| 'J'
|
||||||
|
| '7' ->
|
||||||
|
Arr2D.set system (3 * startCol + 2) (3 * startLine + 1) 1uy
|
||||||
|
pos
|
||||||
|
| _ ->
|
||||||
|
|
||||||
|
Arr2D.set system (3 * startCol) (3 * startLine) 1uy
|
||||||
|
Arr2D.set system (3 * startCol) (3 * startLine + 2) 1uy
|
||||||
|
ofRowAndCol lineLength (startLine + 1) startCol
|
||||||
|
|
||||||
|
let mutable pointB =
|
||||||
|
let pos = ofRowAndCol lineLength (startLine - 1) startCol
|
||||||
|
|
||||||
|
match if pos >= 0 then s.[pos] else 'n' with
|
||||||
|
| '|'
|
||||||
|
| '7'
|
||||||
|
| 'F' ->
|
||||||
|
Arr2D.set system (3 * startCol + 1) (3 * startLine) 1uy
|
||||||
|
pos
|
||||||
|
| _ ->
|
||||||
|
|
||||||
|
let pos = ofRowAndCol lineLength (startLine + 1) startCol
|
||||||
|
|
||||||
|
match if pos < s.Length then s.[pos] else 'n' with
|
||||||
|
| '|'
|
||||||
|
| 'L'
|
||||||
|
| 'J' ->
|
||||||
|
Arr2D.set system (3 * startCol + 1) (3 * startLine + 2) 1uy
|
||||||
|
pos
|
||||||
|
| _ ->
|
||||||
|
|
||||||
|
let pos = ofRowAndCol lineLength startLine (startCol + 1)
|
||||||
|
|
||||||
|
match if pos < s.Length then s.[pos] else 'n' with
|
||||||
|
| '-'
|
||||||
|
| 'J'
|
||||||
|
| '7' ->
|
||||||
|
Arr2D.set system (3 * startCol + 2) (3 * startLine + 1) 1uy
|
||||||
|
pos
|
||||||
|
| _ -> ofRowAndCol lineLength startLine (startCol - 1)
|
||||||
|
|
||||||
|
do
|
||||||
|
let struct (row, col) = toRowAndCol lineLength pointA
|
||||||
|
setAt system (3 * col + 1) (3 * row + 1) s.[pointA] 1uy
|
||||||
|
|
||||||
|
do
|
||||||
|
let struct (row, col) = toRowAndCol lineLength pointB
|
||||||
|
setAt system (3 * col + 1) (3 * row + 1) s.[pointB] 1uy
|
||||||
|
|
||||||
|
while pointA <> pointB do
|
||||||
|
let currentA = pointA
|
||||||
|
pointA <- nextPoint s lineLength pointA prevPointA
|
||||||
|
prevPointA <- currentA
|
||||||
|
|
||||||
|
do
|
||||||
|
let struct (row, col) = toRowAndCol lineLength pointA
|
||||||
|
setAt system (3 * col + 1) (3 * row + 1) s.[pointA] 1uy
|
||||||
|
|
||||||
|
let currentB = pointB
|
||||||
|
pointB <- nextPoint s lineLength pointB prevPointB
|
||||||
|
prevPointB <- currentB
|
||||||
|
|
||||||
|
do
|
||||||
|
let struct (row, col) = toRowAndCol lineLength pointB
|
||||||
|
setAt system (3 * col + 1) (3 * row + 1) s.[pointB] 1uy
|
||||||
|
|
||||||
|
let stackBuf = ResizeArray ()
|
||||||
|
|
||||||
|
for line = 0 to system.Height - 1 do
|
||||||
|
floodFill stackBuf system 0 line
|
||||||
|
floodFill stackBuf system (system.Width - 1) line
|
||||||
|
|
||||||
|
for col = 0 to system.Width - 1 do
|
||||||
|
floodFill stackBuf system col 0
|
||||||
|
floodFill stackBuf system col (system.Height - 1)
|
||||||
|
|
||||||
|
let mutable answer = 0
|
||||||
|
|
||||||
|
for row = 0 to lineCount - 1 do
|
||||||
|
for col = 0 to lineLength - 1 do
|
||||||
|
if Arr2D.get system (3 * col + 1) (3 * row + 1) = 0uy then
|
||||||
|
answer <- answer + 1
|
||||||
|
|
||||||
|
answer
|
95
AdventOfCode2023.FSharp/AdventOfCode2023.FSharp.Lib/Day11.fs
Normal file
95
AdventOfCode2023.FSharp/AdventOfCode2023.FSharp.Lib/Day11.fs
Normal file
@@ -0,0 +1,95 @@
|
|||||||
|
namespace AdventOfCode2023
|
||||||
|
|
||||||
|
open System
|
||||||
|
|
||||||
|
[<RequireQualifiedAccess>]
|
||||||
|
module Day11 =
|
||||||
|
|
||||||
|
type Data =
|
||||||
|
{
|
||||||
|
RowsWithoutGalaxies : ResizeArray<int>
|
||||||
|
ColsWithoutGalaxies : ResizeArray<int>
|
||||||
|
/// row * col
|
||||||
|
Galaxies : ResizeArray<int * int>
|
||||||
|
}
|
||||||
|
|
||||||
|
let parse (s : string) : Data =
|
||||||
|
let galaxies = ResizeArray ()
|
||||||
|
let rowsWithoutGalaxies = ResizeArray ()
|
||||||
|
let mutable hasAnyGalaxy = false
|
||||||
|
let mutable currRowIndex = 0
|
||||||
|
let mutable currColIndex = 0
|
||||||
|
|
||||||
|
for c in s do
|
||||||
|
if c = '\n' then
|
||||||
|
if not hasAnyGalaxy then
|
||||||
|
rowsWithoutGalaxies.Add currRowIndex
|
||||||
|
|
||||||
|
currRowIndex <- currRowIndex + 1
|
||||||
|
currColIndex <- 0
|
||||||
|
hasAnyGalaxy <- false
|
||||||
|
elif c = '#' then
|
||||||
|
hasAnyGalaxy <- true
|
||||||
|
galaxies.Add (currRowIndex, currColIndex)
|
||||||
|
currColIndex <- currColIndex + 1
|
||||||
|
else
|
||||||
|
currColIndex <- currColIndex + 1
|
||||||
|
|
||||||
|
galaxies.Sort (fun (_, c1) (_, c2) -> compare c1 c2)
|
||||||
|
|
||||||
|
let colsWithoutGalaxies =
|
||||||
|
let result = ResizeArray ()
|
||||||
|
let mutable prevCol = 0
|
||||||
|
|
||||||
|
for (_, c) in galaxies do
|
||||||
|
if c > prevCol then
|
||||||
|
for j = prevCol + 1 to c - 1 do
|
||||||
|
result.Add j
|
||||||
|
|
||||||
|
prevCol <- c
|
||||||
|
|
||||||
|
result
|
||||||
|
|
||||||
|
{
|
||||||
|
RowsWithoutGalaxies = rowsWithoutGalaxies
|
||||||
|
ColsWithoutGalaxies = colsWithoutGalaxies
|
||||||
|
Galaxies = galaxies
|
||||||
|
}
|
||||||
|
|
||||||
|
let solve (data : Data) (expansion : uint64) =
|
||||||
|
let mutable answer = 0uL
|
||||||
|
|
||||||
|
for galaxy1 = 0 to data.Galaxies.Count - 1 do
|
||||||
|
let row1, col1 = data.Galaxies.[galaxy1]
|
||||||
|
|
||||||
|
for galaxy2 = galaxy1 + 1 to data.Galaxies.Count - 1 do
|
||||||
|
let row2, col2 = data.Galaxies.[galaxy2]
|
||||||
|
let baseDistance = uint64 (abs (row1 - row2) + abs (col1 - col2))
|
||||||
|
|
||||||
|
let extraDistance =
|
||||||
|
let mutable extraDistance = 0uL
|
||||||
|
|
||||||
|
for i = 1 + min row1 row2 to max row1 row2 - 1 do
|
||||||
|
if data.RowsWithoutGalaxies.Contains i then
|
||||||
|
extraDistance <- extraDistance + expansion - 1uL
|
||||||
|
|
||||||
|
for i = 1 + min col1 col2 to max col1 col2 - 1 do
|
||||||
|
if data.ColsWithoutGalaxies.Contains i then
|
||||||
|
extraDistance <- extraDistance + expansion - 1uL
|
||||||
|
|
||||||
|
extraDistance
|
||||||
|
|
||||||
|
answer <- answer + extraDistance + baseDistance
|
||||||
|
|
||||||
|
answer
|
||||||
|
|
||||||
|
|
||||||
|
let part1 (s : string) =
|
||||||
|
let data = parse s
|
||||||
|
|
||||||
|
solve data 2uL
|
||||||
|
|
||||||
|
let part2 (s : string) =
|
||||||
|
let data = parse s
|
||||||
|
|
||||||
|
solve data 1_000_000uL
|
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,54 @@ 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")
|
||||||
|
|
||||||
|
Console.WriteLine "=====Day 10====="
|
||||||
|
|
||||||
|
do
|
||||||
|
let input = Path.Combine (dir.FullName, "day10.txt") |> File.ReadAllText
|
||||||
|
|
||||||
|
sw.Restart ()
|
||||||
|
let part1 = Day10.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 = Day10.part2 input
|
||||||
|
sw.Stop ()
|
||||||
|
Console.WriteLine (part2.ToString ())
|
||||||
|
Console.Error.WriteLine ((1_000.0 * float sw.ElapsedTicks / float Stopwatch.Frequency).ToString () + "ms")
|
||||||
|
|
||||||
|
Console.WriteLine "=====Day 11====="
|
||||||
|
|
||||||
|
do
|
||||||
|
let input = Path.Combine (dir.FullName, "day11.txt") |> File.ReadAllText
|
||||||
|
|
||||||
|
sw.Restart ()
|
||||||
|
let part1 = Day11.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 = Day11.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,9 @@
|
|||||||
<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="TestDay10.fs" />
|
||||||
|
<Compile Include="TestDay11.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 +30,10 @@
|
|||||||
<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"/>
|
||||||
|
<EmbeddedResource Include="samples\day10part1.txt" />
|
||||||
|
<EmbeddedResource Include="samples\day10.txt" />
|
||||||
|
<EmbeddedResource Include="samples\day11.txt" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
104
AdventOfCode2023.FSharp/Test/TestDay10.fs
Normal file
104
AdventOfCode2023.FSharp/Test/TestDay10.fs
Normal file
@@ -0,0 +1,104 @@
|
|||||||
|
namespace AdventOfCode2023.Test
|
||||||
|
|
||||||
|
open AdventOfCode2023
|
||||||
|
open NUnit.Framework
|
||||||
|
open FsUnitTyped
|
||||||
|
open System.IO
|
||||||
|
|
||||||
|
[<TestFixture>]
|
||||||
|
module TestDay10 =
|
||||||
|
|
||||||
|
let part1Sample1 () =
|
||||||
|
""".....
|
||||||
|
.S-7.
|
||||||
|
.|.|.
|
||||||
|
.L-J.
|
||||||
|
.....
|
||||||
|
"""
|
||||||
|
|> Day10.part1
|
||||||
|
|> shouldEqual 4
|
||||||
|
|
||||||
|
|
||||||
|
[<Test>]
|
||||||
|
let part1Sample () =
|
||||||
|
Assembly.getEmbeddedResource typeof<Dummy>.Assembly "day10part1.txt"
|
||||||
|
|> Day10.part1
|
||||||
|
|> shouldEqual 8
|
||||||
|
|
||||||
|
[<Test>]
|
||||||
|
let part2Sample1 () =
|
||||||
|
"""...........
|
||||||
|
.S-------7.
|
||||||
|
.|F-----7|.
|
||||||
|
.||.....||.
|
||||||
|
.||.....||.
|
||||||
|
.|L-7.F-J|.
|
||||||
|
.|..|.|..|.
|
||||||
|
.L--J.L--J.
|
||||||
|
...........
|
||||||
|
"""
|
||||||
|
|> Day10.part2
|
||||||
|
|> shouldEqual 4
|
||||||
|
|
||||||
|
[<Test>]
|
||||||
|
let part2Sample2 () =
|
||||||
|
"""..........
|
||||||
|
.S------7.
|
||||||
|
.|F----7|.
|
||||||
|
.||....||.
|
||||||
|
.||....||.
|
||||||
|
.|L-7F-J|.
|
||||||
|
.|..||..|.
|
||||||
|
.L--JL--J.
|
||||||
|
..........
|
||||||
|
"""
|
||||||
|
|> Day10.part2
|
||||||
|
|> shouldEqual 4
|
||||||
|
|
||||||
|
[<Test>]
|
||||||
|
let part2Sample3 () =
|
||||||
|
""".F----7F7F7F7F-7....
|
||||||
|
.|F--7||||||||FJ....
|
||||||
|
.||.FJ||||||||L7....
|
||||||
|
FJL7L7LJLJ||LJ.L-7..
|
||||||
|
L--J.L7...LJS7F-7L7.
|
||||||
|
....F-J..F7FJ|L7L7L7
|
||||||
|
....L7.F7||L7|.L7L7|
|
||||||
|
.....|FJLJ|FJ|F7|.LJ
|
||||||
|
....FJL-7.||.||||...
|
||||||
|
....L---J.LJ.LJLJ...
|
||||||
|
"""
|
||||||
|
|> Day10.part2
|
||||||
|
|> shouldEqual 8
|
||||||
|
|
||||||
|
[<Test>]
|
||||||
|
let part2Sample () =
|
||||||
|
Assembly.getEmbeddedResource typeof<Dummy>.Assembly "day10.txt"
|
||||||
|
|> Day10.part2
|
||||||
|
|> shouldEqual 10
|
||||||
|
|
||||||
|
[<Test>]
|
||||||
|
let part1Actual () =
|
||||||
|
let s =
|
||||||
|
try
|
||||||
|
File.ReadAllText (Path.Combine (__SOURCE_DIRECTORY__, "../../inputs/day10.txt"))
|
||||||
|
with
|
||||||
|
| :? DirectoryNotFoundException
|
||||||
|
| :? FileNotFoundException ->
|
||||||
|
Assert.Inconclusive ()
|
||||||
|
failwith "unreachable"
|
||||||
|
|
||||||
|
Day10.part1 s |> shouldEqual 6842
|
||||||
|
|
||||||
|
[<Test>]
|
||||||
|
let part2Actual () =
|
||||||
|
let s =
|
||||||
|
try
|
||||||
|
File.ReadAllText (Path.Combine (__SOURCE_DIRECTORY__, "../../inputs/day10.txt"))
|
||||||
|
with
|
||||||
|
| :? DirectoryNotFoundException
|
||||||
|
| :? FileNotFoundException ->
|
||||||
|
Assert.Inconclusive ()
|
||||||
|
failwith "unreachable"
|
||||||
|
|
||||||
|
Day10.part2 s |> shouldEqual 393
|
47
AdventOfCode2023.FSharp/Test/TestDay11.fs
Normal file
47
AdventOfCode2023.FSharp/Test/TestDay11.fs
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
namespace AdventOfCode2023.Test
|
||||||
|
|
||||||
|
open AdventOfCode2023
|
||||||
|
open NUnit.Framework
|
||||||
|
open FsUnitTyped
|
||||||
|
open System.IO
|
||||||
|
|
||||||
|
[<TestFixture>]
|
||||||
|
module TestDay11 =
|
||||||
|
|
||||||
|
let sample = Assembly.getEmbeddedResource typeof<Dummy>.Assembly "day11.txt"
|
||||||
|
|
||||||
|
[<Test>]
|
||||||
|
let part1Sample () =
|
||||||
|
sample |> Day11.part1 |> shouldEqual 374uL
|
||||||
|
|
||||||
|
[<Test>]
|
||||||
|
let part2Sample () =
|
||||||
|
let data = sample |> Day11.parse
|
||||||
|
Day11.solve data 10uL |> shouldEqual 1030uL
|
||||||
|
Day11.solve data 100uL |> shouldEqual 8410uL
|
||||||
|
|
||||||
|
[<Test>]
|
||||||
|
let part1Actual () =
|
||||||
|
let s =
|
||||||
|
try
|
||||||
|
File.ReadAllText (Path.Combine (__SOURCE_DIRECTORY__, "../../inputs/day11.txt"))
|
||||||
|
with
|
||||||
|
| :? DirectoryNotFoundException
|
||||||
|
| :? FileNotFoundException ->
|
||||||
|
Assert.Inconclusive ()
|
||||||
|
failwith "unreachable"
|
||||||
|
|
||||||
|
Day11.part1 s |> shouldEqual 9947476uL
|
||||||
|
|
||||||
|
[<Test>]
|
||||||
|
let part2Actual () =
|
||||||
|
let s =
|
||||||
|
try
|
||||||
|
File.ReadAllText (Path.Combine (__SOURCE_DIRECTORY__, "../../inputs/day11.txt"))
|
||||||
|
with
|
||||||
|
| :? DirectoryNotFoundException
|
||||||
|
| :? FileNotFoundException ->
|
||||||
|
Assert.Inconclusive ()
|
||||||
|
failwith "unreachable"
|
||||||
|
|
||||||
|
Day11.part2 s |> shouldEqual 519939907614uL
|
@@ -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
|
||||||
|
44
AdventOfCode2023.FSharp/Test/TestDay9.fs
Normal file
44
AdventOfCode2023.FSharp/Test/TestDay9.fs
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
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 () = sample |> 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
|
10
AdventOfCode2023.FSharp/Test/samples/day10.txt
Normal file
10
AdventOfCode2023.FSharp/Test/samples/day10.txt
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
FF7FSF7F7F7F7F7F---7
|
||||||
|
L|LJ||||||||||||F--J
|
||||||
|
FL-7LJLJ||||||LJL-77
|
||||||
|
F--JF--7||LJLJ7F7FJ-
|
||||||
|
L---JF-JLJ.||-FJLJJ7
|
||||||
|
|F|F-JF---7F7-L7L|7|
|
||||||
|
|FFJF7L7F-JF7|JL---7
|
||||||
|
7-L-JL7||F7|L7F-7F7|
|
||||||
|
L.L7LFJ|||||FJL7||LJ
|
||||||
|
L7JLJL-JLJLJL--JLJ.L
|
5
AdventOfCode2023.FSharp/Test/samples/day10part1.txt
Normal file
5
AdventOfCode2023.FSharp/Test/samples/day10part1.txt
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
..F7.
|
||||||
|
.FJ|.
|
||||||
|
SJ.L7
|
||||||
|
|F--J
|
||||||
|
LJ...
|
10
AdventOfCode2023.FSharp/Test/samples/day11.txt
Normal file
10
AdventOfCode2023.FSharp/Test/samples/day11.txt
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
...#......
|
||||||
|
.......#..
|
||||||
|
#.........
|
||||||
|
..........
|
||||||
|
......#...
|
||||||
|
.#........
|
||||||
|
.........#
|
||||||
|
..........
|
||||||
|
.......#..
|
||||||
|
#...#.....
|
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