You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

88 lines
1.6 KiB
Go

package day2
import (
"fmt"
"regexp"
"strconv"
"strings"
)
func puzzleCount(lines []string) int {
result := 0
prevResult := 0
var re1 = regexp.MustCompile(`(?m)Game ([0-9]+):(.*)`)
for _, l := range lines {
matches := re1.FindStringSubmatch(l)
game, _ := strconv.Atoi(matches[1])
prevResult = result
result += game
restOfLine := matches[2]
sets := strings.Split(restOfLine, ";")
for _, s := range sets {
draws := strings.Split(s, ",")
for _, d := range draws {
colors := strings.Split(strings.TrimSpace(d), " ")
n, _ := strconv.Atoi(colors[0])
c := colors[1]
switch c {
case "red":
if n > 12 {
fmt.Println("-----------> ", game)
result = prevResult
}
case "green":
if n > 13 {
fmt.Println("-----------> ", game)
result = prevResult
}
case "blue":
if n > 14 {
fmt.Println("-----------> ", game)
result = prevResult
}
}
}
}
}
return result
}
func puzzlePower(lines []string) int {
result := 0
var re1 = regexp.MustCompile(`(?m)Game ([0-9]+):(.*)`)
for _, l := range lines {
matches := re1.FindStringSubmatch(l)
restOfLine := matches[2]
sets := strings.Split(restOfLine, ";")
minimums := map[string]int{"red": 1, "green": 1, "blue": 1}
for _, s := range sets {
draws := strings.Split(s, ",")
for _, d := range draws {
colors := strings.Split(strings.TrimSpace(d), " ")
n, _ := strconv.Atoi(colors[0])
c := colors[1]
if n > minimums[c] {
minimums[c] = n
}
}
}
power := minimums["red"] * minimums["green"] * minimums["blue"]
result += power
}
return result
}