-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
179 lines (156 loc) · 2.68 KB
/
main.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
package main
import (
"io"
"strconv"
aoc "github.com/teivah/advent-of-code"
)
func fs1(input io.Reader) int {
lines := aoc.ReaderToStrings(input)
gr := gammaRate(lines)
er := epsilonRate(lines)
a, err := strconv.ParseInt(gr, 2, 64)
if err != nil {
panic(err)
}
b, err := strconv.ParseInt(er, 2, 64)
if err != nil {
panic(err)
}
return int(a) * int(b)
}
func gammaRate(lines []string) string {
res := ""
for i := 0; i < len(lines[0]); i++ {
zero := 0
one := 0
for j := 0; j < len(lines); j++ {
if lines[j][i] == '0' {
zero++
} else {
one++
}
}
if zero > one {
res += "0"
} else {
res += "1"
}
}
return res
}
func epsilonRate(lines []string) string {
res := ""
for i := 0; i < len(lines[0]); i++ {
zero := 0
one := 0
for j := 0; j < len(lines); j++ {
if lines[j][i] == '0' {
zero++
} else {
one++
}
}
if zero < one {
res += "0"
} else {
res += "1"
}
}
return res
}
func fs2(input io.Reader) int {
lines := aoc.ReaderToStrings(input)
or := oxygenRating(lines)
cr := co2Rating(lines)
a, err := strconv.ParseInt(or, 2, 64)
if err != nil {
panic(err)
}
b, err := strconv.ParseInt(cr, 2, 64)
if err != nil {
panic(err)
}
return int(a) * int(b)
}
func oxygenRating(lines []string) string {
oxygen := make(map[int]struct{}, len(lines))
for i := 0; i < len(lines); i++ {
oxygen[i] = struct{}{}
}
for i := 0; i < len(lines[0]); i++ {
if len(oxygen) == 1 {
return lines[getFirstKey(oxygen)]
}
zero := 0
one := 0
for k := range oxygen {
line := lines[k]
if line[i] == '0' {
zero++
} else {
one++
}
}
for j := 0; j < len(lines); j++ {
if lines[j][i] == '0' {
if one > zero {
delete(oxygen, j)
} else if zero > one {
} else {
delete(oxygen, j)
}
} else {
if one > zero {
} else if zero > one {
delete(oxygen, j)
} else {
}
}
}
}
return lines[getFirstKey(oxygen)]
}
func co2Rating(lines []string) string {
co2 := make(map[int]struct{}, len(lines))
for i := 0; i < len(lines); i++ {
co2[i] = struct{}{}
}
for i := 0; i < len(lines[0]); i++ {
if len(co2) == 1 {
return lines[getFirstKey(co2)]
}
zero := 0
one := 0
for k := range co2 {
line := lines[k]
if line[i] == '0' {
zero++
} else {
one++
}
}
for j := 0; j < len(lines); j++ {
if lines[j][i] == '0' {
if one > zero {
} else if zero > one {
delete(co2, j)
} else {
}
} else {
if one > zero {
delete(co2, j)
} else if zero > one {
} else {
delete(co2, j)
}
}
}
}
return ""
}
func getFirstKey(m map[int]struct{}) int {
for k := range m {
return k
}
return -1
}