first commit
This commit is contained in:
parent
d388972aee
commit
7b65e60352
9 changed files with 394 additions and 0 deletions
78
2023/day04/main.go
Normal file
78
2023/day04/main.go
Normal file
|
@ -0,0 +1,78 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"os"
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Data struct {
|
||||
CardNr int
|
||||
WinNumber []int
|
||||
YourNumber []int
|
||||
}
|
||||
|
||||
func convertStringToInt(s []string) []int {
|
||||
var result []int
|
||||
for _, x := range s {
|
||||
inti, err := strconv.Atoi(x)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
result = append(result, inti)
|
||||
}
|
||||
return result
|
||||
}
|
||||
func getData(s string) []string {
|
||||
var result []string
|
||||
fileReader, err := os.Open(s)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
fileScanner := bufio.NewScanner(fileReader)
|
||||
fileScanner.Split(bufio.ScanLines)
|
||||
for fileScanner.Scan() {
|
||||
result = append(result, fileScanner.Text())
|
||||
}
|
||||
return result
|
||||
}
|
||||
func convertToUsefulData(s []string) []Data {
|
||||
var result []Data
|
||||
for i, line := range s {
|
||||
spl := strings.Split(line, ":")
|
||||
numbers := strings.Split(spl[1], "|")
|
||||
win := strings.Fields(numbers[0])
|
||||
your := strings.Fields(numbers[1])
|
||||
result = append(result, Data{CardNr: i, WinNumber: convertStringToInt(win), YourNumber: convertStringToInt(your)})
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func main() {
|
||||
fmt.Println("Go Fast!")
|
||||
data := convertToUsefulData(getData("./data"))
|
||||
fmt.Println(data)
|
||||
Part1(data)
|
||||
}
|
||||
|
||||
func Part1(data []Data) {
|
||||
var result int
|
||||
for i, _ := range data {
|
||||
var count int
|
||||
for j, _ := range data[i].YourNumber {
|
||||
if slices.Contains(data[i].WinNumber, data[i].YourNumber[j]) {
|
||||
if count == 0 {
|
||||
count++
|
||||
} else {
|
||||
count *= 2
|
||||
}
|
||||
}
|
||||
}
|
||||
result += count
|
||||
fmt.Printf("card %d Points: %d\n", i, count)
|
||||
}
|
||||
fmt.Println("The result is: ", result)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue