Second day
This commit is contained in:
@@ -0,0 +1,81 @@
|
||||
package day02
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
utils "git.anthonygueguen.fr/g3po/aoc2023/internal"
|
||||
)
|
||||
|
||||
const (
|
||||
MAX_RED int = 12
|
||||
MAX_GREEN = 13
|
||||
MAX_BLUE = 14
|
||||
)
|
||||
|
||||
const (
|
||||
RED string = "red"
|
||||
GREEN = "green"
|
||||
BLUE = "blue"
|
||||
)
|
||||
|
||||
func GetPossibleGame(content []string) []int {
|
||||
var idx []int
|
||||
lines:
|
||||
for i, line := range content {
|
||||
line := strings.Split(line, ":")[1]
|
||||
sets := strings.Split(line, ";")
|
||||
for _, set := range sets {
|
||||
red, green, blue := 0, 0, 0
|
||||
words := strings.Split(set, " ")
|
||||
for k, w := range words {
|
||||
switch w {
|
||||
case RED, RED + ",":
|
||||
red += utils.Must[int](strconv.Atoi(words[k-1]))
|
||||
if red > MAX_RED {
|
||||
continue lines
|
||||
}
|
||||
case GREEN, GREEN + ",":
|
||||
green += utils.Must[int](strconv.Atoi(words[k-1]))
|
||||
if green > MAX_GREEN {
|
||||
continue lines
|
||||
}
|
||||
case BLUE, BLUE + ",":
|
||||
blue += utils.Must[int](strconv.Atoi(words[k-1]))
|
||||
if blue > MAX_BLUE {
|
||||
continue lines
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
idx = append(idx, i+1)
|
||||
}
|
||||
return idx
|
||||
}
|
||||
|
||||
func GetPowerOfGame(content []string) []int {
|
||||
var power []int
|
||||
r := regexp.MustCompile(`(\d)* (blue|red|green)`)
|
||||
for _, line := range content {
|
||||
var red, green, blue []int
|
||||
cubes := r.FindAllString(line, -1)
|
||||
for _, c := range cubes {
|
||||
switch {
|
||||
case strings.Contains(c, RED):
|
||||
val := utils.Must[int](strconv.Atoi(strings.Split(c, " ")[0]))
|
||||
red = append(red, val)
|
||||
case strings.Contains(c, GREEN):
|
||||
val := utils.Must[int](strconv.Atoi(strings.Split(c, " ")[0]))
|
||||
green = append(green, val)
|
||||
case strings.Contains(c, BLUE):
|
||||
val := utils.Must[int](strconv.Atoi(strings.Split(c, " ")[0]))
|
||||
blue = append(blue, val)
|
||||
}
|
||||
}
|
||||
p := slices.Max(red) * slices.Max(green) * slices.Max(blue)
|
||||
power = append(power, p)
|
||||
}
|
||||
return power
|
||||
}
|
||||
@@ -20,3 +20,11 @@ func Init() []string {
|
||||
content := strings.Split(string(bytes), "\n")
|
||||
return content[:len(content)-1]
|
||||
}
|
||||
|
||||
func SumIntArr(arr []int) int {
|
||||
sum := 0
|
||||
for _, v := range arr {
|
||||
sum += v
|
||||
}
|
||||
return sum
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user