solutions for the Advent of Code 2023
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

83 lines
1.3 KiB

package main
import (
"bufio"
"fmt"
"io"
"strconv"
"strings"
)
var replacer = strings.NewReplacer(",", " ", ";", " ")
func maxgame(r io.Reader) (red, green, blue int) {
scanner := bufio.NewScanner(r)
scanner.Split(bufio.ScanWords)
for {
if !scanner.Scan() {
break
}
n, _ := strconv.Atoi(scanner.Text())
scanner.Scan()
switch scanner.Text() {
case "red":
if n > red {
red = n
}
case "green":
if n > green {
green = n
}
case "blue":
if n > blue {
blue = n
}
}
}
return
}
type day02 struct{}
func (d day02) solve1(r io.Reader) string {
var (
totalred = 12
totalgreen = 13
totalblue = 14
gameSum = 0
scanner = bufio.NewScanner(r)
)
for gameN := 1; scanner.Scan(); gameN++ {
line := scanner.Text()
_, game, _ := strings.Cut(line, ": ")
sr := strings.NewReader(replacer.Replace(game))
red, green, blue := maxgame(sr)
if red < totalred && green < totalgreen && blue < totalblue {
gameSum += gameN
}
}
return fmt.Sprintf("%d", gameSum)
}
func (d day02) solve2(r io.Reader) string {
var (
sum = 0
scanner = bufio.NewScanner(r)
)
for scanner.Scan() {
line := scanner.Text()
_, game, _ := strings.Cut(line, ": ")
sr := strings.NewReader(replacer.Replace(game))
red, green, blue := maxgame(sr)
sum += (red * green * blue)
}
return fmt.Sprintf("%d", sum)
}