-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday10_part01.fs
91 lines (76 loc) · 3.22 KB
/
day10_part01.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
module day10_part01
open AdventOfCode_2023.Modules
type Coord = {
row: int
col: int
}
let buildMap (input: string list) =
let array = Array2D.create (input.Length) (input.[0].Length) "."
let startPoint = Array.create 1 { row = 0; col = 0 }
for rowIdx in 0..input.Length - 1 do
for colIdx in 0..input.[0].Length - 1 do
let value = input.[rowIdx].[colIdx].ToString()
array[rowIdx, colIdx] <- value
if value = "S" then
startPoint.[0] <- { row = rowIdx; col = colIdx }
(startPoint.[0], array)
let getValidDirections (position: Coord) (map: string[,]) =
let directions = [[|-1; 0|]; [|1; 0|]; [|0; -1|]; [|0; 1|]]
let validDirections = directions |> List.filter (fun direction ->
let row = position.row + direction.[0]
let col = position.col + direction.[1]
row >= 0 && row < map.GetLength(0) && col >= 0 && col < map.GetLength(1) && map.[row, col] <> "."
)
validDirections
let getStartDirections (position: Coord) (map: string[,]) =
let availableDirections = getValidDirections position map
let startDirections = seq {
for d in availableDirections do
let posToCheck = { row = position.row + d.[0]; col = position.col + d.[1] }
let value = map.[posToCheck.row, posToCheck.col]
match d, value with
| [|-1; 0|], p when ["|"; "7"; "F"] |> List.contains p -> yield d
| [|0; -1|], p when ["-"; "L"; "F"] |> List.contains p -> yield d
| [|0; 1|], p when ["-"; "J"; "7"] |> List.contains p -> yield d
| [|1; 0|], p when ["|"; "J"; "L"] |> List.contains p -> yield d
| _ -> ()
}
startDirections |> List.ofSeq
let getConnections (position: Coord) (map: string[,])=
let value = map.[position.row, position.col]
let directions =
match value with
| "|" -> [[|-1; 0|]; [|1; 0|]]
| "-" -> [[|0; -1|]; [|0; 1|]]
| "L" -> [[|-1; 0|]; [|0; 1|]]
| "J" -> [[|-1; 0|]; [|0; -1|]]
| "7" -> [[|1; 0|]; [|0; -1|]]
| "F" -> [[|1; 0|]; [|0; 1|]]
| "S" -> getStartDirections position map
| _ -> []
let connections = seq {
for d in directions do
let posToCheck = { row = position.row + d.[0]; col = position.col + d.[1] }
yield { row = posToCheck.row; col = posToCheck.col }
}
connections |> Set.ofSeq
let printMap (map: string[,]) =
for row in 0..map.GetLength(0) - 1 do
for col in 0..map.GetLength(1) - 1 do
printf "%s" map.[row, col]
printfn ""
let rec travelMap (startPosition: Coord) (map: string[,]) (visited: Coord Set) =
let possiblePositions = getConnections startPosition map
let nextPosition = Set.difference possiblePositions visited
if nextPosition.IsEmpty then visited
else
let point = nextPosition.MinimumElement
let visited' = Set.add point visited
travelMap point map visited'
let execute =
let path = "day10/day10_input.txt"
let input = LocalHelper.ReadLines path |> Seq.toList
let (startPoint, map) = buildMap input
let visited = Set.add(startPoint) Set.empty
let pipePath = travelMap startPoint map visited
pipePath.Count / 2