@@ -16,49 +16,79 @@ func main() {
16
16
}
17
17
inputstrarr := strings .Split (input , "\n " )
18
18
19
- gamma , epsilon := getShit (inputstrarr )
20
- fmt .Printf ("Answer: %d\n " , gamma * epsilon )
19
+ ox , co2 := getShit (inputstrarr )
20
+ fmt .Printf ("Answer: %d\n " , ox * co2 )
21
21
}
22
22
23
- func getShit (vals []string ) (gamma int , epsilon int ) {
24
- // This loops over the length of each binary
25
- gammaBin := ""
26
- epsilonBin := ""
23
+ func getShit (vals []string ) (ox int , co2 int ) {
24
+ // This function should call getVal for oxygen number (most common bitwise)
25
+ // And for the co2 number (least common bitwise)
26
+ // Then return both
27
+
28
+ oxBin := getVal (vals , true )
29
+ co2bin := getVal (vals , false )
30
+
31
+ fmt .Println (oxBin )
32
+ oxInt , err := strconv .ParseInt (oxBin , 2 , 32 )
33
+ if err != nil {
34
+ panic (err )
35
+ }
36
+ ox = int (oxInt )
37
+
38
+ fmt .Println (co2bin )
39
+ co2Int , err := strconv .ParseInt (co2bin , 2 , 32 )
40
+ if err != nil {
41
+ panic (err )
42
+ }
43
+ co2 = int (co2Int )
44
+
45
+ return ox , co2
46
+ }
47
+
48
+ // gelVal gets a value based on the most common bit selector, or least common if false.
49
+ func getVal (vals []string , mostCommon bool ) string {
27
50
for i := 0 ; i < len (vals [0 ]); i ++ {
51
+ if len (vals ) == 1 {
52
+ return vals [0 ]
53
+ }
54
+
28
55
// This loops over all values in the vals array.
29
- count1 := 0
30
- count0 := 0
56
+ filtered1 := [] string {}
57
+ filtered0 := [] string {}
31
58
for a := 0 ; a < len (vals ); a ++ {
32
59
switch string (vals [a ][i ]) {
33
60
case "1" :
34
- count1 ++
61
+ filtered1 = append ( filtered1 , vals [ a ])
35
62
case "0" :
36
- count0 ++
63
+ filtered0 = append ( filtered0 , vals [ a ])
37
64
}
38
65
}
39
66
40
- if count0 > count1 {
41
- gammaBin = gammaBin + "0"
42
- epsilonBin = epsilonBin + "1"
67
+ if mostCommon {
68
+ if len (filtered1 ) > len (filtered0 ) {
69
+ vals = filtered1
70
+ continue
71
+ }
72
+ if len (filtered0 ) > len (filtered1 ) {
73
+ vals = filtered0
74
+ continue
75
+ }
76
+ // if equal, return 1?
77
+ vals = filtered1
78
+ continue
43
79
} else {
44
- gammaBin = gammaBin + "1"
45
- epsilonBin = epsilonBin + "0"
80
+ if len (filtered1 ) < len (filtered0 ) {
81
+ vals = filtered1
82
+ continue
83
+ }
84
+ if len (filtered0 ) < len (filtered1 ) {
85
+ vals = filtered0
86
+ continue
87
+ }
88
+ // if equal, return 0?
89
+ vals = filtered0
90
+ continue
46
91
}
47
92
}
48
-
49
- fmt .Println (gammaBin )
50
- gammaInt , err := strconv .ParseInt (gammaBin , 2 , 32 )
51
- if err != nil {
52
- panic (err )
53
- }
54
- gamma = int (gammaInt )
55
-
56
- fmt .Println (epsilonBin )
57
- epsilonInt , err := strconv .ParseInt (epsilonBin , 2 , 32 )
58
- if err != nil {
59
- panic (err )
60
- }
61
- epsilon = int (epsilonInt )
62
-
63
- return gamma , epsilon
93
+ return vals [0 ]
64
94
}
0 commit comments