day 3b complete
This commit is contained in:
parent
e9a8f04140
commit
b3827cf53a
3
day3/3b/go.mod
Normal file
3
day3/3b/go.mod
Normal file
@ -0,0 +1,3 @@
|
||||
module PWD
|
||||
|
||||
go 1.17
|
96
day3/3b/main.go
Normal file
96
day3/3b/main.go
Normal file
@ -0,0 +1,96 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"math"
|
||||
"os"
|
||||
)
|
||||
|
||||
func main() {
|
||||
content := returnContent("../input")
|
||||
|
||||
//content := returnContent("testInput")
|
||||
|
||||
oxygen, carbon := findOxygenAndCarbon(content, 0)
|
||||
|
||||
fmt.Println(oxygen, carbon)
|
||||
|
||||
fmt.Println(binaryToInteger(oxygen) * binaryToInteger(carbon))
|
||||
}
|
||||
|
||||
func findOxygenAndCarbon(content *[]string, i int) (oxygen string, carbon string) {
|
||||
//recursion
|
||||
|
||||
oxygen = (*findValues(content, 0, true))[0]
|
||||
|
||||
carbon = (*findValues(content, 0, false))[0]
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
//recursively find the values that fit the criteria
|
||||
func findValues(content *[]string, i int, inclusive bool) (values *[]string) {
|
||||
|
||||
if len(*content) <= 1 {
|
||||
return content
|
||||
}
|
||||
|
||||
count := 0
|
||||
var bit byte = '0'
|
||||
for j := 0; j < len(*content); j++ {
|
||||
if ((*content)[j])[i] == '1' {
|
||||
count++
|
||||
}
|
||||
}
|
||||
|
||||
if inclusive {
|
||||
if float64(count) >= float64(len(*content))/2 {
|
||||
bit = '1'
|
||||
}
|
||||
} else {
|
||||
if float64(count) < float64(len(*content))/2 {
|
||||
bit = '1'
|
||||
}
|
||||
}
|
||||
|
||||
var newContent []string
|
||||
for j := 0; j < len(*content); j++ {
|
||||
if ((*content)[j])[i] == bit {
|
||||
newContent = append(newContent, (*content)[j])
|
||||
}
|
||||
}
|
||||
i++
|
||||
return findValues(&newContent, i, inclusive)
|
||||
}
|
||||
|
||||
func binaryToInteger(input string) (value int) {
|
||||
n := 0
|
||||
for i := len(input) - 1; i >= 0; i-- {
|
||||
if input[i] == '1' {
|
||||
value += (int(math.Pow(float64(2), float64(n))))
|
||||
}
|
||||
n++
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func returnContent(path string) *[]string {
|
||||
//read file and return it as an array of integers
|
||||
|
||||
file, err := os.Open(path)
|
||||
var content []string
|
||||
|
||||
if err != nil {
|
||||
fmt.Println("Unlucky, the file didn't open")
|
||||
return &content
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
scanner := bufio.NewScanner(file)
|
||||
for scanner.Scan() {
|
||||
content = append(content, scanner.Text())
|
||||
}
|
||||
|
||||
return &content
|
||||
}
|
12
day3/3b/testInput
Normal file
12
day3/3b/testInput
Normal file
@ -0,0 +1,12 @@
|
||||
00100
|
||||
11110
|
||||
10110
|
||||
10111
|
||||
10101
|
||||
01111
|
||||
00111
|
||||
11100
|
||||
10000
|
||||
11001
|
||||
00010
|
||||
01010
|
Loading…
Reference in New Issue
Block a user