-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay07.roc
185 lines (148 loc) · 5.72 KB
/
Day07.roc
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
interface Day07 exposes [ output ] imports [ TestUtil ]
output : List I64 -> List (List I64)
output = \puzzleInput ->
testMatrix1 = buildMatrix 10 testInput1
testMatrix2 = buildMatrix 8 testInput2
puzzleMatrix = buildMatrix 595 puzzleInput
[ TestUtil.verify 7 1 1 (outerBags 8 10 testMatrix1 ) 4
, TestUtil.show 7 1 (outerBags 474 595 puzzleMatrix)
, TestUtil.verify 7 2 1 (innerBags 8 10 testMatrix1 ) 32
, TestUtil.verify 7 2 2 (innerBags 1 8 testMatrix2 ) 126
, TestUtil.show 7 2 (innerBags 474 595 puzzleMatrix)
]
# count outer bags
outerBags : I64, I64, List I64 -> I64
outerBags = \num, len, matrix ->
counted = List.repeat len 0
outerBagsHelper len matrix counted [ num ] num 0 [] 1
outerBagsHelper : I64, List I64, List I64, List I64, I64, I64, List I64, I64 -> I64
outerBagsHelper = \len, matrix, counted, current, curVal, curIdx, next, parent ->
if parent < len then
newParent = parent + 1
idx = index len curVal parent
when List.get matrix idx is
Ok count ->
if count > 0 then
when List.get counted parent is
Ok 0 ->
newCounted = List.set counted parent 1
newNext = List.append next parent
outerBagsHelper len matrix newCounted current curVal curIdx newNext newParent
_ ->
outerBagsHelper len matrix counted current curVal curIdx next newParent
else
outerBagsHelper len matrix counted current curVal curIdx next newParent
_ ->
outerBagsHelper len matrix counted current curVal curIdx next newParent
else
newCurIdx = curIdx + 1
when List.get current newCurIdx is
Ok newCurVal ->
outerBagsHelper len matrix counted current newCurVal newCurIdx next 1
_ ->
when List.get next 0 is
Ok newCurVal ->
outerBagsHelper len matrix counted next newCurVal 0 [] 1
_ ->
List.sum counted
# count inner bags
innerBags : I64, I64, List I64 -> I64
innerBags = \num, len, matrix ->
current = List.repeat len 0
next = List.repeat len 0
innerBagsHelper len matrix 0 current 1 num next 1
innerBagsHelper : I64, List I64, I64, List I64, I64, I64, List I64, I64 -> I64
innerBagsHelper = \len, matrix, total, current, curVal, curIdx, next, child ->
if child < len then
newChild = child + 1
idx = index len child curIdx
when List.get matrix idx is
Ok count ->
childCount = count * curVal
newTotal = total + childCount
newNext = addCount next child childCount
innerBagsHelper len matrix newTotal current curVal curIdx newNext newChild
_ ->
innerBagsHelper len matrix total current curVal curIdx next newChild
else
newCurIdx = curIdx + 1
when List.get current newCurIdx is
Ok newCurVal ->
if newCurVal > 0 then
innerBagsHelper len matrix total current newCurVal newCurIdx next 1
else
innerBagsHelper len matrix total current newCurVal newCurIdx next child
_ ->
if List.sum next > 0 then
when List.get next 1 is
Ok newCurVal ->
newNext = List.repeat len 0
innerBagsHelper len matrix total next newCurVal 1 newNext 1
_ ->
total
else
total
addCount : List I64, I64, I64 -> List I64
addCount = \counts, idx, val ->
when List.get counts idx is
Ok oldCount ->
List.set counts idx (oldCount + val)
_ ->
counts
# create N:M container matrix
buildMatrix : I64, List I64 -> List I64
buildMatrix = \len, input ->
initial =
{ current: 0
, outer: 0
, count: 0
, parents: List.repeat (len * len) 0
, length: len
}
final = List.walk input walker initial
final.parents
MatrixAcc : { current : I64, outer : I64, count : I64, parents : List I64, length : I64 }
walker : I64, MatrixAcc -> MatrixAcc
walker = \val, acc ->
when val is
58 ->
newOuter = acc.current
{ acc & current: 0, outer: newOuter }
124 ->
newCount = acc.current
{ acc & current: 0, count: newCount }
60 ->
idx = index acc.length acc.current acc.outer
newParents = List.set acc.parents idx acc.count
{ acc & current: 0, count: 0, parents: newParents }
10 ->
{ acc & outer: 0 }
_ ->
newCurrent = 10 * acc.current + val - 48
{ acc & current: newCurrent }
index : I64, I64, I64 -> I64
index = \len, inner, outer ->
inner * len + outer
# test data
testInput1 : List I64
testInput1 =
[ 49, 58, 49, 124, 56, 60, 10
, 50, 58, 51, 124, 53, 60, 52, 124, 52, 60, 10
, 51, 58, 51, 124, 49, 60, 52, 124, 55, 60, 10
, 52, 58, 10
, 53, 58, 10
, 54, 58, 49, 124, 49, 60, 50, 124, 55, 60, 10
, 55, 58, 50, 124, 56, 60, 57, 124, 53, 60, 10
, 56, 58, 49, 124, 50, 60, 50, 124, 57, 60, 10
, 57, 58, 53, 124, 53, 60, 54, 124, 52, 60, 10
]
testInput2 : List I64
testInput2 =
[ 49, 58, 50, 124, 50, 60, 10
, 50, 58, 50, 124, 51, 60, 10
, 51, 58, 50, 124, 52, 60, 10
, 52, 58, 50, 124, 53, 60, 10
, 53, 58, 50, 124, 54, 60, 10
, 54, 58, 50, 124, 55, 60, 10
, 55, 58, 10
]