2021/03 part 1 finished started with part 2
This commit is contained in:
parent
55c353b141
commit
2d8a3d5780
3 changed files with 140 additions and 28 deletions
|
@ -3,7 +3,10 @@ package main
|
|||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"math/bits"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func GetData(s string) []string {
|
||||
|
@ -19,39 +22,96 @@ func GetData(s string) []string {
|
|||
}
|
||||
return result
|
||||
}
|
||||
func ConvertToUsefulData(s []string) [][]uint {
|
||||
a := make([][]uint, len(s[0]))
|
||||
for i := range a {
|
||||
a[i] = make([]uint, len(s))
|
||||
}
|
||||
for i, data := range s {
|
||||
split := strings.Split(data, "")
|
||||
for j, data2 := range split {
|
||||
x, _ := strconv.Atoi(data2)
|
||||
a[j][i] = uint(x)
|
||||
}
|
||||
}
|
||||
return a
|
||||
}
|
||||
func main() {
|
||||
fmt.Println("GoFast!")
|
||||
|
||||
result := Part1(ConvertToUsefulData(GetData("./data")))
|
||||
fmt.Printf("The Power consumption is: %d", result)
|
||||
}
|
||||
|
||||
func Part1(s []string) int {
|
||||
var store []rune
|
||||
var result []string
|
||||
resultLen := len([]rune(s[0]))
|
||||
|
||||
for x := 0; x < resultLen; x++ {
|
||||
zero, one := 0, 0
|
||||
for _, report := range s {
|
||||
chars := []rune(report)
|
||||
store = append(store, chars[x])
|
||||
func Part1(data [][]uint) uint {
|
||||
gammaRate := uint(0)
|
||||
epsilonRate := uint(0)
|
||||
for _, rows := range data {
|
||||
dict := make(map[uint]uint)
|
||||
for _, entry := range rows {
|
||||
dict[entry] = dict[entry] + 1
|
||||
}
|
||||
for _, data := range store {
|
||||
switch data {
|
||||
case '0':
|
||||
zero += 1
|
||||
case '1':
|
||||
one += 1
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Println(zero, one)
|
||||
if zero < one {
|
||||
result = append(result, "1")
|
||||
if dict[0] > dict[1] {
|
||||
gammaRate = gammaRate << 1
|
||||
epsilonRate = epsilonRate << 1
|
||||
epsilonRate, _ = bits.Add(epsilonRate, 1, 0)
|
||||
} else {
|
||||
result = append(result, "0")
|
||||
gammaRate = gammaRate << 1
|
||||
epsilonRate = epsilonRate << 1
|
||||
gammaRate, _ = bits.Add(gammaRate, 1, 0)
|
||||
}
|
||||
zero, one = 0, 0
|
||||
}
|
||||
fmt.Println(result)
|
||||
//fmt.Printf("Gamma Rate: %b as int (%d)\n", gammaRate, gammaRate)
|
||||
//fmt.Printf("Epsilon Rate: %b as int (%d)\n", epsilonRate, epsilonRate)
|
||||
|
||||
return gammaRate * epsilonRate
|
||||
}
|
||||
func ConvertToUsefulDataPart2(s []string) [][]uint {
|
||||
a := make([][]uint, len(s))
|
||||
for i := range a {
|
||||
a[i] = make([]uint, len(s[0]))
|
||||
}
|
||||
for i, data := range s {
|
||||
split := strings.Split(data, "")
|
||||
for j, data2 := range split {
|
||||
x, _ := strconv.Atoi(data2)
|
||||
a[j][i] = uint(x)
|
||||
}
|
||||
}
|
||||
return a
|
||||
}
|
||||
|
||||
func WhichIsMore(data []uint) int {
|
||||
dict := make(map[uint]uint)
|
||||
for _, entry := range data {
|
||||
dict[entry] = dict[entry] + 1
|
||||
}
|
||||
if dict[0] == dict[1] {
|
||||
return 1
|
||||
}
|
||||
if dict[0] < dict[1] {
|
||||
return 1
|
||||
}
|
||||
if dict[0] > dict[1] {
|
||||
return 0
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func Part2(data [][]uint) (uint, uint) {
|
||||
oxygenRate := uint(0)
|
||||
for _, row := range data {
|
||||
fmt.Println(row)
|
||||
more := WhichIsMore(row)
|
||||
//countOnly := WhichIsMore(row)
|
||||
switch more {
|
||||
case 0:
|
||||
|
||||
case 1:
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
for _, row := range data {
|
||||
fmt.Println(row)
|
||||
}
|
||||
return 0, 0
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue