-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay16.cls
213 lines (186 loc) · 4.9 KB
/
Day16.cls
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
213
Class MC2022AOC.Day16
{
ClassMethod runGame(intLive As %Integer = 1, intVerb As %Integer = 0) As %Integer
{
I intLive=1
{ d ..initLive() }
ELSE
{ d ..initTest() }
S (intCount)=0
S aValve=0
s aFlow=""
s iTimeAll=30
s iTimeRemaining=30
S aState=""
s astrAction=""
// init the control array
for intRow=1:1:^AOCD16 {
s Data=$LFS(^AOCD16(intRow),",")
S aValve($LI(Data,1))=$LI(Data,2,*)
S aValve=$I(aValve)
I $LI(Data,2)>0
{
S aFlow($LI(Data,2),$LI(Data,1))=$LI(Data,3,*)
S aState($LI(Data,1))="C"
} ELSE {
// no point opening it so ignore it by setting already open
S aState($LI(Data,1))="O"
}
}
zw aValve w !
s sStart="AA"
s intLoopCheck=0
Loop1
s intLoopCheck=intLoopCheck+1
I intLoopCheck>10 G xit
k aTree
s aTree=""
d ..getTree(.aValve,.aTree,sStart)
zw aTree w !
k aMaxFlow
s aMaxFlow=""
d ..calcFlow(.aValve,.aTree,sStart,iTimeRemaining, .aMaxFlow)
zw aMaxFlow w !
S mF=""
L1 S mF=$O(aMaxFlow(mF),-1) G:mF="" L1x
s nV=$O(aMaxFlow(mF,"")) G:nV="" L1x
I aState(nV)="C" {
s iTimeRemaining=iTimeRemaining-aTree(sStart,nV)-1
I iTimeRemaining<0 G L1x // Out of time.
s aState(nV)="O"
s astrAction(iTimeAll-iTimeRemaining)="Open "_nV_ " flow is "_iTimeRemaining_"m @ "_($LI(aValve(nV),1))_" = "_(iTimeRemaining*($LI(aValve(nV),1)))
s astrAction=astrAction+(iTimeRemaining*($LI(aValve(nV),1)))
G L1x
}
G L1
L1x
I nV'="" s sStart=nV
w "Next Valve "_nV,!
G Loop1
xit
zw astrAction w !
Q
}
ClassMethod calcFlow(ByRef aGrid, ByRef aTree, strSource, iTimeLeft, ByRef aMaxFlow)
{
s maxFlow=iTimeLeft
s aN=""
L1 s aN=$O(aTree(strSource,aN)) G:aN="" L1x
s maxFlow=(iTimeLeft-aTree(strSource,aN)-1)*$LI(aGrid(aN),1)
s:maxFlow>0 aMaxFlow(maxFlow,aN)=aN
G L1
L1x
}
ClassMethod getTree(ByRef aGrid, ByRef aTree, strSource)
{
S aVisited(strSource)=1
s aUnvisited=""
s aNode=""
L1 s aNode=$O(aGrid(aNode)) G:aNode="" L1x
I aNode=strSource {
// Don't need to calculate distance to itself
S aTree(strSource,aNode)=0
s aVisited(strSource)=1
} else {
S aTree(strSource,aNode)=999999
s aUnvisited(aNode)=1
}
G L1
L1x
s curNode=strSource
s legLen=1
L2
f i=2:1:$LL(aGrid(curNode))
{
s nextHop=$LI(aGrid(curNode),i)
S curDist=aTree(strSource,curNode)
// 1 is static value of travel between nodes
I $G(aTree(strSource,nextHop),999999) > (curDist+legLen) S aTree(strSource,nextHop)=curDist+1
I $D(aVisited(nextHop))=0 s aQueue((curDist+1)*-1,nextHop)=nextHop
}
s aVisited(curNode)=1
k aUnvisited(curNode)
s curNode=..getNext(.aGrid,.aTree,.aUnvisited,strSource) G:curNode="" L2x
G L2
L2x
Q
}
ClassMethod getNext(ByRef aGrid, ByRef aTree, ByRef aUnvisited, strSource) As %String
{
s aN=""
L1 s aN=$o(aUnvisited(aN)) G:aN="" L1x
// this will overwrite if there are duplicate lengths but it doesn't matter
s aSort(aTree(strSource,aN))=aN
g L1
L1x
s retVal=""
S iDist=$O(aSort(""),1,retVal)
q retVal
}
ClassMethod initTest()
{
k ^AOCD16
S ^AOCD16=10
S ^AOCD16(1)="AA,0,DD,II,BB"
S ^AOCD16(2)="BB,13,CC,AA"
S ^AOCD16(3)="CC,2,DD,BB"
S ^AOCD16(4)="DD,20,CC,AA,EE"
S ^AOCD16(5)="EE,3,FF,DD"
S ^AOCD16(6)="FF,0,EE,GG"
S ^AOCD16(7)="GG,0,FF,HH"
S ^AOCD16(8)="HH,22,GG"
S ^AOCD16(9)="II,0,AA,JJ"
S ^AOCD16(10)="JJ,21,II"
}
ClassMethod initLive()
{
k ^AOCD16
S ^AOCD16=46
S ^AOCD16(1)="OJ,0,EW,IG"
S ^AOCD16(2)="BN,0,SA,AA"
S ^AOCD16(3)="SA,5,QK,LP,ZP,BN,VH"
S ^AOCD16(4)="RL,21,AM"
S ^AOCD16(5)="LR,19,XZ"
S ^AOCD16(6)="VQ,0,OW,IG"
S ^AOCD16(7)="ZK,0,EW,WC"
S ^AOCD16(8)="IG,16,OJ,VQ"
S ^AOCD16(9)="WC,22,VD,ZK"
S ^AOCD16(10)="EW,18,OJ,ZK"
S ^AOCD16(11)="FP,8,GB"
S ^AOCD16(12)="JF,23,VD"
S ^AOCD16(13)="BL,0,AA,ZD"
S ^AOCD16(14)="BZ,0,QK,JA"
S ^AOCD16(15)="KH,0,SJ,FC"
S ^AOCD16(16)="FU,0,FC,MH"
S ^AOCD16(17)="ZP,0,SA,FC"
S ^AOCD16(18)="DZ,0,AA,MH"
S ^AOCD16(19)="RI,0,LP,MH"
S ^AOCD16(20)="AE,0,FC,AA"
S ^AOCD16(21)="JA,4,MM,BZ,JR,ZI,QO"
S ^AOCD16(22)="XP,0,ZD,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