-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.fsx
More file actions
209 lines (170 loc) · 6.61 KB
/
utils.fsx
File metadata and controls
209 lines (170 loc) · 6.61 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
open System.IO
open System.Net
open System.Net.Http
open System
open System.Text.RegularExpressions
let private year = Directory.GetCurrentDirectory() |> Path.GetFileName
let private get (path: string) =
let token = File.ReadAllText(Path.Combine(__SOURCE_DIRECTORY__, "token"))
let cookies = new CookieContainer()
cookies.Add(new Cookie("session", token, "/", "adventofcode.com"))
let handler = new HttpClientHandler()
handler.CookieContainer <- cookies
let client = new HttpClient(handler)
client.BaseAddress <- new Uri "https://adventofcode.com"
let result = client.GetStringAsync(path).GetAwaiter().GetResult()
result.Trim()
let GetData (day: int) =
let directory = Path.Combine(__SOURCE_DIRECTORY__, string year, "Inputs")
let file: string = Path.Combine(directory, $"{day}.txt")
if File.Exists file then
File.ReadAllText file
else
let str = get $"/{year}/day/{day}/input"
Directory.CreateDirectory directory |> ignore
File.WriteAllText(file, str)
str
let GetExample (day: int) (id: int) =
let directory = Path.Combine(__SOURCE_DIRECTORY__, string year, "Examples")
let file = Path.Combine(directory, $"{day}-{id}.txt")
if File.Exists file then
File.ReadAllText file
else
let html = get $"/{year}/day/{day}"
let pattern = @"<pre><code>(.*?)</code></pre>"
let matches = Regex.Matches(html, pattern, RegexOptions.Singleline)
let exampleData =
matches
|> Seq.map (fun m -> m.Groups[1].Value)
|> Seq.toArray
|> fun a -> Array.get a (id - 1)
let decodedExampleData = WebUtility.HtmlDecode exampleData
Directory.CreateDirectory directory |> ignore
File.WriteAllText(file, decodedExampleData.Trim())
decodedExampleData
/// Split a string by a character, removing empty entries
let Split (character: string) (input: string) =
input.Split(character, StringSplitOptions.RemoveEmptyEntries ||| StringSplitOptions.TrimEntries)
/// Split input into lines, removing empty entries
let GetLines (input: string) =
input.Trim().Split([| "\n"; "\r\n" |], StringSplitOptions.RemoveEmptyEntries ||| StringSplitOptions.TrimEntries)
/// Split input into blocks separated by double newlines
let GetBlocks (input: string) =
input
.Trim()
.Split([| "\n\n"; "\r\n\r\n" |], StringSplitOptions.RemoveEmptyEntries ||| StringSplitOptions.TrimEntries)
/// Convert string to lowercase
let ToLower (input: string) = input.ToLower()
/// Convert string to uppercase
let ToUpper (input: string) = input.ToUpper()
/// Convert character to integer (0-9)
let charToInt (c: char) = int c - int '0'
let charToInt64 (c: char) = int64 c - int64 '0'
/// Active pattern for regex matching that returns capture groups
let (|Regex|_|) pattern input =
let m = Regex.Match(input, pattern)
if m.Success then
Some(List.tail [ for g in m.Groups -> g.Value ])
else
None
/// Active pattern for regex matching that returns capture groups and the match object
let (|RegexEx|_|) pattern input =
let m = Regex.Match(input, pattern)
if m.Success then
Some(List.tail [ for g in m.Groups -> g.Value ], m)
else
None
/// Active pattern for parsing integers
let (|Int|_|) (str: string) =
match Int32.TryParse str with
| true, value -> Some value
| _ -> None
/// Active pattern for parsing unsigned integers
let (|UInt|_|) (str: string) =
match UInt32.TryParse str with
| true, value -> Some value
| _ -> None
/// Active pattern for parsing long integers
let (|Long|_|) (str: string) =
match Int64.TryParse str with
| true, value -> Some value
| _ -> None
/// Active pattern for parsing unsigned long integers
let (|ULong|_|) (str: string) =
match UInt64.TryParse str with
| true, value -> Some value
| _ -> None
/// Active pattern for splitting string by spaces and converting to int array
let (|IntArray|) list = list |> Split " " |> Array.map int
/// Active pattern for splitting string by spaces and converting to long array
let (|LongArray|) list = list |> Split " " |> Array.map int64
/// Active pattern for splitting string by specified character into array
let (|SplitArray|) character list = list |> Split character
/// Active pattern for splitting string by specified character into list
let (|SplitList|) character list = list |> Split character |> List.ofArray
/// Replace text in a string
let Replace (string: string) (oldValue: string) (newValue: string) = string.Replace(oldValue, newValue)
/// Replace text using regex pattern
let RegexReplace pattern (replacement: string) input =
let regex = new Regex(pattern)
regex.Replace(input, replacement)
/// Get first regex match groups as list
let Match pattern input =
let m = Regex.Match(input, pattern)
if m.Success then
List.tail [ for g in m.Groups -> g.Value ]
else
[]
/// Get all regex matches as flattened list of capture groups
let Matches pattern input =
let m = Regex.Matches(input, pattern)
m
|> Seq.collect (fun x ->
x.Groups
|> Seq.collect (fun y -> y.Captures |> Seq.map (fun z -> z.Value))
|> Seq.skip 1
|> Seq.toList)
|> Seq.toList
/// Convert a string into a 2D array of ((x, y), char)
let ToGrid (input: string) =
input
|> GetLines
|> Array.mapi (fun y line ->
line
|> Seq.mapi (fun x c -> ((x, y), c))
|> Seq.toArray)
|> Array.concat
let toArray2D (input: string) =
let lines = GetLines input
let height = lines.Length
let width = lines.[0].Length
Array2D.init height width (fun y x -> lines.[y].[x])
let allPermutations list =
let rec permutations (input: 'a list) =
seq {
match input with
| [] -> yield [] // Base case: the only permutation of an empty list is an empty list
| head :: tail ->
// For each permutation of the tail
for tailPerm in permutations tail do
// Insert the head into every possible position of the tail permutation
for i in 0..List.length tailPerm do
yield List.take i tailPerm @ [head] @ List.skip i tailPerm
}
list
|> Seq.toList
|> permutations
let allCombinations list =
let rec combinations remaining =
seq {
match remaining with
| [] -> ()
| head :: tail ->
yield [head]
yield! combinations tail
for combo in combinations tail do
yield head :: combo
}
list
|> Seq.toList
|> combinations