-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay18.fs
212 lines (173 loc) · 5.61 KB
/
Day18.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
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
210
211
212
namespace Adventofcode2021
module Day18 =
open System
open System.Collections.Generic
[<Literal>]
let InputFile = "Day18Input.txt"
let getNextPair (s: string) =
s.ToCharArray()
|> Array.takeWhile (fun c -> c <> ']')
|> String
|> fun s -> s + "]"
let (|OpenBrack|CloseBrack|Digit|Comma|Empty|) (s: string) =
match s with
| "" -> Empty
| _ when s.[0] = '[' -> OpenBrack
| _ when s.[0] = ']' -> CloseBrack
| _ when Char.IsDigit s.[0] -> Digit
| _ when s.[0] = ',' -> Comma
| _ -> failwith $"unsupported token {s}"
let getPair (n: string) =
let openStack = Stack<string>()
let closeStack = Stack<string>()
let rec helper idx =
match n.[idx..] with
| Empty -> failwith "no pair found"
| OpenBrack ->
openStack.Push("[")
helper (idx + 1)
| CloseBrack ->
closeStack.Push("]") |> ignore
helper (idx + 1)
| Digit -> helper (idx + 1)
| Comma ->
if openStack.Count = closeStack.Count + 1 then
let left = n.[1 .. idx - 1]
let right = n.[idx + 1 .. n.Length - 2]
(left, right)
else
helper (idx + 1)
helper 0
let getLeftRight (pair: string) =
pair.Trim('[', ']')
|> fun s -> s.Split(',')
|> fun a -> int a.[0], int a.[1]
let findPairToExplode (stack: Stack<string>) (n: string) =
let rec helper idx =
match n.[idx..] with
| Empty -> None
| OpenBrack ->
stack.Push("[")
if stack.Count = 5 then
Some idx
else
helper (idx + 1)
| CloseBrack ->
stack.Pop() |> ignore
helper (idx + 1)
| Digit
| Comma -> helper (idx + 1)
helper 0
let digits =
[| '0'
'1'
'2'
'3'
'4'
'5'
'6'
'7'
'8'
'9' |]
let addToFirstLeft (n: string) toAdd =
let index = n.LastIndexOfAny(digits)
if index >= 0 then
let (index', length) =
if Char.IsDigit(n.[index - 1]) then
index - 1, 2
else
index, 1
let left = n.Substring(index', length) |> int
let sum = left + toAdd
n.Substring(0, index')
+ sum.ToString()
+ n.Substring(index + 1)
else
n
let addToFirstRight (n: string) toAdd =
let index = n.IndexOfAny(digits)
if index >= 0 then
let length =
if Char.IsDigit(n.[index + 1]) then
2
else
1
let right = n.Substring(index, length) |> int
let sum = right + toAdd
n.Substring(0, index)
+ sum.ToString()
+ n.Substring(index + length)
else
n
let explode (n: string) (idx: int) =
let toExp = getNextPair n.[idx..]
let left, right = getLeftRight toExp
let leftPart = addToFirstLeft (n.Substring(0, idx)) left
let rightPart = addToFirstRight (n.Substring(idx + toExp.Length)) right
let nAfterReplace = leftPart + "0" + rightPart
nAfterReplace
let tryExplode (n: string) =
n
|> findPairToExplode (Stack<string>())
|> function
| Some idx -> explode n idx
| None -> n
let split n =
let left = n / 2
let right = float n / 2. |> ceil |> int
$"[{left},{right}]"
let trySplit (n: string) =
n.ToCharArray()
|> Array.windowed 2
|> Array.tryFindIndex (fun w ->
match w with
| [| x; y |] -> Char.IsDigit x && Char.IsDigit y
| _ -> false)
|> function
| Some index ->
let prefix = n.Substring(0, index)
let splited = split (n.Substring(index, 2) |> int)
let postfix = n.Substring(index + 2)
prefix + splited + postfix
| None -> n
let rec reduce (n: string) =
let afterExp = tryExplode n
if afterExp <> n then
reduce afterExp
else
let afterSplit = trySplit n
if afterSplit <> n then
reduce afterSplit
else
n
let add n1 n2 = $"[{n1},{n2}]" |> reduce
let rec magnitude (n: string) =
let left, right = getPair n
let magLeft =
if Char.IsDigit left[0] then
3 * int (left[ 0 ].ToString())
else
3 * magnitude left
let magRight =
if Char.IsDigit right[0] then
2 * int (right[ 0 ].ToString())
else
2 * magnitude right
magLeft + magRight
let day18 () =
InputFile
|> System.IO.File.ReadAllLines
|> Array.reduce add
|> magnitude
let day18Part2 () =
let numbers = System.IO.File.ReadAllLines InputFile
let indexes = [ 0 .. numbers.Length - 1 ]
seq {
for n in indexes do
let rest = indexes |> List.filter (fun i -> i <> n)
for r in rest do
(numbers[n], numbers[r])
(numbers[r], numbers[n])
}
|> Seq.map (fun (a, b) -> ((add a b) |> magnitude))
|> Seq.max