-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
209 lines (169 loc) · 5.03 KB
/
main.go
File metadata and controls
209 lines (169 loc) · 5.03 KB
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
package main
import (
"bufio"
"fmt"
"io"
"math"
"os"
"path"
"sort"
"strings"
"time"
)
func input() *os.File {
input, err := os.Open(path.Join("2021", "8", "input.txt"))
if err != nil {
panic(err)
}
return input
}
type record struct {
patterns []string
output []string
}
// map alphabetically sorted segment display to the number it represents
var segmentsToDigit = map[string]int{
"abcefg": 0,
"cf": 1,
"acdeg": 2,
"acdfg": 3,
"bcdf": 4,
"abdfg": 5,
"abdefg": 6,
"acf": 7,
"abcdefg": 8,
"abcdfg": 9,
}
// alphabetical list of segments
var segments = []byte{'a', 'b', 'c', 'd', 'e', 'f', 'g'}
func parse(r io.Reader) []record {
scanner := bufio.NewScanner(r)
var records []record
for scanner.Scan() {
line := scanner.Text()
parts := strings.Split(line, " | ")
records = append(records, record{
patterns: strings.Split(parts[0], " "),
output: strings.Split(parts[1], " "),
})
}
if scanner.Err() != nil {
panic(scanner.Err())
}
return records
}
func solve(r io.Reader) {
records := parse(r)
sum := 0
start := time.Now()
for _, record := range records {
mapping := findSignalSegmentMapping(record.patterns)
sum += translateDigits(record.output, mapping)
}
fmt.Println(sum, time.Now().Sub(start))
}
/*
a -> 1 7
b ->
c -> 6 8
d -> 0 8
e 8 -> 9
f ->
*/
// findSignalSegmentMapping finds the mapping which satisfies all digits in patterns, or in other words
// the mapping which results in all translated patterns being valid digits.
// It does so by trying every single permutation of possible mappings. This is the place to optimize.
func findSignalSegmentMapping(patterns []string) map[byte]byte {
mapping := make(map[byte]byte)
perm := make([]int, len(segments))
for i := range perm {
perm[i] = i
}
updateMapping := func() {
for i, v := range perm {
mapping[byte('a'+i)] = byte('a' + v)
}
}
updateMapping()
for !areAllDigitsValid(patterns, mapping) {
ok := nextPermutation(perm)
if !ok {
panic("no more permutations to try")
}
updateMapping()
}
return mapping
}
// nextPermutation generates the next permutation of nums. It does so using Knuth's permutation algorithm from
// https://stackoverflow.com/a/11208543 and returns false when all permutations are exhausted.
func nextPermutation(nums []int) bool {
var largestIndex = -1
for i := len(nums) - 2; i >= 0; i-- {
if nums[i] < nums[i+1] {
largestIndex = i
break
}
}
if largestIndex < 0 {
return false
}
var largestIndex2 = -1
for i := len(nums) - 1; i >= 0; i-- {
if nums[largestIndex] < nums[i] {
largestIndex2 = i
break
}
}
tmp := nums[largestIndex]
nums[largestIndex] = nums[largestIndex2]
nums[largestIndex2] = tmp
for i, j := largestIndex+1, len(nums)-1; i < j; {
tmp = nums[i]
nums[i] = nums[j]
nums[j] = tmp
i++
j--
}
return true
}
// translateDigits translates every digit in mapping to an integer, and then assembles the digits into a single integer,
// treading digits as a single seven-segment display.
func translateDigits(digits []string, mapping map[byte]byte) int {
sum := 0
for i, digit := range digits {
v, ok := translateDigit(digit, mapping)
if !ok {
panic("should be a valid mapping")
}
sum += int(math.Pow10(len(digits)-i-1)) * v
}
return sum
}
// areAllDigitsValid translates every digit in digits using mapping. If all result in a digit, it returns true.
func areAllDigitsValid(digits []string, mapping map[byte]byte) bool {
for _, digit := range digits {
_, ok := translateDigit(digit, mapping)
if !ok {
return false
}
}
return true
}
// translateDigit takes a digit composed of mixed up segments and returns the integer digit it represents.
// It does so by passing digit through mapping to un-scramble the segments and then comparing against segmentsToDigit.
// If mapping is invalid, and results in the second return value will be false.
func translateDigit(digit string, mapping map[byte]byte) (int, bool) {
bytes := make([]byte, len(digit))
for i := 0; i < len(digit); i++ {
bytes[i] = mapping[digit[i]]
}
sort.Slice(bytes, func(i, j int) bool {
return bytes[i] < bytes[j]
})
val, ok := segmentsToDigit[string(bytes)]
return val, ok
}
func main() {
solve(strings.NewReader("be cfbegad cbdgef fgaecd cgeb fdcge agebfd fecdb fabcd edb | fdgacbe cefdb cefbgd gcbe\nedbfga begcd cbg gc gcadebf fbgde acbgfd abcde gfcbed gfec | fcgedb cgb dgebacf gc\nfgaebd cg bdaec gdafb agbcfd gdcbef bgcad gfac gcb cdgabef | cg cg fdcagb cbg\nfbegcd cbd adcefb dageb afcb bc aefdc ecdab fgdeca fcdbega | efabcd cedba gadfec cb\naecbfdg fbg gf bafeg dbefa fcge gcbea fcaegb dgceab fcbdga | gecf egdcabf bgf bfgea\nfgeab ca afcebg bdacfeg cfaedg gcfdb baec bfadeg bafgc acf | gebdcfa ecba ca fadegcb\ndbcfg fgd bdegcaf fgec aegbdf ecdfab fbedc dacgb gdcebf gf | cefg dcbef fcge gbcadfe\nbdfegc cbegaf gecbf dfcage bdacg ed bedf ced adcbefg gebcd | ed bcgafe cdgba cbgef\negadfb cdbfeg cegd fecab cgb gbdefca cg fgcdab egfdb bfceg | gbdfcae bgc cg cgb\ngcafb gcf dcaebfg ecagb gf abcdeg gaef cafbge fdbac fegbdc | fgae cfgab fg bagce"))
solve(input())
}