-
Notifications
You must be signed in to change notification settings - Fork 0
/
day3.go
103 lines (92 loc) · 1.43 KB
/
day3.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
package main
import (
"bufio"
"fmt"
"os"
"strconv"
//"strings"
)
func main() {
f, _ := os.Open("day3_input")
s := bufio.NewScanner(f)
inputs := make([]string, 0)
for s.Scan() {
inputs = append(inputs, s.Text())
}
// Part1
g := make([]int, len(inputs[0]))
for _, v := range inputs {
for i := 0; i < len(v); i++ {
if v[i] == '0' {
g[i]--
} else {
g[i]++
}
}
}
var gs, es string
for _, v := range g {
if v < 0 {
gs += "0"
es += "1"
} else {
gs += "1"
es += "0"
}
}
gamma, _ := strconv.ParseInt(gs, 2, 64)
epsilon, _ := strconv.ParseInt(es, 2, 64)
fmt.Println(gamma * epsilon)
// Part2
pos := 0
ox := inputs
for len(ox) > 1 {
cb := mostCommonBit(ox, pos, '1')
filt := make([]string, 0)
for _, v := range ox {
if v[pos] == cb {
filt = append(filt, v)
}
}
ox = filt
pos++
}
co := inputs
pos = 0
for len(co) > 1 {
var cb byte
if mostCommonBit(co, pos, '1') == '0' {
cb = '1'
} else {
cb = '0'
}
filt := make([]string, 0)
for _, v := range co {
if v[pos] == cb {
filt = append(filt, v)
}
}
co = filt
pos++
}
oxv, _ := strconv.ParseInt(ox[0], 2, 64)
cov, _ := strconv.ParseInt(co[0], 2, 64)
fmt.Println(oxv * cov)
}
func mostCommonBit(inputs []string, pos int, def byte) byte {
b := 0
for _, v := range inputs {
if v[pos] == '0' {
b--
} else {
b++
}
}
if b > 0 {
return '1'
}
if b < 0 {
return '0'
}
return def
}