Day 10 part 1

This commit is contained in:
Smaug123
2023-12-10 11:24:27 +00:00
parent 3b98d704d1
commit 6134ce3f82
7 changed files with 270 additions and 0 deletions

View File

@@ -20,6 +20,7 @@
<Compile Include="Day7.fs"/>
<Compile Include="Day8.fs"/>
<Compile Include="Day9.fs"/>
<Compile Include="Day10.fs" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,133 @@
namespace AdventOfCode2023
#if DEBUG
#else
#nowarn "9"
#endif
open System
[<RequireQualifiedAccess>]
module Day10 =
type Pipe =
| Empty = '.'
| NorthSouth = '|'
| EastWest = '-'
| NorthEast = 'L'
| NorthWest = '7'
| SouthEast = 'F'
| SouthWest = '7'
| Animal = 'S'
/// Returns first the line number, then the position within that line.
/// lineLength includes the newline, as does pos.
let inline toRowAndCol (lineLength : int) (lineCount : int) (pos : int) : struct (int * int) =
let lineNum = pos / lineLength
let withinLine = pos % lineLength
struct (lineNum, withinLine)
let inline ofRowAndCol (lineLength : int) (lineCount : int) (lineNum : int) (col : int) : int =
lineNum * lineLength + col
let inline nextPoint (s : ReadOnlySpan<char>) (lineLength : int) (lineCount : int) (currPos : int) (prevPos : int) =
let struct (currLineNum, currCol) = toRowAndCol lineLength lineCount currPos
let struct (prevLineNum, prevCol) = toRowAndCol lineLength lineCount prevPos
match s.[currPos] with
| '|' ->
if prevLineNum < currLineNum then
ofRowAndCol lineLength lineCount (currLineNum + 1) currCol
else
ofRowAndCol lineLength lineCount (currLineNum - 1) currCol
| '-' ->
if prevCol < currCol then
ofRowAndCol lineLength lineCount currLineNum (currCol + 1)
else
ofRowAndCol lineLength lineCount currLineNum (currCol - 1)
| 'L' ->
if prevLineNum = currLineNum then
ofRowAndCol lineLength lineCount (currLineNum - 1) currCol
else
ofRowAndCol lineLength lineCount currLineNum (currCol + 1)
| '7' ->
if prevLineNum = currLineNum then
ofRowAndCol lineLength lineCount (currLineNum + 1) currCol
else
ofRowAndCol lineLength lineCount currLineNum (currCol - 1)
| 'F' ->
if prevLineNum = currLineNum then
ofRowAndCol lineLength lineCount (currLineNum + 1) currCol
else
ofRowAndCol lineLength lineCount currLineNum (currCol + 1)
| 'J' ->
if prevLineNum = currLineNum then
ofRowAndCol lineLength lineCount (currLineNum - 1) currCol
else
ofRowAndCol lineLength lineCount currLineNum (currCol - 1)
| c -> failwithf "unrecognised: %c" c
let part1 (s : string) =
let s = s.AsSpan()
let lineCount = s.Count '\n'
let lineLength = (s.IndexOf '\n' + 1)
let startPos = s.IndexOf 'S'
let struct (startLine, startCol) = toRowAndCol lineLength lineCount startPos
let mutable distance = 1
let mutable prevPointA = startPos
let mutable prevPointB = startPos
let mutable pointA =
let pos = ofRowAndCol lineLength lineCount startLine (startCol - 1)
match s.[pos] with
| '-'
| 'L'
| 'F' -> pos
| _ ->
let pos = ofRowAndCol lineLength lineCount startLine (startCol + 1)
match s.[pos] with
| '-'
| 'J'
| '7' -> pos
| _ ->
ofRowAndCol lineLength lineCount (startLine + 1) startCol
let mutable pointB =
let pos = ofRowAndCol lineLength lineCount (startLine - 1) startCol
match s.[pos] with
| '|'
| '7'
| 'F' -> pos
| _ ->
let pos = ofRowAndCol lineLength lineCount (startLine + 1) startCol
match s.[pos] with
| '|'
| 'L'
| 'J' -> pos
| _ ->
let pos = ofRowAndCol lineLength lineCount startLine (startCol + 1)
match s.[pos] with
| '-'
| 'J'
| '7' -> pos
| _ -> ofRowAndCol lineLength lineCount startLine (startCol - 1)
while pointA <> pointB do
let currentA = pointA
pointA <- nextPoint s lineLength lineCount pointA prevPointA
prevPointA <- currentA
let currentB = pointB
pointB <- nextPoint s lineLength lineCount pointB prevPointB
prevPointB <- currentB
distance <- distance + 1
distance
let part2 (s : string) =
0