Day 10 part 1
This commit is contained in:
133
AdventOfCode2023.FSharp/AdventOfCode2023.FSharp.Lib/Day10.fs
Normal file
133
AdventOfCode2023.FSharp/AdventOfCode2023.FSharp.Lib/Day10.fs
Normal 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
|
Reference in New Issue
Block a user