-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
110 lines (85 loc) · 1.56 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
package main
import (
"bufio"
"fmt"
"io"
"os"
"path"
)
func input() *os.File {
input, err := os.Open(path.Join("2021", "3", "input.txt"))
if err != nil {
panic(err)
}
return input
}
const width = 12
func solve(r io.Reader) {
scanner := bufio.NewScanner(r)
var vals []string
for scanner.Scan() {
row := scanner.Text()
vals = append(vals, row)
}
if scanner.Err() != nil {
panic(scanner.Err())
}
vals2 := make([]string, len(vals))
copy(vals2, vals)
pos := 0
for len(vals) != 1 {
common := count(vals)
if common[pos] >= 0 {
vals = discard(vals, pos, '0')
} else {
vals = discard(vals, pos, '1')
}
pos += 1
}
pos = 0
for len(vals2) != 1 {
common := count(vals2)
if common[pos] < 0 {
vals2 = discard(vals2, pos, '0')
} else {
vals2 = discard(vals2, pos, '1')
}
pos += 1
}
fmt.Println(vals, vals2, toInt(vals[0]), toInt(vals2[0]), toInt(vals[0])*toInt(vals2[0]))
}
func toInt(v string) int {
sum := 0
for i, vv := range v {
if vv == '1' {
sum += (1 << (width - i - 1))
}
}
return sum
}
func discard(vals []string, i int, w uint8) []string {
var ret []string
for _, v := range vals {
if v[i] != w {
ret = append(ret, v)
}
}
return ret
}
func count(nums []string) []int {
var sum []int = make([]int, width)
for _, row := range nums {
for i, v := range row {
if v == '0' {
sum[i] -= 1
} else {
sum[i] += 1
}
}
}
return sum
}
func main() {
//solve(strings.NewReader("00100\n11110\n10110\n10111\n10101\n01111\n00111\n11100\n10000\n11001\n00010\n01010"))
solve(input())
}