@@ -114,13 +114,23 @@ func main() {
114
114
}
115
115
116
116
HowManyTimesDoDigitsAppear := world .HowManyTimesDoDigitsAppear (digitsUnderTest )
117
- fmt .Printf ("Answer: %d\n " , HowManyTimesDoDigitsAppear )
117
+ fmt .Printf ("How many times digits appear: %d\n " , HowManyTimesDoDigitsAppear )
118
+
119
+ sumOutputs := world .SumOutputValues ()
120
+ fmt .Printf ("Sum of output values: %d\n " , sumOutputs )
118
121
}
119
122
120
123
type World struct {
121
124
problems []* Problem
122
125
}
123
126
127
+ func (w * World ) SumOutputValues () (sum int ) {
128
+ for _ , p := range w .problems {
129
+ sum += p .WhatIsOutputValue ()
130
+ }
131
+ return sum
132
+ }
133
+
124
134
func (w * World ) HowManyTimesDoDigitsAppear (digits map [int ]struct {}) (count int ) {
125
135
for _ , p := range w .problems {
126
136
p .Komput ()
@@ -146,13 +156,17 @@ type Problem struct {
146
156
147
157
func (p * Problem ) Komput () {
148
158
length6digs := [][]string {}
159
+ length5digs := [][]string {}
149
160
for _ , segment := range p .segmentValues {
150
161
// simple cases out of the way first, 1,4,7,8
151
162
val := p .WhatIsSegmentValue (segment )
152
163
if val == - 1 {
153
164
if len (segment ) == 6 {
154
165
length6digs = append (length6digs , segment )
155
166
}
167
+ if len (segment ) == 5 {
168
+ length5digs = append (length5digs , segment )
169
+ }
156
170
continue
157
171
}
158
172
p .foundValues [val ] = segment
@@ -189,6 +203,38 @@ func (p *Problem) Komput() {
189
203
length6digs = nil
190
204
}
191
205
}
206
+
207
+ // find digits of length 5 now...
208
+ for {
209
+ if len (length5digs ) == 0 {
210
+ // yay we found them all.
211
+ break
212
+ }
213
+
214
+ // if we have a diff of 1 between segment 6 and this segment, this segment is 5.
215
+ for i , val := range length5digs {
216
+ if diff (p .foundValues [6 ], val ) == 1 {
217
+ p .foundValues [5 ] = val
218
+ length5digs = append (length5digs [:i ], length5digs [i + 1 :]... )
219
+ break
220
+ }
221
+ }
222
+
223
+ // if we have a diff of 1 between segment 9 and this segment, this segment is 3.
224
+ for i , val := range length5digs {
225
+ if diff (p .foundValues [9 ], val ) == 1 {
226
+ p .foundValues [3 ] = val
227
+ length5digs = append (length5digs [:i ], length5digs [i + 1 :]... )
228
+ break
229
+ }
230
+ }
231
+
232
+ // the last one must be 2...
233
+ if len (length5digs ) == 1 {
234
+ p .foundValues [2 ] = length5digs [0 ]
235
+ length5digs = nil
236
+ }
237
+ }
192
238
}
193
239
194
240
// diff returns the number of digits from a but not in b.
@@ -216,6 +262,14 @@ func contains(s []string, char string) bool {
216
262
return false
217
263
}
218
264
265
+ func (p * Problem ) WhatIsOutputValue () (outputVal int ) {
266
+ for _ , testVal := range p .testValues {
267
+ val := p .WhatIsSegmentValue (testVal )
268
+ outputVal = (outputVal * 10 ) + val
269
+ }
270
+ return outputVal
271
+ }
272
+
219
273
// returns segment value, or -1 if not known yet.
220
274
func (p * Problem ) WhatIsSegmentValue (testValue []string ) int {
221
275
// A simple test for getting values 1,4,7,8 is to check length
0 commit comments