-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day12.fs
101 lines (80 loc) · 3.04 KB
/
Day12.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
92
93
94
95
96
97
98
99
100
101
namespace Adventofcode2021
module Day12 =
[<Literal>]
let InputFile = "Day12Input.txt"
type Connection = { Cave1: string; Cave2: string }
let parse (s: string) =
let parts = s.Split('-')
{ Cave1 = parts.[0]; Cave2 = parts.[1] }
let isSmallCave (s: string) =
s.ToCharArray()
|> Array.forall System.Char.IsLower
let getNextPaths (connections: Connection []) path =
let pos = List.last path
let visitedSmallCaves = path |> List.filter isSmallCave
let neighbours =
connections
|> Array.filter (fun c -> c.Cave1 = pos || c.Cave2 = pos)
|> Array.map (fun c ->
if c.Cave1 = pos then
c.Cave2
else
c.Cave1)
|> Array.filter (fun c ->
match c with
| _ when isSmallCave c -> visitedSmallCaves |> List.contains c |> not
| _ -> true)
neighbours
|> Array.map (fun n -> List.append path [ n ])
let bfs getNextPathsF (connections: Connection []) =
let mutable paths = Set.empty
let queue = System.Collections.Generic.Queue<string list>()
getNextPathsF connections [ "start" ]
|> Array.iter (fun p -> queue.Enqueue p)
while (queue.Count <> 0) do
let currentPath = queue.Dequeue()
if (List.last currentPath) = "end" then
paths <- Set.add currentPath paths
else
getNextPathsF connections currentPath
|> Array.iter (fun p -> queue.Enqueue p)
paths.Count
let day12 () =
InputFile
|> System.IO.File.ReadAllLines
|> Array.map parse
|> bfs getNextPaths
let getNextPathsPart2 (connections: Connection []) path =
let pos = List.last path
let visitedSmallCaves = path |> List.filter isSmallCave
let smallCaveVisits = visitedSmallCaves |> List.countBy id |> Map.ofList
let smallCaveVisitedTwice =
visitedSmallCaves
|> Set.ofList
|> fun s -> s.Count < visitedSmallCaves.Length
let neighbours =
connections
|> Array.filter (fun c -> c.Cave1 = pos || c.Cave2 = pos)
|> Array.map (fun c ->
if c.Cave1 = pos then
c.Cave2
else
c.Cave1)
|> Array.filter (fun c ->
match c with
| "start" -> false
| _ when isSmallCave c ->
let alreadyVisited = visitedSmallCaves |> List.contains c
if alreadyVisited then
smallCaveVisits.[c] < 2
&& not smallCaveVisitedTwice
else
true
| _ -> true)
neighbours
|> Array.map (fun n -> List.append path [ n ])
let day12Part2 () =
InputFile
|> System.IO.File.ReadAllLines
|> Array.map parse
|> bfs getNextPathsPart2