AdventOfCode2021/day03/3a/main.go

62 lines
1.1 KiB
Go
Raw Normal View History

2021-12-03 19:40:58 +00:00
package main
import (
2021-12-17 13:23:44 +00:00
"AdventOfCode2021/shared"
2021-12-03 19:40:58 +00:00
"bufio"
"fmt"
"os"
)
func main() {
content := returnContent("../input")
2021-12-16 19:55:49 +00:00
//content := returnContent("../testInput")
2021-12-03 19:40:58 +00:00
gamma, epsilon := findGammaAndEpsilon(content)
2021-12-17 13:23:44 +00:00
fmt.Println(shared.BinaryToInteger(&gamma) * shared.BinaryToInteger(&epsilon))
2021-12-03 19:40:58 +00:00
}
func findGammaAndEpsilon(content *[]string) (gamma string, epsilon string) {
for i := 0; i < len((*content)[1]); i++ {
count := 0
//Loop through list checking index[i] for each string
for j := 0; j < len(*content); j++ {
if ((*content)[j])[i] == '1' {
count++
}
}
if count >= len(*content)/2 {
gamma += "1"
epsilon += "0"
} else {
gamma += "0"
epsilon += "1"
}
}
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
}