-
Notifications
You must be signed in to change notification settings - Fork 0
/
day18_part01.fs
87 lines (69 loc) · 2.6 KB
/
day18_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
module day18_part01
open AdventOfCode_2024.Modules
open System.Collections.Generic
type KindType =
| Empty
| Corrupted
type Coord = {
X: int
Y: int
Kind : KindType
}
let parseContent(lines: string array) (size: int) =
let map = Array2D.init (size+1) (size+1) (fun row col -> { X = col; Y = row; Kind = Empty })
let corrupted =
lines
|> Array.map(fun l ->
{ X = (int)(l.Split(",")[0]); Y = (int)(l.Split(",")[1]); Kind = Corrupted }
)
(map, corrupted)
let printMap(graph: Coord[,]) =
let maxRows = graph.GetLength(0)
let maxCols = graph.GetLength(1)
for row in 0..(maxRows-1) do
for col in 0..(maxCols-1) do
printf "%s" (if graph[row,col].Kind.IsCorrupted then "#" else ".")
printfn ""
let findShortestPath (graph: Coord[,]) (start: Coord) (goal: Coord) =
let maxRows = graph.GetLength(0)
let maxCols = graph.GetLength(1)
let isInBoundaries (row: int) (col: int) =
row >= 0 && col >= 0 && row < maxRows && col < maxCols
let directions = [ (-1, 0); (1, 0); (0, 1); (0, -1) ]
let queue = Queue<Coord * int>()
let visited = HashSet<Coord>()
queue.Enqueue((start, 0))
let rec bfs () =
if queue.Count = 0 then None
else
let (current, path) = queue.Dequeue()
if current = goal then
Some(path)
else
if not (visited.Contains(current)) && current.Kind.IsEmpty then
let _ = visited.Add(current)
for (dRow, dCol) in directions do
let nextRow = current.Y + dRow
let nextCol = current.X + dCol
if isInBoundaries nextRow nextCol then
let neighbor = graph[nextRow, nextCol]
if not (visited.Contains(neighbor)) then
queue.Enqueue((neighbor, path+1))
bfs ()
bfs ()
let buildCorruptedMap(map: Coord[,])(corrupted: Coord[])(numOfBytes: int) =
corrupted
|> Array.take(numOfBytes)
|> Array.iter(fun c ->
map[c.Y, c.X] <- { map[c.X, c.Y] with Kind = Corrupted }
)
let execute() =
let path = "day18/day18_input.txt"
let (size, numOfBytes) = (70, 1024)
let content = LocalHelper.GetLinesFromFile path
let (map, corrupted) = parseContent content size
let start = { Y = 0; X = 0; Kind = Empty }
let endnode = { Y = size; X = size; Kind = Empty }
buildCorruptedMap map corrupted numOfBytes
let path = findShortestPath map start endnode
path.Value