aboutsummaryrefslogtreecommitdiff
path: root/2021/03/second.go
blob: 33938a112ae6e4d13167533a406a2f3f5a716103 (plain)
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
package main

import (
	"bufio"
	"fmt"
	"log"
	"os"
)

func findBitCount(data []string) []int {
	bits := make([]int, 12)
	for _, line := range data {
		for n := 0; n < 12; n++ {
			if line[n] == '1' {
				bits[n]++
			}
		}
	}
	return bits
}

func main() {
	f, err := os.Open("input")
	if err != nil {
		log.Fatalf("%+v", err)
	}
	defer f.Close()

	scanner := bufio.NewScanner(f)
	scanner.Split(bufio.ScanLines)

	data := make([]string, 0)
	for scanner.Scan() {
		data = append(data, scanner.Text())
	}

	datacsr := make([]string, len(data))
	copy(datacsr, data)

	var ogr int
	for n := 0; n < 12; n++ {
		if len(data) == 1 {
			break
		}
		bits := findBitCount(data)
		var i byte
		if 2*bits[n] >= len(data) {
			i = '1'
		} else {
			i = '0'
		}
		newdata := make([]string, 0)
		for j := 0; j < len(data); j++ {
			if data[j][n] == i {
				newdata = append(newdata, data[j])
			}
		}
		data = newdata
	}
	for n := 0; n < 12; n++ {
		ogr *= 2
		if data[0][n] == '1' {
			ogr++
		}
	}

	data = datacsr
	var csr int
	for n := 0; n < 12; n++ {
		if len(data) == 1 {
			break
		}
		bits := findBitCount(data)
		var i byte
		if 2*bits[n] < len(data) {
			i = '1'
		} else {
			i = '0'
		}
		newdata := make([]string, 0)
		for j := 0; j < len(data); j++ {
			if data[j][n] == i {
				newdata = append(newdata, data[j])
			}
		}
		data = newdata
	}
	for n := 0; n < 12; n++ {
		csr *= 2
		if data[0][n] == '1' {
			csr++
		}
	}

	fmt.Println(ogr * csr)
}