aboutsummaryrefslogtreecommitdiff
path: root/2021/03
diff options
context:
space:
mode:
Diffstat (limited to '2021/03')
-rw-r--r--2021/03/first.go42
-rw-r--r--2021/03/input1000
-rw-r--r--2021/03/second.go96
3 files changed, 1138 insertions, 0 deletions
diff --git a/2021/03/first.go b/2021/03/first.go
new file mode 100644
index 0000000..5343505
--- /dev/null
+++ b/2021/03/first.go
@@ -0,0 +1,42 @@
+package main
+
+import (
+ "bufio"
+ "fmt"
+ "log"
+ "os"
+)
+
+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)
+
+ bits := make([]int, 12)
+ for scanner.Scan() {
+ num := scanner.Text()
+ for n := 0; n < 12; n++ {
+ if num[n] == '1' {
+ bits[n]++
+ }
+ }
+ }
+
+ gamma := 0
+ epsilon := 0
+ for n := 0; n < 12; n++ {
+ gamma *= 2
+ epsilon *= 2
+ if bits[n] > 500 {
+ gamma++
+ } else {
+ epsilon++
+ }
+ }
+ fmt.Println(gamma * epsilon)
+}
diff --git a/2021/03/input b/2021/03/input
new file mode 100644
index 0000000..99c0fc4
--- /dev/null
+++ b/2021/03/input
@@ -0,0 +1,1000 @@
+111011001010
+010011101110
+110001001010
+001101011101
+110100000011
+010110110010
+101000110110
+010100000011
+111001101111
+111001100011
+000110000100
+111011110100
+010001100000
+100011000010
+001001111010
+101001010010
+110000111010
+011100110101
+010111101111
+100011001001
+011110100001
+011101110110
+001011111001
+001110010100
+100001001000
+000100110111
+011111110010
+010101110011
+001101011100
+100001110101
+111000100000
+010111001010
+110000001010
+101011110100
+101010011111
+011001001000
+010010100100
+001100111110
+011111100101
+011010011001
+000010010100
+010001110111
+000101011001
+001110101100
+100011100101
+110001011100
+100101011110
+111000001010
+000010111010
+001001000010
+011011001100
+000111110000
+110110110010
+001011111100
+110111110101
+001000101010
+101010011001
+111110011001
+001000111110
+110110010010
+010011001000
+110100100010
+111000100011
+110111000001
+110110010001
+001100011011
+111100010111
+110110101001
+100010000101
+001001101000
+010000110101
+111010001100
+110000001110
+100111110010
+110101110011
+011100111010
+100000010001
+101000111101
+001010001100
+110100000000
+011001110100
+011010101111
+001011011000
+111100101000
+101001110100
+001111100001
+110111110111
+101100010000
+110000010101
+110111010101
+101110100100
+011010001001
+011101110111
+101011110111
+011001101111
+101101001011
+010000100011
+111011111100
+110101000100
+011111101011
+101000001110
+000111001010
+011100010010
+010011011111
+100000001101
+000110001011
+101100110101
+001001010000
+001111101100
+110100010011
+100011111001
+110000101100
+101111101101
+010011100110
+011000011101
+111110001111
+000100111011
+000001001100
+111110000010
+001001110111
+100000110010
+011000111000
+100010100110
+111110100010
+000001010110
+011010001101
+001100001010
+100111001001
+001010110101
+100111000101
+110011001110
+011100000110
+101111111110
+001110100010
+110101011010
+000011111001
+101011110110
+000111000110
+110010001100
+001001010101
+011111001011
+100010101101
+001000010000
+110111110001
+001111110011
+110111101101
+010011010001
+100110101000
+001011100100
+001010111011
+101000010001
+000101001011
+011010010011
+011011000001
+111111010101
+010000011001
+101100111101
+001001110010
+000010001011
+111000001001
+010001001100
+110110011011
+111011100100
+001000001010
+101110010110
+010010000100
+100000011011
+010000110011
+111111101101
+101010001011
+001011110100
+000111100011
+110011100111
+000011100000
+100101001110
+110100000010
+110101110101
+100001010101
+010100000110
+011010110110
+001011000101
+010010100001
+001001100110
+111011110111
+100110011111
+100111010000
+010010111010
+001000101100
+000101101110
+011001010111
+010001000110
+001110111000
+010001001010
+111000011000
+000011110000
+111100011110
+101110111001
+001101001000
+111010110110
+001110101010
+100001111011
+110010010110
+101001101011
+001100111011
+111001010100
+101111011011
+010010000010
+101101101111
+101000101000
+001100100010
+111001100101
+110110001011
+000110011001
+110010110000
+000111101111
+010010001101
+110110111101
+010100100100
+001111010110
+001101000011
+011110010011
+101111101010
+111001000110
+000110001001
+100100001000
+010100110010
+001011001011
+100010110100
+001110101111
+001001101011
+000111100100
+000001111011
+100111111101
+111011100110
+000100101101
+100110110000
+010001000000
+111111111111
+011010001110
+001100000100
+000010010000
+000100100000
+010100010101
+100100111110
+001100111100
+000001010010
+011010000001
+000000111010
+110011110111
+110011100110
+000011011011
+001001001001
+100111100111
+110000101110
+101100101111
+110001110110
+111011111001
+101000110001
+010000011110
+001011110101
+001100011010
+111001110001
+001110110001
+101011010100
+110110100111
+101011001100
+110010111100
+011111001111
+110001101011
+011100111100
+111101110010
+001111000001
+100101110101
+110001101000
+110001100110
+000011110110
+001111111111
+110101001111
+101000111001
+101100111001
+110011011011
+011100100111
+111000001100
+110010001001
+000101001000
+010101101100
+110010000100
+011000010101
+010001101111
+011000000100
+111111100101
+000011101011
+011001111010
+011010000101
+000001001011
+001111001010
+001011011010
+010011111010
+101001110001
+101001000000
+100101100101
+111100000010
+110000000100
+010110000101
+011011011101
+010110101001
+010011000111
+110000010001
+000101100001
+111000000100
+000011001101
+101101110011
+110101011101
+001111000010
+101110011100
+000100001010
+111101110001
+110110111111
+001011101011
+100001010100
+011001110010
+010001111000
+001101101011
+011011101001
+010111101010
+011110000000
+111111000010
+100010110111
+111010111111
+100110001001
+011111000000
+000111000010
+111001111100
+111010100111
+001010110010
+100011110001
+011100000100
+011110010110
+101001000011
+100011111101
+001100100111
+000110100100
+001001000111
+101000000000
+011100010101
+101110010111
+000110010001
+010101001111
+010010000111
+001010001111
+000111000101
+111101111010
+010010111111
+101111101110
+101100101000
+101101010101
+000011111011
+011111000101
+011111111001
+101000100111
+010101110111
+000111101100
+011010011010
+110001000010
+100110111100
+011110011011
+001101011010
+010001011001
+100110100001
+001010111001
+011100011011
+110001111101
+100111100100
+101111111101
+111000011001
+000000101110
+101111011100
+010100111111
+010001010100
+011001111100
+110100010001
+110001111110
+111001110000
+001110010111
+001100101101
+111001100001
+000100011101
+011101011010
+010110010000
+100000111100
+100100100110
+110111111000
+001111000111
+111111110010
+111010111011
+001000111000
+101010110101
+011001110110
+001100110001
+000111101001
+010011000001
+001101011001
+111001111010
+000110011011
+000101000100
+110000011011
+010011011000
+011100111111
+010101101101
+101100110100
+001011010101
+010001000011
+001000000111
+111111011011
+110000000011
+100101010110
+001001001101
+000101010100
+111101101100
+000111110101
+100101010010
+101110001110
+101011111110
+011011000011
+000111010101
+011010001000
+001010110000
+011110000110
+001111100110
+111001111110
+000101101111
+110111100100
+100011101001
+111000100101
+100100111101
+110001000011
+110000101111
+110001111100
+000101000111
+001011110000
+010000001100
+101101110001
+000110100111
+011101000001
+001111101101
+100000000100
+010010000000
+001111101000
+001010111010
+101010011010
+111010100011
+101111110010
+001011110110
+100010010001
+010110010110
+110111111010
+000111001011
+000001111100
+100101101111
+010100011101
+000010011110
+011100100000
+011101100000
+110000100111
+110111100000
+100001110100
+011111011011
+101110110001
+010000011000
+010011010101
+001101100101
+110110100101
+101000011110
+101011000011
+000010010101
+101001110000
+110001011111
+001101101101
+000001111111
+110010111001
+111001010101
+000010101100
+111011100101
+011111001110
+011000011010
+111111110110
+111010110111
+011010010110
+110011001011
+000010101000
+011101011100
+111010110000
+010011110010
+011110010001
+101111111001
+010010110101
+101010101001
+001101110001
+011111110110
+011110110001
+001111001100
+110000011001
+111001000101
+111110101001
+010011010100
+110011000010
+110000100100
+101000101001
+010101011010
+011001100011
+101110101101
+100100001011
+111010010001
+111010001110
+100110101100
+111010010111
+101111001010
+111101111111
+101111000110
+101111101111
+011001001101
+100110000111
+101101010100
+110100011010
+010111010001
+110011101011
+101001001100
+100111010111
+000011111110
+011110000101
+001011110111
+100000110110
+011110100111
+011100011111
+010100111101
+100100010011
+001000101001
+010111010110
+100000011100
+000000111111
+111101111001
+011011100100
+101011010110
+111010001111
+111101111101
+110100110010
+010001001110
+011001110011
+110100110101
+110010100010
+111111111101
+000110011100
+011010100110
+000000010011
+100100010110
+110110010111
+101011101001
+110000000010
+001110011001
+111111011110
+000111000001
+110010101011
+111111111001
+010001010010
+001111010100
+011011110101
+110000000111
+100011010010
+000010110111
+111100111111
+111111101010
+011000100011
+011111000100
+011100001001
+110001011011
+011011010111
+100000101111
+101001100100
+110000110101
+000001110111
+110101001000
+000001100000
+010001111110
+011110011101
+011011100111
+110000011010
+111010100110
+001101110010
+111000010000
+010110101000
+010001010111
+000010101111
+111000001110
+111011100001
+111100011001
+101111001110
+101011011000
+101100101100
+111111110011
+101010010101
+110110010101
+001100111010
+100001101000
+001100000011
+010110110000
+101110101011
+100001100101
+011001011010
+110110001111
+001000000110
+011101011111
+001010101001
+001111101001
+111100011111
+100010110010
+000001101110
+011110011010
+010110100101
+010010100011
+101010001000
+010000001001
+111100110011
+110010001111
+000101100010
+011111100000
+000100000001
+100100010000
+011110000011
+000010010001
+110010101001
+000000001010
+110010011100
+000000100100
+100010110001
+101111001100
+000001101010
+111011010110
+010101011101
+100110101011
+101001011011
+010011100100
+111000001000
+100111110001
+100100101011
+011001101001
+010101110000
+101000011001
+101111110111
+010111110011
+011111111111
+011100110011
+010010110010
+111011111101
+011110000010
+100001101010
+110100011100
+001001010011
+001110000010
+000110000101
+010110001010
+101000101010
+100000101001
+000001101100
+001001111011
+100011010000
+010000100000
+001111111000
+111011111000
+100011101000
+100110001100
+010001010011
+100110110101
+100001010011
+100100101111
+011101101011
+011110111101
+101110010100
+101010110111
+001010001011
+011111001101
+111011001101
+111011000110
+001100000001
+101100110111
+101100110010
+001000110011
+111010101010
+010101010110
+111100111001
+111000101001
+111000010101
+001000101101
+100111000010
+100000010110
+000100011000
+010100011100
+111010110011
+100011100011
+111111110101
+110101010110
+011001001011
+011101110100
+111110000110
+011010100101
+110100010010
+011111100010
+111010110001
+100111000100
+100000010101
+111000110010
+111100101001
+101010100010
+000110010010
+001111101111
+010011111011
+110010000011
+000010101101
+111111100111
+101001110110
+100111011001
+011010011111
+011011011001
+110001000001
+111011011011
+001011001111
+100101000010
+110000010010
+000011011000
+010111001001
+010001111010
+110101000110
+111011101010
+001100110101
+001111000110
+000000000010
+010100111010
+111101110110
+101101110101
+011001010101
+010110001101
+011110100011
+010101010101
+100110100011
+101111110011
+000111111101
+010011000100
+000010110101
+000001011010
+101100001000
+000101111001
+100001001100
+110110000100
+111010011000
+110000001000
+010111001101
+100111000000
+000001010000
+000110101110
+000100110011
+101011101000
+100110110010
+111011100000
+100000101011
+111100101011
+100010100011
+101001000001
+110001111010
+001001110101
+001000101110
+101011111010
+000110110110
+000100010000
+111001110010
+100010111101
+111110110010
+001111110001
+011111101101
+001001101111
+110111100001
+000010111111
+101110001111
+101011101011
+110001010010
+011010100111
+011011010110
+111100001000
+111100001111
+111101011110
+010000000000
+000000011000
+001000000100
+010111100001
+101110010011
+000101000101
+101100010110
+010111011111
+000010100101
+100001111110
+011101001000
+100010001101
+100111100001
+011010110001
+010110101100
+111010110100
+010110010101
+010100111100
+110001100101
+010010011000
+111110110011
+110001101100
+100011001110
+100001000000
+101001100000
+010111111101
+011000110001
+101001011101
+100110011101
+011101111000
+000000100001
+010000010000
+110100010110
+101100011101
+111011001111
+100101111000
+110101010100
+101111000011
+000101110000
+110101110100
+010110111000
+100010101001
+010111001111
+001011111101
+001111010011
+101001101010
+011110010100
+001100110111
+010111010111
+000001100100
+100001000010
+101001111011
+000011000100
+111000100100
+111010111001
+101101001111
+001010110011
+001000000011
+111111101001
+101001010101
+001010110110
+110101110010
+101101010001
+101010111000
+110100000101
+111100110100
+100010111000
+010010001111
+011000111101
+110011011000
+101010111001
+011000010000
+001001000001
+001100101000
+110111001001
+011001000110
+101001101000
+100010011101
+000001001101
+010001011110
+111111000001
+000111100010
+110110011111
+000001110110
+110101010001
+110100100011
+100110010100
+010101100111
+000000011110
+001000010011
+000101011010
+010101101110
+001010111100
+110100111101
+101101011011
+011011101100
+100000000001
+000101010111
+101010010011
+110001000110
+101110001000
+010110000010
+110100001011
+010110100110
+000110100101
+111110111100
+110010101101
+110111101110
+001111000000
+001101010000
+010011000011
+011110010101
+100001000100
+000000111011
+010011010110
+100110110111
+111000000000
+100101111110
+110011001100
+100001001111
+100100010010
+001101111011
+110010000010
+011101001011
+011000010111
+010000010100
+100100101010
+001100101100
+000100101001
+100100000011
+101010111011
+000001100101
+101011110101
+100101111111
+010101100001
+101000001010
+000011110010
+111001010010
+100001100011
+010111111111
+001110011000
+010001011000
+000011001110
+110010111011
+001100001111
+100001110111
+110000010000
+101110000001
+001011100110
+010101001101
+001001001111
+001110100100
+111110111010
+011101100010
+001010011011
+011011010011
+111000101011
+110110110001
+110100101111
+000000100010
+111111101111
+111000101101
+011001000010
+100000001001
+000101011100
+110100100110
+111011011111
+110110111100
+110011110000
+111110011110
+001101010010
+101000111110
+010101110110
+101100000011
+001111101110
+010101011001
+110010110010
+100100110111
+011100000001
+101110000110
+001011101000
+000011100111
+010101000000
+101011100000
+100100010001
+011011100010
+011010000100
+010010110011
+011010111101
+010010011100
+010010010011
+011000011111
+101101111111
+010100001010
+100100110100
+101011101111
+100100100010
+010001011101
+101101110000
+110010000101
+111011010111
+010011110111
+100101000011
+001100111101
+110110100000
+101001100011
+111001000011
+100001011111
+010000111101
+100011100001
+101100011010
+100110100000
diff --git a/2021/03/second.go b/2021/03/second.go
new file mode 100644
index 0000000..33938a1
--- /dev/null
+++ b/2021/03/second.go
@@ -0,0 +1,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)
+}