-
Notifications
You must be signed in to change notification settings - Fork 0
/
aoc03.go
65 lines (53 loc) · 915 Bytes
/
aoc03.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
package main
import (
"bufio"
"fmt"
"os"
)
func bin2dec(ds []int) int {
res := 0
for _, d := range ds {
if d == 0 {
res *= 2
} else {
res = 2*res + 1
}
}
return res
}
func main() {
file, err := os.Open("input03.txt")
if err != nil {
fmt.Println(err)
}
defer file.Close()
inp := make([]string, 0)
scanner := bufio.NewScanner(file)
for scanner.Scan() {
inp = append(inp, scanner.Text())
}
l := len(inp)
m := len(inp[0])
sums := make([]int, 0)
for j := 0; j < m; j++ {
cnt := 0
for i := 0; i < l; i++ {
if inp[i][j] == '1' {
cnt += 1
}
}
sums = append(sums, cnt)
}
gamma := make([]int, 0)
epsilon := make([]int, 0)
for j := 0; j < m; j++ {
if sums[j] > l/2 {
gamma = append(gamma, 1)
epsilon = append(epsilon, 0)
} else {
gamma = append(gamma, 0)
epsilon = append(epsilon, 1)
}
}
fmt.Println(bin2dec(gamma) * bin2dec(epsilon))
}