-
Notifications
You must be signed in to change notification settings - Fork 0
/
binary_diagnostic.go
205 lines (163 loc) · 3.69 KB
/
binary_diagnostic.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
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
package main
import (
"log"
"os"
"path/filepath"
"strconv"
"strings"
)
const pathInput = "input.txt"
const pathTestInput = "test.txt"
func logErr(e error) {
if e != nil {
log.Panicln(e)
}
}
func readFile(path string) (str string) {
fp, err := filepath.Abs(path)
logErr(err)
dat, err := os.ReadFile(fp)
logErr(err)
str = string(dat)
return str
}
type binaryIndex struct {
zeros int
ones int
}
func coutOccurrences(spl []string) (result []binaryIndex) {
digits := len(spl[0])
// init slice of results
for i := 0; i < digits; i++ {
result = append(result, binaryIndex{0, 0})
}
for _, line := range spl {
for i, rn := range line {
switch rn {
case '0':
result[i].zeros++
break
case '1':
result[i].ones++
break
default:
log.Panicln("Unhandled rune", rn)
}
}
}
return result
}
func binaryToInt(binary string) (dec int) {
res, err := strconv.ParseInt(binary, 2, 64)
logErr(err)
return int(res)
}
func part1(spl []string, debug bool) (product int) {
res := coutOccurrences(spl)
gammaRate := ""
epsilonRate := ""
for i := 0; i < len(res); i++ {
if res[i].ones == res[i].zeros {
log.Panicln("Zeroes and Ones are the same =>", res[i].ones, res[i].zeros)
} else if res[i].ones > res[i].zeros {
gammaRate += "1"
epsilonRate += "0"
} else {
gammaRate += "0"
epsilonRate += "1"
}
}
if debug {
log.Println("gammaRate => ", gammaRate)
log.Println("epsilonRate => ", epsilonRate)
}
gammaInt := binaryToInt(gammaRate)
epsilonInt := binaryToInt(epsilonRate)
if debug {
log.Println("gammaInt => ", gammaInt)
log.Println("epsilonInt => ", epsilonInt)
}
return gammaInt * epsilonInt
}
func coutOccurrencesForPosition(spl []string, pos int) (result binaryIndex) {
for _, line := range spl {
rn := rune(line[pos])
switch rn {
case '0':
result.zeros++
break
case '1':
result.ones++
break
default:
log.Panicln("Unhandled rune", rn)
}
}
return result
}
func filterSinglePosition(spl []string, position int, value rune) (filteredSpl []string) {
for _, line := range spl {
if rune(line[position]) == value {
filteredSpl = append(filteredSpl, line)
}
}
return filteredSpl
}
func part2(spl []string, debug bool) (product int) {
var OxySeq string
var Co2Seq string
filteredSplOxy := spl
filteredSplCo2 := spl
for i := 0; i < len(spl[0]); i++ {
var filterOxyRune rune
var filterCo2Rune rune
if OxySeq == "" {
resOxy := coutOccurrencesForPosition(filteredSplOxy, i)
if resOxy.ones == resOxy.zeros {
filterOxyRune = '1'
} else if resOxy.ones > resOxy.zeros {
filterOxyRune = '1'
} else {
filterOxyRune = '0'
}
filteredSplOxy = filterSinglePosition(filteredSplOxy, i, filterOxyRune)
if len(filteredSplOxy) == 1 {
OxySeq = filteredSplOxy[0]
}
}
if Co2Seq == "" {
resCo2 := coutOccurrencesForPosition(filteredSplCo2, i)
if resCo2.ones == resCo2.zeros {
filterCo2Rune = '0'
} else if resCo2.ones > resCo2.zeros {
filterCo2Rune = '0'
} else {
filterCo2Rune = '1'
}
filteredSplCo2 = filterSinglePosition(filteredSplCo2, i, filterCo2Rune)
if len(filteredSplCo2) == 1 {
Co2Seq = filteredSplCo2[0]
}
}
}
if debug {
log.Println("OxySeq => ", OxySeq)
log.Println("Co2Seq => ", Co2Seq)
}
oxyInt := binaryToInt(OxySeq)
co2Int := binaryToInt(Co2Seq)
if debug {
log.Println("oxyInt => ", oxyInt)
log.Println("co2Int => ", co2Int)
}
return oxyInt * co2Int
}
func main() {
str := readFile(pathInput)
// fields --> split by whitespace and newline
splice := strings.Fields(str)
prod1 := part1(splice, false)
log.Println("Part1 result =>", prod1)
prod2 := part2(splice, true)
log.Println("Part2 result =>", prod2)
}