Skip to content

Commit 9dc97a3

Browse files
committed
day 8 part 2
1 parent e59fed3 commit 9dc97a3

File tree

1 file changed

+55
-1
lines changed

1 file changed

+55
-1
lines changed

08/main.go

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,13 +114,23 @@ func main() {
114114
}
115115

116116
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)
118121
}
119122

120123
type World struct {
121124
problems []*Problem
122125
}
123126

127+
func (w *World) SumOutputValues() (sum int) {
128+
for _, p := range w.problems {
129+
sum += p.WhatIsOutputValue()
130+
}
131+
return sum
132+
}
133+
124134
func (w *World) HowManyTimesDoDigitsAppear(digits map[int]struct{}) (count int) {
125135
for _, p := range w.problems {
126136
p.Komput()
@@ -146,13 +156,17 @@ type Problem struct {
146156

147157
func (p *Problem) Komput() {
148158
length6digs := [][]string{}
159+
length5digs := [][]string{}
149160
for _, segment := range p.segmentValues {
150161
// simple cases out of the way first, 1,4,7,8
151162
val := p.WhatIsSegmentValue(segment)
152163
if val == -1 {
153164
if len(segment) == 6 {
154165
length6digs = append(length6digs, segment)
155166
}
167+
if len(segment) == 5 {
168+
length5digs = append(length5digs, segment)
169+
}
156170
continue
157171
}
158172
p.foundValues[val] = segment
@@ -189,6 +203,38 @@ func (p *Problem) Komput() {
189203
length6digs = nil
190204
}
191205
}
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+
}
192238
}
193239

194240
// diff returns the number of digits from a but not in b.
@@ -216,6 +262,14 @@ func contains(s []string, char string) bool {
216262
return false
217263
}
218264

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+
219273
// returns segment value, or -1 if not known yet.
220274
func (p *Problem) WhatIsSegmentValue(testValue []string) int {
221275
// A simple test for getting values 1,4,7,8 is to check length

0 commit comments

Comments
 (0)