2023-12-02 18:28:38 -07:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
_ "embed"
|
|
|
|
"fmt"
|
2023-12-08 20:33:36 -07:00
|
|
|
"os"
|
2023-12-02 18:28:38 -07:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2023-12-08 20:33:36 -07:00
|
|
|
|
2023-12-15 15:13:24 -07:00
|
|
|
aoc "go.sour.is/advent-of-code"
|
2023-12-02 18:28:38 -07:00
|
|
|
)
|
|
|
|
|
2023-12-08 20:33:36 -07:00
|
|
|
func main() {
|
|
|
|
result, err := aoc.Runner(run)
|
|
|
|
if err != nil {
|
|
|
|
aoc.Log("ERR", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
aoc.Log(result)
|
|
|
|
}
|
|
|
|
|
|
|
|
type result struct {
|
|
|
|
sum int
|
|
|
|
powerSum int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r result) String() string {
|
|
|
|
return fmt.Sprintln("result pt1:", r.sum, "\nresult pt2:", r.powerSum)
|
|
|
|
}
|
2023-12-02 18:28:38 -07:00
|
|
|
|
|
|
|
type gameResult struct {
|
|
|
|
red, green, blue int
|
|
|
|
}
|
|
|
|
|
2023-12-08 20:33:36 -07:00
|
|
|
func run(scan *bufio.Scanner) (*result, error) {
|
2023-12-02 18:28:38 -07:00
|
|
|
// only 12 red cubes, 13 green cubes, and 14 blue cubes
|
|
|
|
maxCounts := gameResult{
|
|
|
|
red: 12,
|
|
|
|
green: 13,
|
|
|
|
blue: 14,
|
|
|
|
}
|
|
|
|
|
|
|
|
games := [][]gameResult{}
|
|
|
|
games = append(games, []gameResult{})
|
|
|
|
|
|
|
|
for scan.Scan() {
|
|
|
|
pfx, text, ok := strings.Cut(scan.Text(), ":")
|
|
|
|
if !ok || !strings.HasPrefix(pfx, "Game") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-12-13 08:32:37 -07:00
|
|
|
rounds := aoc.SliceMap(func(text string) gameResult {
|
|
|
|
round := gameResult{}
|
2023-12-02 18:28:38 -07:00
|
|
|
|
2023-12-13 08:32:37 -07:00
|
|
|
for _, result := range strings.Split(text, ",") {
|
2023-12-02 18:28:38 -07:00
|
|
|
ns, color, _ := strings.Cut(strings.TrimSpace(result), " ")
|
|
|
|
n, err := strconv.Atoi(ns)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch color {
|
|
|
|
case "red":
|
2023-12-13 08:32:37 -07:00
|
|
|
round.red = n
|
2023-12-02 18:28:38 -07:00
|
|
|
case "green":
|
2023-12-13 08:32:37 -07:00
|
|
|
round.green = n
|
2023-12-02 18:28:38 -07:00
|
|
|
case "blue":
|
2023-12-13 08:32:37 -07:00
|
|
|
round.blue = n
|
2023-12-02 18:28:38 -07:00
|
|
|
}
|
|
|
|
}
|
2023-12-13 08:32:37 -07:00
|
|
|
return round
|
|
|
|
}, strings.Split(text, ";")...)
|
|
|
|
|
|
|
|
games = append(games, rounds)
|
2023-12-02 18:28:38 -07:00
|
|
|
}
|
|
|
|
|
2023-12-08 20:33:36 -07:00
|
|
|
aoc.Log(games)
|
|
|
|
aoc.Log(len(games))
|
2023-12-02 18:28:38 -07:00
|
|
|
|
|
|
|
sum := 0
|
|
|
|
powerSum := 0
|
|
|
|
for i, game := range games {
|
|
|
|
if i == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
mins := gameResult{}
|
|
|
|
ok := true
|
|
|
|
|
|
|
|
for _, round := range game {
|
|
|
|
mins.red = max(mins.red, round.red)
|
|
|
|
mins.green = max(mins.green, round.green)
|
|
|
|
mins.blue = max(mins.blue, round.blue)
|
|
|
|
|
|
|
|
if maxCounts.red < round.red {
|
2023-12-08 20:33:36 -07:00
|
|
|
aoc.Log("game", i, round, "too many red", round.red)
|
2023-12-02 18:28:38 -07:00
|
|
|
ok = false
|
|
|
|
} else if maxCounts.blue < round.blue {
|
2023-12-08 20:33:36 -07:00
|
|
|
aoc.Log("game", i, round, "too many blue", round.blue)
|
2023-12-02 18:28:38 -07:00
|
|
|
ok = false
|
|
|
|
} else if maxCounts.green < round.green {
|
2023-12-08 20:33:36 -07:00
|
|
|
aoc.Log("game", i, round, "too many green", round.green)
|
2023-12-02 18:28:38 -07:00
|
|
|
ok = false
|
|
|
|
}
|
2023-12-08 20:33:36 -07:00
|
|
|
aoc.Log("game", i, round, ok)
|
2023-12-02 18:28:38 -07:00
|
|
|
}
|
|
|
|
if ok {
|
|
|
|
sum += i
|
2023-12-08 20:33:36 -07:00
|
|
|
aoc.Log("game", i, "passes", sum)
|
2023-12-02 18:28:38 -07:00
|
|
|
}
|
2023-12-08 20:33:36 -07:00
|
|
|
power := mins.red * mins.blue * mins.green
|
|
|
|
aoc.Log("game", i, "mins", mins, power)
|
2023-12-02 18:28:38 -07:00
|
|
|
powerSum += power
|
|
|
|
}
|
2023-12-08 20:33:36 -07:00
|
|
|
return &result{sum, powerSum}, nil
|
2023-12-02 18:28:38 -07:00
|
|
|
}
|