-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday08.go
142 lines (131 loc) · 2.77 KB
/
day08.go
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
package main
import (
"aoc2021/util"
"fmt"
"sort"
"strings"
"sync"
"time"
)
func main() {
lines := util.GetLines("day08\\08.in")
start := time.Now()
day08a(lines)
duration := time.Since(start)
day08b(lines)
duration2 := time.Since(start)
fmt.Printf("p1: %s, p2: %s\n", duration, duration2-duration)
}
func countDigits(line string) int {
numbers := 0
groups := strings.Fields(line[60:])
for _, group := range groups {
l := len(group)
switch l {
case 2, 3, 4, 7:
numbers++
}
}
return numbers
}
func stringInside(a, b string) bool {
for _, r := range a {
if !strings.ContainsRune(b, r) {
return false
}
}
return true
}
func overlap(a, b string) int {
overlap := 0
for _, r := range a {
if strings.ContainsRune(b, r) {
overlap++
}
}
return overlap
}
// Taken from: https://stackoverflow.com/questions/22688651/golang-how-to-sort-string-or-byte
func sortString(w string) string {
s := strings.Split(w, "")
sort.Strings(s)
return strings.Join(s, "")
}
func decodeDigits(line string, ch chan int, wg *sync.WaitGroup) {
defer wg.Done()
decoder := map[string]int{}
encoder := map[int]string{}
encoding := []string{}
groups := []string{}
//sort fields
for i, v := range strings.Fields(line) {
sorted := sortString(v)
if i < 10 {
encoding = append(encoding, sorted)
} else if i > 10 {
groups = append(groups, sorted)
}
}
//set easy items
for _, code := range encoding {
switch len(code) {
case 2:
decoder[code] = 1
encoder[1] = code
case 3:
decoder[code] = 7
encoder[7] = code
case 4:
decoder[code] = 4
encoder[4] = code
case 7:
decoder[code] = 8
encoder[8] = code
}
}
//set dependend
for _, code := range encoding {
switch len(code) {
case 5:
if stringInside(encoder[1], code) {
decoder[code] = 3
} else if overlap(encoder[4], code) == 3 {
decoder[code] = 5
} else {
decoder[code] = 2
}
case 6:
if stringInside(encoder[4], code) {
decoder[code] = 9
} else if stringInside(encoder[1], code) {
decoder[code] = 0
} else {
decoder[code] = 6
}
}
}
ch <- decoder[groups[0]]*1000 + decoder[groups[1]]*100 +
decoder[groups[2]]*10 + decoder[groups[3]]
}
func day08a(lines []string) {
sum1478 := 0
for _, line := range lines {
sum1478 += countDigits(line)
}
fmt.Printf("Solution for part A: %d\n", sum1478)
}
func day08b(lines []string) {
sum := 0
sampleChan := make(chan int, len(lines))
var wg sync.WaitGroup
for _, line := range lines {
wg.Add(1)
go decodeDigits(line, sampleChan, &wg)
}
wg.Wait()
close(sampleChan)
for s := range sampleChan {
sum += s
}
fmt.Printf("Solution for part B: %d\n", sum)
}