half of day 3

This commit is contained in:
ZennDev1337 2023-11-15 18:34:41 +01:00
parent be09a5df2c
commit 9d18226366
2 changed files with 47 additions and 3 deletions

View file

@ -1,11 +1,55 @@
package main package main
import "fmt" import (
"bufio"
"fmt"
"os"
)
func GetData(s string) []string {
var result []string
fileReader, err := os.Open(s)
if err != nil {
return nil
}
lines := bufio.NewScanner(fileReader)
lines.Split(bufio.ScanLines)
for lines.Scan() {
result = append(result, lines.Text())
}
return result
}
func main() { func main() {
fmt.Println("GoFast!") fmt.Println("GoFast!")
} }
func Part1() int { func Part1(s []string) int {
var store []rune
var result []string
resultLen := len([]rune(s[0]))
zero, one := 0, 0
for x := 0; x < resultLen; x++ {
for _, report := range s {
chars := []rune(report)
store = append(store, chars[x])
}
for _, data := range store {
switch data {
case '0':
zero++
case '1':
one++
}
}
fmt.Println(zero, one)
if zero < one {
result = append(result, "1")
} else {
result = append(result, "0")
}
zero, one = 0, 0
}
fmt.Println(result)
return 0 return 0
} }

View file

@ -4,7 +4,7 @@ import "testing"
func TestPart1(t *testing.T) { func TestPart1(t *testing.T) {
const expected = 198 const expected = 198
result := Part1() result := Part1(GetData("./test-data"))
if result != expected { if result != expected {
t.Fatalf("Expected: %d Result: %d", expected, result) t.Fatalf("Expected: %d Result: %d", expected, result)
} }