This commit is contained in:
parent
2e0faf87eb
commit
d2ce81647b
@ -14,9 +14,10 @@ func check(e error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type gameSet struct {
|
var requiredCubes = map[string]int {
|
||||||
nCubes []int
|
"red": 12,
|
||||||
color []string
|
"green": 13,
|
||||||
|
"blue": 14,
|
||||||
}
|
}
|
||||||
|
|
||||||
type possibleGame struct {
|
type possibleGame struct {
|
||||||
@ -34,49 +35,63 @@ func splitSets (s string) ([]string, int) {
|
|||||||
return sets, numSets
|
return sets, numSets
|
||||||
}
|
}
|
||||||
|
|
||||||
func newGameSet(s []string, i int) []gameSet {
|
func newGameSet(s string) map[string]int {
|
||||||
// Make an array of structs as big as we need
|
m := make(map[string]int)
|
||||||
mySets := make([]gameSet, i)
|
|
||||||
_ = mySets
|
tempNumColor := strings.SplitN(s, ",", -1)
|
||||||
// Iterate the array of strings
|
|
||||||
// e.g., s = 4 red, 5 blue, 7 green
|
for i := 0; i < len(tempNumColor); i++ {
|
||||||
for index, set := range s {
|
TrimmedNumColor := strings.Trim(tempNumColor[i], " ")
|
||||||
// We extract every set
|
NumColor := strings.SplitN(TrimmedNumColor, " ", -1)
|
||||||
tempNumAndColor := strings.SplitN(set, ",", -1)
|
|
||||||
_ = index
|
m[NumColor[1]], _ = strconv.Atoi(NumColor[0])
|
||||||
mySets[index].nCubes = make([]int, len(tempNumAndColor))
|
}
|
||||||
mySets[index].color = make([]string, len(tempNumAndColor))
|
|
||||||
// The order is always number-space-color
|
return m
|
||||||
// We now populate the struct
|
}
|
||||||
for j := 0; j < len(tempNumAndColor); j++ {
|
func CheckGame(mySet map[string]int, p *possibleGame) {
|
||||||
// Remove leading whitespace
|
|
||||||
TrimmedNumColor := strings.Trim(tempNumAndColor[j], " ")
|
if (mySet["red"] > requiredCubes["red"]) {
|
||||||
// Split into number and color
|
p.redIsPossible = false
|
||||||
NumColor := strings.SplitN(TrimmedNumColor, " ", -1)
|
}
|
||||||
// [0] is number, [1] is color
|
if (mySet["green"] > requiredCubes["green"]) {
|
||||||
mySets[index].nCubes[j], _ = strconv.Atoi(NumColor[0])
|
p.greenIsPossible = false
|
||||||
mySets[index].color[j] = NumColor[1]
|
}
|
||||||
}
|
if (mySet["blue"] > requiredCubes["blue"]) {
|
||||||
|
p.blueIsPossible = false
|
||||||
}
|
}
|
||||||
return mySets
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func CheckGame(mySets []gameSet) bool {
|
func PossibleGamesSum(s string, gn int, gt *int) {
|
||||||
|
var isPossible possibleGame
|
||||||
return false
|
// Initialize everything to true. If everything was set to false,
|
||||||
}
|
// we would have to check in both directions for every pass
|
||||||
|
isPossible.redIsPossible = true
|
||||||
func PossibleGamesSum(s string) bool {
|
isPossible.greenIsPossible = true
|
||||||
|
isPossible.blueIsPossible = true
|
||||||
|
// We will pass a pointer so we can go one map at a time and
|
||||||
|
// still maintain the results
|
||||||
|
var isPossiblePoint *possibleGame
|
||||||
|
isPossiblePoint = &isPossible
|
||||||
// We receive a string with the sets, not split
|
// We receive a string with the sets, not split
|
||||||
// We proceed to split
|
// We proceed to split
|
||||||
sets, numSets := splitSets(s)
|
sets, numSets := splitSets(s)
|
||||||
// We received a []string with sets and the number of sets
|
// We received a []string with sets and the number of sets
|
||||||
// Now it's time to create a struct
|
// Now it's time to create a map
|
||||||
mySets := newGameSet(sets, numSets)
|
var mySet map[string]int
|
||||||
isPossible := CheckGame(mySets)
|
// For every set we have in the current game
|
||||||
_ = isPossible
|
for i := 0; i < numSets; i++ {
|
||||||
return false
|
// We create a map
|
||||||
|
mySet = newGameSet(sets[i])
|
||||||
|
// We check if the game is possible
|
||||||
|
CheckGame(mySet, isPossiblePoint)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isPossible.redIsPossible == true) &&
|
||||||
|
(isPossible.greenIsPossible == true) &&
|
||||||
|
(isPossible.blueIsPossible == true) {
|
||||||
|
*gt += gn
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func PrintAndWait[T any](x T) {
|
func PrintAndWait[T any](x T) {
|
||||||
@ -91,6 +106,12 @@ func main() {
|
|||||||
|
|
||||||
// This variable will hold the game number
|
// This variable will hold the game number
|
||||||
var gameNum int = 0
|
var gameNum int = 0
|
||||||
|
// This variable will hold the pointer of the sum of possible games
|
||||||
|
var gameSumPoint *int
|
||||||
|
gameSum := 0
|
||||||
|
gameSumPoint = &gameSum
|
||||||
|
|
||||||
|
|
||||||
_ = gameNum
|
_ = gameNum
|
||||||
scanner := bufio.NewScanner(file)
|
scanner := bufio.NewScanner(file)
|
||||||
|
|
||||||
@ -102,6 +123,7 @@ func main() {
|
|||||||
// We convert the number that remains after replacing "Game " with ""
|
// We convert the number that remains after replacing "Game " with ""
|
||||||
gameNum, _ = strconv.Atoi(strings.Replace(gameAndCubes[0], "Game ", "", 1))
|
gameNum, _ = strconv.Atoi(strings.Replace(gameAndCubes[0], "Game ", "", 1))
|
||||||
// Now, for every game, split the sets
|
// Now, for every game, split the sets
|
||||||
PossibleGamesSum(gameAndCubes[1])
|
PossibleGamesSum(gameAndCubes[1], gameNum, gameSumPoint)
|
||||||
}
|
}
|
||||||
|
fmt.Printf("The sum of possible games is: %d\n", gameSum)
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user